POJ 2965:The Pilots Brothers' refrigerator

来源:互联网 发布:windows同步时间出错 编辑:程序博客网 时间:2024/05/18 18:03

这个题目的解法与poj:1753差不多,只是我添加了一个链表来记录每次操作的步骤

我就直接贴代码了

#include <iostream>#include <queue>#include <list>using namespace std;#define SIZE 4#defineMAX65536int flip[]={63624,62532,61986,61713,36744,20292,12066,7953,35064,17652,8946,4593,34959,17487,8751,4383};queue<int> myqueue;struct{int i;int prior;}record[MAX];int main(){int answer[SIZE*SIZE];int step[MAX];bool flag[MAX];memset(step,0,MAX);memset(flag,false,MAX);int stat = 0;int nextstat=0;int i,j,tmp1;char c;for (i=0;i<SIZE*SIZE;++i){cin>>c;c=='+'?(stat<<=1)++:stat<<=1;}flag[stat]=0;step[stat]=0;record[stat].i=-1;myqueue.push(stat);while(!myqueue.empty()){stat=myqueue.front();myqueue.pop();for(i=0;i<SIZE*SIZE;++i){nextstat=stat^flip[i];if(flag[nextstat]==true)continue;flag[nextstat]=true;step[nextstat]=step[stat]+1;record[nextstat].prior=stat;record[nextstat].i=i;if (nextstat==0x0000){cout<<step[nextstat]<<endl;tmp1=nextstat;answer[0]=0;for (j=0;j<step[nextstat]-1;j++){tmp1=record[tmp1].prior;answer[j+1]=tmp1;}for (j=step[nextstat]-1;j>=0;j--){cout<<((record[answer[j]].i)/4)+1 <<"  "<<((record[answer[j]].i)%4)+1<<endl;}return 0;}myqueue.push(nextstat);}}return 0;}