Flip Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 37427 Accepted: 16288
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
bwww
Sample Output
4
Source
Northeastern Europe 2000
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char map[5][5];
int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};//翻转方向函数
bool judge()
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(map[i][j]!=map[0][0])
return false;
}
}
return true;
}
void flip(int n)//翻转函数
{
int x,y;
if(n%4==0)
{
x=n/4-1;
}
else{
x=n/4;
}
y=n-4*x-1;
map[x][y]=(map[x][y]== 'b' ? 'w' : 'b');
for(int i=0;i<4;i++)
{
int xx=x+dir[i][0];
int yy=y+dir[i][1];
if(xx>=0 && xx<4 && yy>=0 && yy<4)
{
map[xx][yy]=(map[xx][yy]=='b' ? 'w' : 'b');
}
}
}
bool dfs(int t,int s,int n)
/*参数说明:
t:表示规定最多能翻的次数
s:表示目前已经翻的次数
n:表示目前已经翻的位置
*/
{
if(n>16)/*如果整个图的范围都翻完了,
还没找到说明找不到*/
{
return false;
}
if(s==t)//翻转次数到了
{
if(judge())
{
return true;
}
return false;
}
flip(n+1);
//翻转n+1个
if(dfs(t,s+1,n+1))
{
return true;
}
flip(n+1);
//翻转回去
if(dfs(t,s,n+1))//跳过该点从下一个点开始翻转
{
return true;
}
return false;
}
int main()
{
while(scanf("%s",map[0])!=EOF)
{
for(int i=1;i<4;i++)
{
scanf("%s",map[i]);
}
bool flag=true;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
if(map[0][0]!=map[i][j])
{
flag=false;
break;
}
if(flag)
{
printf("0\n");
continue;
}
//判断这个图是不是已经翻转好了
//如果本身就是翻转好的
//不用翻转了,直接输出0,结束
flag=true;
for(int i=1;i<=16;i++)
{
if(dfs(i,0,0))//翻i次,从零开始翻,已经翻了零次
//如果翻i次正好能成功,则dfs为true。
{
printf("%d\n",i);
break;
}
if(i==16)
{
flag=false;
}
}
if(!flag)
printf("Impossible\n");
}
return 0;
}