poj 1753 Flip Game
Flip Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 24957 | Accepted: 10755 |
Description
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:
Consider the following position as an example:
bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:
bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
- Choose any one of the 16 pieces.
- Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).
Consider the following position as an example:
bwbw
wwww
bbwb
bwwb
Here "b" denotes pieces lying their black side up and "w" denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:
bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
Input
The input consists of 4 lines with 4 characters "w" or
"b" each that denote game field position.
Output
Write to the output file a single integer number - the
minimum number of rounds needed to achieve the goal of the game from the given
position. If the goal is initially achieved, then write 0. If it's impossible to
achieve the goal, then write the word "Impossible" (without quotes).
Sample Input
bwwb bbwb bwwb bwww
Sample Output
4
Source
开始时直接枚举16种状态,用的bfs不过超时了,后来看了一下网上的思路,大多是bfs加位运算,不过表示不怎么懂位运算,
试着用dfs也没搞出来,看了一下别人的代码,表示很神奇的说
1 #include<stdio.h> 2 char map[4][4]; 3 int vis[4][4]; 4 int step,flag; 5 int judge() 6 { 7 int i,j; 8 for(i=0;i<4;i++) 9 for(j=0;j<4;j++) 10 if(vis[i][j]!=vis[0][0]) 11 return 0; 12 13 return 1; 14 } 15 void turn(int i,int j) 16 { 17 vis[i][j]=!vis[i][j]; 18 if(i>0) vis[i-1][j]=!vis[i-1][j]; 19 if(i<3) vis[i+1][j]=!vis[i+1][j]; 20 if(j>0) vis[i][j-1]=!vis[i][j-1]; 21 if(j<3) vis[i][j+1]=!vis[i][j+1]; 22 } 23 void dfs(int i,int j,int step1) 24 { 25 26 if(step==step1) 27 { 28 if(judge()) 29 flag=1; 30 31 return ; 32 } 33 if(flag ||i==4) return ; 34 turn(i,j); 35 if(j<3) 36 dfs(i,j+1,step1+1); 37 else 38 dfs(i+1,0,step1+1); 39 turn(i,j); 40 if(j<3) 41 dfs(i,j+1,step1); 42 else 43 dfs(i+1,0,step1); 44 return ; 45 } 46 int main() 47 { 48 int i,j; 49 for(i=0;i<4;i++) 50 scanf("%s",map[i]); 51 for(i=0;i<4;i++) 52 { 53 for(j=0;j<4;j++) 54 { 55 if(map[i][j]=='b') 56 vis[i][j]=1; 57 else 58 vis[i][j]=0; 59 60 } 61 62 } 63 flag=0; 64 for(step=0;step<=16;step++) 65 { 66 dfs(0,0,0); 67 if(flag) 68 break; 69 } 70 if(flag) 71 printf("%d\n",step); 72 else 73 printf("Impossible\n"); 74 return 0; 75 }