Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 25810 | Accepted: 11149 |
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).

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
这道题看网上很多报告都写了BFS的代码,但是,作为ACM新手,我还是用了一下枚举的方法,简单来说,开始的时候先直接判断当前是否为纯色,如果是,直接输出0,如果不是,进行从1到16循环,套用全组合模板求出16取i个数的所有可能,在每次全组合得到结果的位置带入翻转函数对棋盘进行翻转,并且判断棋盘翻转后是否变为纯色,如果是,跳出全组合函数,如果不是,继续循环,直到全组合所有可能都循环结束的时候依然无法满足纯色,跳出全组合函数返回-1,之后,如果全组合函数返回非-1的值,那么就输出当前循环的的i,并且结束程序,如果返回-1,那么继续循环,当程序循环到16时依然返回-1,那么就是无法满足,输出“Impossible”,程序结束,另外还要说,如果一个棋子被翻转超过两次,是没有意义的,所以其实最多也就循环到16次,而且每个点至多可以使用一次。
下面是AC的代码:
#include<cstdio> #include<iostream> #include<cmath> using namespace std; char a[4][4],b[4][4]; int x[16],y[16]; struct p { int x; int y; }pa[20]; int judge() { int i,j; char c; c=b[0][0]; for(i=0;i<4;i++) for(j=0;j<4;j++) if(b[i][j]!=c) return 0; return 1; } void change(int x,int y) { if(b[x][y]=='w') b[x][y]='b'; else b[x][y]='w'; if(b[x-1][y]=='w'&&x-1>=0) b[x-1][y]='b'; else if(x-1>=0) b[x-1][y]='w'; if(b[x+1][y]=='w'&&x+1<=3) b[x+1][y]='b'; else if(x+1<=3) b[x+1][y]='w'; if(b[x][y-1]=='w'&&y-1>=0) b[x][y-1]='b'; else if(y-1>=0) b[x][y-1]='w'; if(b[x][y+1]=='w'&&y+1<=3) b[x][y+1]='b'; else if(y+1<=3) b[x][y+1]='w'; } int createfab(int m,int n) //全组合 { int i,j,lcount,wa[20]; for(i=1;i<=n;i++) wa[i]=i; wa[n+1]=m+1; for(i=1;i<=n;i++) change(pa[wa[i]].x,pa[wa[i]].y); if(judge()==1) return n; for(i=0;i<4;i++) for(j=0;j<4;j++) b[i][j]=a[i][j]; lcount=1; while(wa[1]<m-n+1) { for(i=n;i>0;i--) { if(wa[i]<wa[i+1]-1) { wa[i]++; for(j=i;j<n;j++) wa[j+1]=wa[j]+1; for(i=1;i<=n;i++) change(pa[wa[i]].x,pa[wa[i]].y); if(judge()==1) return n; for(i=0;i<4;i++) for(j=0;j<4;j++) b[i][j]=a[i][j]; lcount++; break; } } } return -1; } int main() { int i,j,k; k=1; for(i=0;i<4;i++) for(j=0;j<4;j++) { pa[k].x=i; pa[k].y=j; k++; } for(i=0;i<4;i++) { scanf("%s",a[i]); } for(i=0;i<4;i++) for(j=0;j<4;j++) b[i][j]=a[i][j]; if(judge()==1) { printf("0\n"); return 0; } for(i=1;i<=16;i++) { if(createfab(16,i)!=-1) { printf("%d\n",i); return 0; } } printf("Impossible\n"); return 0; }