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: 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 bwwwSample Output
4Source
Northeastern Europe 2000
题意:有4*4的正方形,每个格子要么是黑色,要么是白色,当把一个格子的颜色改变(黑->白或者白->黑)时,其周围上下左右(如果存在的话)的格子的颜色也被反转,问至少反转几个格子可以使4*4的正方形变为纯白或者纯黑?
思路:这题可以位运算+BFS;但其实也可以用高斯消元法;
为了求出最小的步骤,由于可能无穷解,对于自由变元,我们就需要枚举;
对于自由变元;
例如方程式
x1+2*x2+5*x3=4;
0 + 0 + x3=1;
确定变元为x3,自由变元为x2,只有一个自由变元,x1随自由变元x2的取值确定;
代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int INF=0x3fffffff; int a[20][20],x[20]; char map[20][20]; int equ=16,var=16; void init() { memset(x,0,sizeof(x)); for(int i=0;i<4;i++) for(int j=0;j<4;j++) { int t=i*4+j; a[t][t]=1; if(i>0) a[(i-1)*4+j][t]=1; if(i<3) a[(i+1)*4+j][t]=1; if(j>0) a[i*4+j-1][t]=1; if(j<3) a[i*4+j+1][t]=1; } } int Gauss() { int k,col; int num=0,free_x[20]; memset(free_x,0,sizeof(free_x)); for(k=0,col=0;k<equ&&col<var;col++,k++) { int max_r=k; for(int i=k+1;i<equ;i++) if(abs(a[max_r][col])<abs(a[i][col])) max_r=i; if(max_r!=k) for(int j=col;j<=var;j++) swap(a[k][j],a[max_r][j]); if(a[k][col]==0) { k--; free_x[num++]=col; continue; } for(int i=k+1;i<equ;i++) { if(a[i][col]!=0) { for(int j=col;j<=var;j++) { a[i][j]^=a[k][j]; } } } } for(int i=k;i<equ;i++) if(a[i][var]!=0) return -1; int stat=1<<(var-k); int res=INF; for(int i=0;i<stat;i++) { int cnt=0; int index=i; for(int j=0;j<var-k;j++) //枚举自由变元; { x[free_x[j]]=(index&1); if(x[free_x[j]]) cnt++; index>>=1; } for(int j=k-1;j>=0;j--) { int t=0; while(a[j][t]==0) t++; int temp=a[j][var]; for(int l=t+1;l<var;l++) if(a[j][l]) temp^=x[l]; x[t]=temp; if(x[t]) cnt++; } if(res>cnt) res=cnt; } return res; } int main() { while(~scanf("%s",map[0])) { for(int i=1;i<=3;i++) scanf("%s",map[i]); memset(a,0,sizeof(a)); init(); for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(map[i][j]=='b') a[i*4+j][var]=1; else a[i*4+j][var]=0; int ans1=Gauss(); memset(a,0,sizeof(a)); init(); for(int i=0;i<4;i++) for(int j=0;j<4;j++) if(map[i][j]=='b') a[i*4+j][var]=0; else a[i*4+j][var]=1; int ans2=Gauss(); if(ans1==-1&&ans2==-1) printf("Impossible\n"); else if(ans1==-1&&ans2!=-1) printf("%d\n",ans2); else if(ans1!=-1&&ans2==-1) printf("%d\n",ans1); else printf("%d\n",min(ans1,ans2)); } }