提交时间:2024-01-23 10:27:41

运行 ID: 123948

#include<stdio.h> #include<math.h> #include<string.h> using namespace std; const int N=7e4; typedef struct { int zt; int flips; }state; state q[N]; int head=0,tail=-1; int flip[17]={19,39,78,140,305,626,1252,2248,4880,10016,20032,35968,12544,29184,58368,51200}; int fliped[N]; char map[4][4]; void enq(state s) { ++tail; q[tail].zt=s.zt; q[tail].flips=s.flips; fliped[q[tail].zt] = 1; } void deq() { ++head; } state gethead() { return q[head]; } int isempty() { if(head>tail) return 1; else return 0; } int bfs() { int i; state temp,temp1; memset(fliped,0,sizeof(fliped)); enq(q[0]); while(!isempty()) { temp=gethead(); deq(); if(temp.zt==0 || temp.zt == 65535) return temp.flips; else { for(i=0;i<16;i++) { temp1.zt =temp.zt ^ flip[i]; if(!fliped[temp1.zt]) { temp1.flips=temp.flips+1; enq(temp1); } } } } return -1; } int main() { int i,j,minflip; int Inizt=0; for(i=0;i<4;i++) scanf("%s",map[i]); for(i=0;i<4;i++) for(j=0;j<4;j++) { if(map[i][j] - 'w' == 0) Inizt+=(int)pow(2,(double)(i*4+j)); } q[0].zt=Inizt; q[0].flips=0; minflip=bfs(); if(minflip == -1) printf("Impossible\n"); else printf("%d\n",minflip); return 0; }