首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【POJ】1753 - Flip Game(bfs,枚举)

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

作者头像
FishWang
发布2025-08-26 19:45:42
发布2025-08-26 19:45:42
7900
代码可运行
举报
运行总次数:0
代码可运行

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).

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

代码语言:javascript
代码运行次数:0
运行
复制
bwwb
bbwb
bwwb
bwww

Sample Output

代码语言:javascript
代码运行次数:0
运行
复制
4

Source

Northeastern Europe 2000

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

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档