Flip Game POJ

来源:互联网 发布:怎样在淘宝联盟买东西 编辑:程序博客网 时间:2024/05/18 13:05
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: 
  1. Choose any one of the 16 pieces. 
  2. 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
bwwbbbwbbwwbbwww
Sample Output
4
题解:
可发现每个一个格子改变是0或1次就够了,然后只要最上面一排的改变次数确定了,那么整个图的改变次数也确定了(第一排确定之后其中某一个只能由其下面一个确定,依次类推,整个就出来了)。那么枚举第一排的状态0-15的二进制数表示,最后判断方案可行性。复杂度O(2*16*16)
#include"stdio.h"#include"string.h"#include"cstdio"#include"algorithm"#define INF 0x3f3f3f3fusing namespace std;char A[5][5]; int B[5][5];int X[4]={0,0,-1,1},Y[4]={1,-1,0,0};int ans;void BFS(int x,int y){ans++;B[x][y]+=1;for(int i=0;i<4;i++){int xn=X[i]+x,yn=Y[i]+y;if(xn>=0&&xn<4&&yn>=0&&yn<4)B[xn][yn]+=1;}}void flip(int x){int i=0;//printf("%d\n",x);while(x){if(x&1){BFS(0,i);//printf("1 ");}i++;x>>=1;}}int pd(int x,int y){if(A[x-1][y]=='b'&&B[x-1][y]%2==0||A[x-1][y]=='w'&&B[x-1][y]%2==1)return 1;else{BFS(x,y);return 1;}}int pd1(int x,int y){if(A[x-1][y]=='w'&&B[x-1][y]%2==0||A[x-1][y]=='b'&&B[x-1][y]%2==1)return 1;else{BFS(x,y);return 1;}}int pdpd(){for(int i=0;i<4;i++){if(A[3][i]=='w'&&B[3][i]%2==0||A[3][i]=='b'&&B[3][i]%2==1)return 0;}return 1;}int pdpd1(){for(int i=0;i<4;i++){if(A[3][i]=='b'&&B[3][i]%2==0||A[3][i]=='w'&&B[3][i]%2==1)return 0;}return 1;}int main(){int ret=INF;for(int i=0;i<4;i++){if(i>0)getchar();scanf("%s",A[i]);}for(int i=0;i<16;i++){ans=0;memset(B,0,sizeof(B));flip(i);for(int j=1;j<4;j++){for(int k=0;k<4;k++)pd(j,k);}//printf("%d\n",i);if(pdpd())ret=min(ret,ans);}for(int i=0;i<16;i++){ans=0;memset(B,0,sizeof(B));flip(i);for(int j=1;j<4;j++){for(int k=0;k<4;k++)pd1(j,k);}if(pdpd1())ret=min(ret,ans);}if(ret!=INF)printf("%d\n",ret);elseprintf("Impossible\n");}


原创粉丝点击