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:
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).
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
Source
Northeastern Europe 2000
高消求解异或方程组,是典型的开关问题,要枚举自由变量。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int x[30];
int free_x[30];
int mat[30][30];
int Gauss(int equ,int var)
{
int num=0;
int col=0,max_r;
int i,j,k;
for(k=0;k<equ && col<var;k++,col++)
{
max_r=k;
for(i=k+1;i<equ;i++)
if(abs(mat[max_r][col]) < abs(mat[i][col]))
max_r=i;
if(max_r != k)
{
for(i=col;i<var+1;i++)
swap(mat[max_r][i],mat[k][i]);
}
if(mat[k][col] == 0)
{
k--;
free_x[num++]=col;
continue;
}
for(i=k+1;i<equ;i++)
{
if(mat[i][col])
for(j=col;j<var+1;j++)
mat[i][j]^=mat[k][j];
}
}
for(i=k;i<equ;i++)
if(mat[i][col])
return -1;
int all=1<<(var-k);
int cnt,res=0x3f3f3f3f;
for(i=0;i<all;i++)
{
int index=i;
cnt=0;
for(j=0;j<var-k;j++)
{
x[free_x[j]]=(index&1);
if(index&1)
cnt++;
index>>=1;
}
for(j=k-1;j>=0;j--)
{
int temp=mat[j][var];
for(int l=j+1;l<var;l++)
if(mat[j][l])
temp^=mat[j][l]*x[l];
x[j]=temp;
if(x[j])
cnt++;
}
if(res>cnt)
res=cnt;
}
return res;
}
void init()
{
memset(mat,0,sizeof(mat));
memset(x,0,sizeof(x));
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
int t=i*4+j;
mat[t][t]=1;
if(i>0)
mat[(i-1)*4+j][t]=1;
if(i<3)
mat[(i+1)*4+j][t]=1;
if(j>0)
mat[i*4+j-1][t]=1;
if(j<3)
mat[i*4+j+1][t]=1;
}
}
char str[20][20];
int main()
{
while(~scanf("%s%s%s%s",str[0],str[1],str[2],str[3]))
{
init();
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(str[i][j]=='w')
mat[i*4+j][16]=1;
}
}
int ans1=Gauss(16,16);
init();
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(str[i][j]=='b')
mat[i*4+j][16]=1;
}
}
int ans2=Gauss(16,16);
if(ans1 == -1 && ans2 == -1)
printf("Impossible\n");
else
printf("%d\n",min(ans1,ans2));
}
return 0;
}