POJ 1753 Flip Game BFS

这篇博客主要介绍了POJ 1753翻转游戏的解决思路,作者提到这是一道可以通过枚举所有情况解决的题目,虽然也有人尝试使用DFS。作者使用了BFS来求解,但初次尝试时出现了小错误。文章还提到了一些高级的位运算技巧,并分享了其他人的解决方案链接,供读者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Flip Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 27410 Accepted: 11902

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

bwwb
bbwb
bwwb
bwww

Sample Output

4

Source


POJ训练计划枚举第一题。。。话说这第一眼为什么能看的出是枚举 %>_<% 

一看是DFS模板题,而且每个棋子最多翻一回(翻两回等于没翻)。确实可以枚举所有情况的说。。。不是也有人写DFS么。。。

我果断队列开小WA了一次。

还有些高大上的位运算神马的。反正我2965还在WA,如果重写就试试位运算写法。


某人提供的“官方数据”(显然不科学啊啊啊,按照上面的数据,10^6的队列不可能不够用啊)

官方测试数据: 这些都过了,就不可能WA了。
bwbw
wwww
bbwb
bwwb
Impossible
bwwb
bbwb
bwwb
bwww
4
wwww
wwww
wwww
wwww
0
bbbb
bbbb
bbbb
bbbb
0
bbbb
bwbb
bbbb
bbbb
Impossible
bwbb
bwbb
bwbb
bbbb
Impossible
bwbb
wwwb
bwbb
bbbb
1
wwww
wwwb
wwbb
wwwb
1
wwww
wwww
wwwb
wwbb
1
wbwb
bwbw
wbwb
bwbw
Impossible
bbbb
bwwb
bwwb
bbbb
4
bwwb
wbbw
wbbw
bwwb
4
bbww
bbww
wwbb
wwbb
Impossible
bbwb
bbbw
wwbb
wwwb
Impossible
wwwb
wwbw
wbww
wwbw
Impossible
bbbb
wwww
wwbb
wbbb
Impossible
bwwb
wbwb
wbbb
wbbb
4
bwbb
bwbb
bwbw
bbbw
5
wbwb
bbbb
bbww
wbbb
6
bbwb
bbbb
wbwb
bbbb
5

代码:

#include <iostream>
using namespace std;
struct map{
	bool m[6][6][2];
	int x,step;
}queue[800010],tem;

int main()
{
	string s; int i,j,k,l;
	tem.x=tem.step=0;
	for (i=1;i<=4;++i){
		cin>>s;
		for (j=0;j<4;++j)
			if (s[j]=='b'){
				tem.x++;
				tem.m[i][j+1][1]=1;
				tem.m[i][j+1][0]=1;
			}
			else{
				tem.m[i][j+1][1]=0;
				tem.m[i][j+1][0]=1;
			} 
	}
	
	if (tem.x==16 || tem.x==0){
		cout<<'0'<<endl;
		return 0;
	}
	
	int so=0,fa=0;
	queue[0]=tem;
	while (fa<=so && so<=800001){
		for (i=1;i<=4;++i)
			for (j=1;j<=4;++j){
				if (tem.m[i][j][0]){
					tem.m[i][j][0]=0;
					tem.m[i][j][1]^=1;
					tem.m[i-1][j][1]^=1;
					tem.m[i+1][j][1]^=1;
					tem.m[i][j-1][1]^=1;
					tem.m[i][j+1][1]^=1; 
					tem.x=0; tem.step++;
					for (k=1;k<=4;++k)
						for (l=1;l<=4;++l)
							tem.x+=tem.m[k][l][1];
					if (tem.x==0 || tem.x==16){
						cout<<tem.step<<endl;
						return 0;
					}
					queue[++so]=tem;
					tem=queue[fa];
				}
			}
		tem=queue[++fa];
	}
	cout<<"Impossible\n";
	return 0;
}

kdwycz的网站:  http://kdwycz.com/

kdwyz的刷题空间:http://blog.youkuaiyun.com/kdwycz


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值