POJ - 1753 Flip Game 模拟
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).
wwww
bwwb
bwww
wwwb
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
//Flip Game
#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f3f
int main()
{
char str[6][6];
char copy[6][6];
int m,n;
//while(scanf("%s",str[1]+1)!=EOF)
//{
int i,j,k,count=0,min;
int a,b,c,d;
for(i=1;i<5;i++){
scanf("%s",str[i]+1);
}
min=INF;
for(a=0;a<2;a++){
for(b=0;b<2;b++){
for(c=0;c<2;c++)
for(d=0;d<2;d++){
for(i=0;i<6;i++){
for(j=0;j<6;j++){
copy[i][j]=str[i][j];
}
}
// if(a==0 && b==1 && c==0 && d==0)printf("ddd");
if(a==1){
if(copy[1][1]=='w') copy[1][1]='b';
else copy[1][1]='w';
if(copy[1][2]=='w') copy[1][2]='b';
else copy[1][2]='w';
if(copy[2][1]=='w') copy[2][1]='b';
else copy[2][1]='w';
}
if(b==1){
if(copy[1][2]=='w') copy[1][2]='b';
else copy[1][2]='w';
if(copy[1][1]=='w') copy[1][1]='b';
else copy[1][1]='w';
if(copy[1][3]=='w') copy[1][3]='b';
else copy[1][3]='w';
if(copy[2][2] =='w') copy[2][2]='b';
else copy[2][2]='w';
}
if(c==1){
if(copy[1][3]=='w') copy[1][3]='b';
else copy[1][3]='w';
if(copy[1][2]=='w') copy[1][2]='b';
else copy[1][2]='w';
if(copy[1][4]=='w') copy[1][4]='b';
else copy[1][4]='w';
if(copy[2][3]=='w') copy[2][3]='b';
else copy[2][3]='w';
}
if(d==1){
if(copy[1][4]=='w') copy[1][4]='b';
else copy[1][4]='w';
if(copy[1][3]=='w') copy[1][3]='b';
else copy[1][3]='w';
if(copy[2][4]=='w') copy[2][4]='b';
else copy[2][4]='w';
}
for(i=2;i<5;i++){
for(j=1;j<5;j++){
if( copy[i-1][j]=='w' ){ //置成黑色
copy[i-1][j]='b';
if(copy[i][j]=='w') copy[i][j]='b';
else copy[i][j]='w';
if(copy[i][j-1]=='w') copy[i][j-1]='b';
else copy[i][j-1]='w';
if(copy[i][j+1]=='w') copy[i][j+1]='b';
else copy[i][j+1]='w';
if(copy[i+1][j]=='w') copy[i+1][j]='b';
else copy[i+1][j]='w';
count++;
//printf("******\n");
}
}
}
if(copy[4][1]=='b' && copy[4][2]=='b' && copy[4][3]=='b' && copy[4][4]=='b'){
count+=a+b+c+d;
if(min>count) min=count;
}
//if(count==2){
// printf("**%d %d %d %d\n",a,b,c,d);
//}
count=0;
for(i=0;i<6;i++){
for(j=0;j<6;j++){
copy[i][j]=str[i][j];
}
}
if(a==1){
if(copy[1][1]=='w') copy[1][1]='b';
else copy[1][1]='w';
if(copy[1][2]=='w') copy[1][2]='b';
else copy[1][2]='w';
if(copy[2][1]=='w') copy[2][1]='b';
else copy[2][1]='w';
}
if(b==1){
if(copy[1][2]=='w') copy[1][2]='b';
else copy[1][2]='w';
if(copy[1][1]=='w') copy[1][1]='b';
else copy[1][1]='w';
if(copy[1][3]=='w') copy[1][3]='b';
else copy[1][3]='w';
if(copy[2][2] =='w') copy[2][2]='b';
else copy[2][2]='w';
}
if(c==1){
if(copy[1][3]=='w') copy[1][3]='b';
else copy[1][3]='w';
if(copy[1][2]=='w') copy[1][2]='b';
else copy[1][2]='w';
if(copy[1][4]=='w') copy[1][4]='b';
else copy[1][4]='w';
if(copy[2][3]=='w') copy[2][3]='b';
else copy[2][3]='w';
}
if(d==1){
if(copy[1][4]=='w') copy[1][4]='b';
else copy[1][4]='w';
if(copy[1][3]=='w') copy[1][3]='b';
else copy[1][3]='w';
if(copy[2][4]=='w') copy[2][4]='b';
else copy[2][4]='w';
}
for(i=2;i<5;i++){
for(j=1;j<5;j++){
if( copy[i-1][j]=='b' ){ //置成白色
copy[i-1][j]='w';
if(copy[i][j]=='w') copy[i][j]='b';
else copy[i][j]='w';
if(copy[i][j-1]=='w') copy[i][j-1]='b';
else copy[i][j-1]='w';
if(copy[i][j+1]=='w') copy[i][j+1]='b';
else copy[i][j+1]='w';
if(copy[i+1][j]=='w') copy[i+1][j]='b';
else copy[i+1][j]='w';
count++;
//printf("******\n");
}
}
}
if(copy[4][1]=='w' && copy[4][2]=='w' && copy[4][3]=='w' && copy[4][4]=='w'){
count+=a+b+c+d;
if(min>count) min=count;
}
//if(count==2){
// printf("%d %d %d %d\n",a,b,c,d);
//}
count=0;
}
}
}
if(min==INF){
printf("Impossible\n");
}else printf("%d\n",min);
return 0;
}