【POJ 2965】 The Pilots Brothers' refrigerator

【POJ 2965】 The Pilots Brothers’ refrigerator

跟1753(我博客里另一篇有讲)棋盘问题一个做法 预处理后用二进制(BFS)暴力枚举 用到异或运算 很方便 大大缩短代码量

代码如下

#include <cstdio>
#include <queue>
#include <cstring>
#include <stack>
#define INF 0x3f3f3f3f

using namespace std;

typedef struct Edge
{
    int last,x,y;
}Edge;

Edge eg[65536];
bool vis[65536];
char str[18];
int step[65536];
int dir[17];
int ms;

int GetNum()
{
    int i,x = 0;
    for(i = 1; i < 17; ++i)
    {
        x <<= 1;
        x += (str[i] == ‘+‘)? 1: 0;
    }
    return x;
}

void PrintNum(int x)
{
    int i = 1;
    while(x)
    {
        if(x&1) printf("1 ");
        else printf("0 ");
        x >>= 1;
        if(!(i%4)) printf("\n");
        i++;
    }
    for(; i < 17; ++i)
    {
        printf("0 ");
        if(!(i%4)) printf("\n");
    }
    printf("\n");
}

void GetDir()
{
    int i,j;
    for(i = 1; i < 17; ++i) str[i] = ‘-‘;
    for(i = 1; i < 17; ++i)
    {
        for(j = i; j > 0; j -= 4) str[j] = ‘+‘;
        for(j = i; j < 17; j += 4) str[j] = ‘+‘;
        for(j = (i-1)/4*4+1; j <= (i-1)/4*4+4; ++j) str[j] = ‘+‘;
        dir[i-1] = GetNum();
        for(j = i; j > 0; j -= 4) str[j] = ‘-‘;
        for(j = i; j < 17; j += 4) str[j] = ‘-‘;
        for(j = (i-1)/4*4+1; j <= (i-1)/4*4+4; ++j) str[j] = ‘-‘;
    }
}

void Bfs()
{
    memset(vis,0,sizeof(vis));
    memset(step,INF,sizeof(step));
    queue <int> q;
    step[ms] = 0;
    int u,v,i;
    q.push(ms);
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        vis[u] = -1;
        for(i = 0; i < 16; ++i)
        {
            v = u^dir[i];
            if(step[v] > step[u]+1)
            {
                step[v] = step[u]+1;
                eg[v].last = u;
                eg[v].x = i/4+1;
                eg[v].y = i - i/4*4 +1;
                if(!vis[v])
                {
                    q.push(v);
                    vis[v] = 1;
                }
            }
        }
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    int i,cnt;
    for(i = 1; i < 17; i += 4)
    {
        scanf("%s",str+i);
    }
    ms = GetNum();
    GetDir();
    Bfs();
    stack <pair <int,int> > s;
    cnt = 0;
    for(i = 0; i != ms; i = eg[i].last)
    {
        cnt++;
        s.push(pair <int,int>(eg[i].x,eg[i].y));
    }
    printf("%d\n",s.size());
    while(!s.empty())
    {
        printf("%d %d\n",s.top().first,s.top().second);
        s.pop();
    }
    return 0;
}

然而看了Disscuss发现一思路 堪称BT

他写的很好 直接为大家拷过来:

证明:要使一个为’+’的符号变为’-‘,必须其相应的行和列的操作数为奇数;可以证明,如果’+’位置对应的行和列上每一个位置都进行一次操作,则整个图只有这一’+’位置的符号改变,其余都不会改变.

设置一个4*4的整型数组,初值为零,用于记录每个点的操作数,那么在每个’+’上的行和列的的位置都加1,得到结果模2(因为一个点进行偶数次操作的效果和没进行操作一样,这就是楼上说的取反的原理),然后计算整型数组中一的

遍历后 最终状态发生改变的数目即为操作数,其位置为要操作的位置(其他原来操作数为偶数的因为操作并不发生效果,因此不进行操作)

取模可以直接通过对bool型取否 初始为假 最终为真即为操作位置

