poj 2965 The Pilots Brothers' refrigerator bfs+状态压缩+路径回溯

来源:互联网 发布:xp优化工具2016 编辑:程序博客网 时间:2024/06/08 11:45

此题易超时,因为16个开关,每个开关有两种状态,可以将’-‘看成0,’+’看成1,从第一排第一个到第四排最后一个,每一种状态都可以用一个数字来代替,所以一共是(1<<16)-1种状态,即65535种,bfs还是可以的。在广搜过程中,因为每一个开关扳动的过程其对应的一行及一列都会发生变化,可以将其状态对应的值分别和这些状态对应的数字进行^处理,为了防止TLE,可以提前打个表,这样异或一下就可以了,不用异或7次。
路径回溯可以再开一个结构体,对应每一次搜索都给定一个次序,在结构体里面存两个值,一个对应改变的行列,一个对应状态改变之前的那个次序,这样在广搜搜到最终状态的时候就可以了一步一步回溯到最终的结果。
题目:
The Pilots Brothers’ refrigerator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 23926 Accepted: 9221 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 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.

The task is to determine the minimum number of handle switching necessary to open the refrigerator.

Input

The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.

Output

The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.

Sample Input

-+–


-+–
Sample Output

6
1 1
1 3
1 4
4 1
4 3
4 4

代码:

#include<stdio.h>#include<string.h>#include<queue>using namespace std;char a[8][8];bool c[100000];int  multi[16]= {4383, 8751, 17487,34959,                 4593, 8946,17652, 35064,                 7953,12066,20292,36744,                 61713,61986, 62532,63624                },k=0;             //手动打表struct stu{    int x,step,num;} s;struct st{    int x,pre;} d[100001];void present(int num){    if(num)    {        present(d[num].pre);        printf("%d %d\n",(d[num].x)/4+1,(d[num].x)%4+1);    }}bool mach(int i,stu t){    s.x=t.x;    s.x=s.x^multi[i];    if(!c[s.x])    {        s.num=++k;        d[k].x=i;        d[k].pre=t.num;        s.step=t.step+1;        c[s.x]=1;        return 1;    }    return 0;}void bfs(){    queue<stu>q;    q.push(s);    stu t;    int i,j;    while(!q.empty())    {        t=q.front();        q.pop();        if(!t.x)        {            printf("%d\n",t.step);            present(t.num);        }//路径回溯        else        {            for(i=0; i<16; i++)            {                if(mach(i,t))                    q.push(s);            }        }    }}int main(){    int i,j;    while(scanf("%s",a[1]+1)!=EOF)    {        memset(c,0,sizeof(c));        s.x=0,s.step=0,k=0,s.num=0;        for(i=2; i<=4; i++)            scanf("%s",a[i]+1);        for(i=1; i<=4; i++)            for(j=1; j<=4; j++)                if(a[i][j]=='+')                    s.x^=(1<<(j-1+(i-1)*4));   //状态压缩        if(s.x)            c[s.x]=1;        bfs();    }}
0 0
原创粉丝点击