您的位置:首页 > 其它

POJ 1753 Flip Game

2016-08-06 09:34 381 查看
[align=center]Flip Game[/align]
[align=center]Time Limit: 1000MS[/align] Memory Limit: 65536K
Total Submissions: 40291 Accepted: 17497
DescriptionFlip 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. Eachround 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: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:bwbwwwwwbbwbbwwbHere "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:bwbwbwwwwwwbwwwbThe 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.InputThe input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.OutputWrite 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 thegoal, then write the word "Impossible" (without quotes).Sample Input
bwwb
bbwb
bwwb
bwww
Sample Output
4
#include<stdio.h>char map[4][4];bool judge()//判断是否全部颜色相同{for(int i=0;i<4;i++)for(int j=0;j<4;j++)if(map[i][j]!=map[0][0])return  false;return true;}void change(int i)//翻格子后的变化{int r=i/4;   int c=i%4;  //转换横纵坐标map[r][c]=(map[r][c]=='b'?'w':'b');if(r>0) map[r-1][c]=(map[r-1][c]=='b'?'w':'b');if(r<3) map[r+1][c]=(map[r+1][c]=='b'?'w':'b');if(c>0) map[r][c-1]=(map[r][c-1]=='b'?'w':'b');if(c<3) map[r][c+1]=(map[r][c+1]=='b'?'w':'b');}bool DFS(int n,int num){if(n==0){if(judge())return true ;elsereturn false;}for(int i=num;i<16;i++){change(i);//上下左右变色if(DFS(n-1,i+1))//判断是否找到return true;change(i);//变色复原}return false;}int main(){int i;bool flag=false;for(i=0;i<4;i++){for(int j=0;j<4;j++)scanf("%c",&map[i][j]);getchar();}for(i=0;i<=16;i++){if(DFS(i,0))//判断是否找到{flag=true;break;}}if(flag)printf("%d\n",i);elseprintf("Impossible\n");return 0;}
Frighting!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj DFS