天天看點

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