天天看点

POJ 1753 Flip Game BFS

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).
POJ 1753 Flip Game BFS

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

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.csdn.net/kdwycz