Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 29059 | Accepted: 12568 |
Descriptionios
Inputide
Outputthis
Sample Inputspa
bwwb bbwb bwwb bwww
Sample Outputcode
4
這個題也是用位運算搜索(枚舉)
0xc800,0xe400,0x7200,0x3100,
0x8c80,0x4e40,0x2720,0x1310,
0x08c8,0x04e4,0x0272,0x0131,
0x008c,0x004e,0x0027,0x0013,
用十六進制表示比較方便,這就是十六種狀態,能夠枚舉每種便可,用異或操做,能夠改變每種狀態。
1 /*====================================================================== 2 * Author : kevin 3 * Filename : FlipGame.cpp 4 * Creat time : 2014-05-12 20:25 5 * Description : 6 ========================================================================*/ 7 #include <iostream> 8 #include <algorithm> 9 #include <cstdio> 10 #include <cstring> 11 #include <queue> 12 #include <cmath> 13 #define M 6 14 #define Max 0xffff 15 using namespace std; 16 int cnt[Max+100]; 17 int dir[M][M]={ 18 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000, 19 0x0000,0xc800,0xe400,0x7200,0x3100,0x0000, 20 0x0000,0x8c80,0x4e40,0x2720,0x1310,0x0000, 21 0x0000,0x08c8,0x04e4,0x0272,0x0131,0x0000, 22 0x0000,0x008c,0x004e,0x0027,0x0013,0x0000, 23 0x0000,0x0000,0x0000,0x0000,0x0000,0x0000 24 }; 25 int BFS(int t) 26 { 27 queue<int>pq; 28 cnt[t] = 1; 29 pq.push(t); 30 while(!pq.empty()){ 31 int num = pq.front(); 32 pq.pop(); 33 for(int i = 1; i <= 4; i++){ 34 for(int j = 1; j <= 4; j++){ 35 int d = num^dir[i][j]; 36 if(d == 0x0000 || d == 0xffff){ 37 cnt[d] = cnt[num]; 38 return cnt[d]; 39 } 40 if(!cnt[d]){ 41 cnt[d] = cnt[num]+1; 42 pq.push(d); 43 } 44 } 45 } 46 } 47 return -1; 48 } 49 void Init() 50 { 51 char str[M]; 52 int number = 0,steps; 53 while(cin>>str){ 54 number = 0; 55 memset(cnt,0,sizeof(cnt)); 56 for(int j = 0; j < 4; j++){ 57 number = number<<1; 58 if(str[j] == 'w'){ 59 number = number|1; 60 } 61 } 62 for(int i = 0; i < 3; i++){ 63 cin>>str; 64 for(int j = 0; j < 4; j++){ 65 number = number<<1; 66 if(str[j] == 'w'){ 67 number = number|1; 68 } 69 } 70 } 71 if(number == 0x0000 || number == 0xffff){ 72 printf("0\n"); 73 continue; 74 } 75 else{ 76 steps = BFS(number); 77 } 78 if(steps == -1){ 79 printf("Impossible\n"); 80 } 81 else{ 82 printf("%d\n",steps); 83 } 84 } 85 } 86 int main(int argc,char *argv[]) 87 { 88 Init(); 89 return 0; 90 }