代码奉上

#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

vector <pair<int,int> > v;
bool m[5][5];
char str[5][5];

int main()
{
    memset(m,0,sizeof(m));
    int i,j,k;
    for(i = 1; i <= 4; ++i)
        scanf("%s",str[i]+1);
    for(i = 1; i <= 4; ++i)
    {
        for(j = 1; j <= 4; ++j)
        {
            if(str[i][j] == ‘+‘)
            {
                m[i][j] = !m[i][j];
                for(k = 1; k <= 4; ++k)
                {
                    m[i][k] = !m[i][k];
                    m[k][j] = !m[k][j];
                }
            }
        }
    }
    for(i = 1; i <= 4; ++i)
    {
        for(j = 1; j <= 4; ++j)
            if(m[i][j]) v.push_back(pair <int,int> (i,j));
    }
    printf("%d\n",v.size());
    for(i = 0; i < v.size(); ++i)
        printf("%d %d\n",v[i].first,v[i].second);

    return 0;
}

【POJ 2965】 The Pilots Brothers' refrigerator

时间: 2024-10-18 06:06:45

【POJ 2965】 The Pilots Brothers' refrigerator的相关文章

POJ 2965:The Pilots Brothers&#39; refrigerator

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18080   Accepted: 6855   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

【递归】POJ2965-The Pilots Brothers&#39; refrigerator

和POJ1753的方法一致,唯一不同的是需要记录路径,只需添加一个全集变量的path路径即可. 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int INF=100000; 5 int map[4][4]; 6 int path[4][4]; 7 int maxpath[4][4]; 8 int ans=INF; 9 10 void open(int step,int turn) 11

poj 2965 The Pilots Brothers&#39; refrigerator 【dfs+枚举】【双十一大礼包】

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27522   Accepted: 10625   Special Judge Description The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open

The Pilots Brothers&#39; refrigerator - poj 2965

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20325   Accepted: 7830   Special Judge Description The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator. There are 16 handle

poj 2965 The Pilots Brothers&#39; refrigerator[ 枚举 ]

传送门:http://poj.org/problem?id=2965 思路:二进制枚举,递归输出路径.G++ 900+Ms勉强过,C++超时. 代码: #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<queue> #include<stack> #include<map>

POJ 2965 The Pilots Brothers&#39; refrigerator

原题链接:http://poj.org/problem?id=2965 简单的dfs,因为是要求最小值,枚举深度来得到递归终止条件,和 poj1753很像,毕竟是放在一起的. #include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <cmath> #include <cstdlib> #include <vector&g

POJ 2965 The Pilots Brothers&#39; refrigerator (想法题)

POJ 2965 题意: 输入一个形如: -+-- ---- ---- -+-- 4*4图案,+表示close,-表示open,定义一种操作为:改变某个单元格符号(+变-,-变+),同时单元格所在行与所在列的所有单元格符号都会发生改变. 求最少操作次数能使所有单元格内都是'-'.并输出要操作的单元格. 思路: 正常的做法和POJ 1573类似,dfs枚举即可,见code1. 这里提供一种高效的做法: 通过思考我们可以验证,某一个单元格内符号为'+',同时对其所在行与所在列的所有单元格进行操作(其

poj 2965 The Pilots Brothers&#39; refrigerator(dfs 枚举 +打印路径)

链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手,'+'代表关,'-'代表开,当16个把手都为开(即'-')时,门才能打开,问至少要几步门才能打开 改变状态规则:选定16个把手中的任意一个,可以改变其本身以及同行同列的状态(即若为开,则变为关,若为关,则变为开),这一次操作为一步. 分析:这题与poj 1753思路差不多,每个把手最多改变一次状态, 所有整个矩阵最多改变16次状态 思路:直接dfs枚举所有状态,直到找到目标状态 但是要打印路径,所有应在dfs时记录路径 注

poj 2965 The Pilots Brothers&#39; refrigerator(高斯消元)

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18641   Accepted: 7145   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o