题目链接:http://poj.org/problem?id=1753
Time Limit: 1000MS Memory Limit: 65536K
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
港真,这题,DFS太暴力了,原本还以为有什么高级方法……就是16格,flip或者不flip,2^16情况全部测试一遍,找那个使得 judge() == 1(满足全棋盘同色),同时step最小。
1 #include<cstdio> 2 #include<iostream> 3 using namespace std; 4 int chess[4][4]; 5 int dx[4]={0,-1,0,+1}; 6 int dy[4]={+1,0,-1,0}; 7 int cnt=99999; 8 bool judge() //判断整个棋盘是否同色,是返回真,否返回假 9 { 10 bool now=chess[0][0]; 11 for(int i=0;i<4;i++) 12 { 13 for(int j=0;j<4;j++) 14 { 15 if(now != chess[i][j]) return false; 16 } 17 } 18 return true; 19 } 20 void flip(int x,int y) //翻转(x,y)棋子及其周围的四个棋子 21 { 22 chess[x][y]=!chess[x][y]; 23 for(int i=0;i<4;i++) 24 { 25 int next_x=x+dx[i]; 26 int next_y=y+dy[i]; 27 if(next_x >= 0 && next_x < 4 && next_y >= 0 && next_y < 4) 28 chess[next_x][next_y]=!chess[next_x][next_y]; 29 } 30 } 31 void dfs(int x,int y,int step) 32 { 33 if (judge()) 34 { 35 if (cnt > step) cnt=step; 36 return; 37 } 38 if (x >= 4 || y >= 4) return; 39 int next_x,next_y; 40 next_y=(y+1)%4; 41 next_x=(next_y > y)?x:x+1; 42 flip(x,y); 43 dfs(next_x,next_y,step+1); //翻当前棋子,搜索下一个 44 flip(x,y); 45 dfs(next_x,next_y,step); //不翻当前棋子,搜索下一个 46 return; 47 } 48 int main() 49 { 50 for(int i=0;i<4;i++) 51 { 52 for (int j=0;j<4;j++) 53 { 54 char temp; 55 scanf("%c",&temp); 56 if(temp == 'b') chess[i][j]=1; 57 else chess[i][j]=0; 58 } 59 scanf("%*c"); 60 } 61 dfs(0,0,0); 62 if(cnt == 99999) printf("Impossible "); 63 else printf("%d ",cnt); 64 return 0; 65 }