【POJ】1753 - Flip Game(bfs,枚举)

Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 37629   Accepted: 16368

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: 
  1. Choose any one of the 16 pieces. 
  2. 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).

【POJ】1753 - Flip Game(bfs,枚举)_第1张图片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



这道题要注意0步就符合条件的情况,其他的还是按照bfs来做就好啦。虽然这道题个人觉得有点吃力,还需要多练习啊!

代码如下:

#include <cstdio>
int map[6][6];		//分别表示行列 
int step;
bool flag = false;

bool check()		//检查地图是否达成条件 
{
	int k = map[1][1];
	int ans;
	for (int i=1;i<=4;i++)
	{
		for (int j=1;j<=4;j++)
		{
			if (k != map[i][j])
				return false;
		}
	}
	return true;
}

void op(int x,int y)		//翻棋 
{
	map[x][y] = !map[x][y];
	map[x+1][y] = !map[x+1][y];
	map[x-1][y] = !map[x-1][y];
	map[x][y+1] = !map[x][y+1];
	map[x][y-1] = !map[x][y-1];
	return;
}

void bfs(int x,int y,int n)
{
	if (n == step)
	{
//		if (check())
//		{
//			flag = true;
//		}
//		return;
		flag = check();
		return;		//这样写比上面的更简单些 
	}
	if (x >= 5 || flag)		//超过第四行也没法继续翻棋 
		return;
	
	op(x,y);		//开始下一轮翻棋 
	if (y < 4)
		bfs (x,y+1,n+1);
	else
		bfs (x+1,1,n+1);
		
	op(x,y);		//不成就撤回上一个步骤 ,然后对下一个搜索 
	if (y < 4)
		bfs (x,y+1,n);		//这里要注意,这一步不应该是 n+1 
	else
		bfs (x+1,1,n);
}
int main()
{
	char t;		//输入地图 
	for (int i=1;i<=4;i++)
	{
		for (int j=1;j<=4;j++)
		{
			scanf ("%c",&t);
			if (t == 'b')
				map[i][j] = 1;
			else
				map[i][j] = 0;
		}
		getchar();
	}
	for (step=0;step<=16;step++)
	{
		bfs(1,1,0);
		if (flag)
			break;
	}
	if (flag)
		printf ("%d\n",step);
	else
		printf ("Impossible\n");
	return 0;
}


你可能感兴趣的:(【POJ】1753 - Flip Game(bfs,枚举))