| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 20263 | Accepted: 8783 |
Description
- 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
Output
Sample Input
bwwb bbwb bwwb bwww
Sample Output
4
代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int flag=0;
int map[6][6];
int dx[5]={-1,0,0,0,1};
int dy[5]={0,-1,0,1,0};
int step;
int judge() //判断是否为同一种颜色;
{
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
if(map[i][j]!=map[1][1])
return 0;
}
}
return 1;
}
void flip(int x,int y)
{
int a,b;
for(int i=0;i<5;i++)
{
a=x+dx[i];
b=y+dy[i];
map[a][b]=!map[a][b];
}
}
void DFS(int x,int y,int n) //点(x,y)是否为现在要操作的点;
{
if(n==step)
{
flag=judge();
return;
}
if(flag||x==5)
return;
flip(x,y);//翻转;
if(y<4)
DFS(x,y+1,n+1);
else
DFS(x+1,1,n+1);
flip(x,y);//还原状态,不翻转;
if(y<4)
DFS(x,y+1,n);
else
DFS(x+1,1,n);
}
int main()
{
char c[8][8];
for(int i=1;i<=4;i++)
{
scanf("%s",c[i]+1);
for(int j=1;j<=4;j++)
{
if(c[i][j]=='b')
map[i][j]=1;
else
map[i][j]=0;
}
}
for(step=0;step<=16;step++) //枚举0~16步;
{
DFS(1,1,0);
if(flag)
break;
}
if(flag)
printf("%d\n",step);
else
printf("Impossible\n");
//system("pause");
return 0;
}