1. 程式人生 > >POJ 1753 Flip Game(狀態壓縮+BFS)

POJ 1753 Flip Game(狀態壓縮+BFS)

-a alt sin 3rd 初始 搜索 == hose follow

題目網址:http://poj.org/problem?id=1753

題目:

Flip Game

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

思路:

棋盤只有4*4即16格,我們把這16格的白棋黑棋狀態分別用1,0表示,就可以用十進制的0——(2^15-1即65535)表示所有情況。利用0^1=1,1^1=0,0^0=0即無論0,1與0做^運算都等於其本身,無論0,1與1做^運算都等於另一個數的特性,我們將翻轉棋子操作,變成將選定棋子和四周的棋子的狀態^1,其余棋子狀態^0。再用bfs進行搜索,用vis數組標記當前棋盤情況是否出現過,未出現則將當前情況入隊,反之則不入。

技術分享


代碼:
 1 #include <cstdio>
 2 #include <vector>
 3 #include <queue>
 4 using namespace std;
 5 int vis[70000];
 6 int mp[16]={//預處理,事先算出翻轉16個棋子 分別對應的"^"操作數
 7                 51200,58368,29184,12544,
 8                 35968,20032,10016,4880,
 9                 2248,1252,626,305,
10                 140,78,39,19
11             };
12 char chess[5][5];
13 vector<int>v;
14 queue<int>q;
15 int change(){//將棋盤的初始狀態壓縮
16     int x=v[0];
17     for (int i=1; i<v.size(); i++) {
18         x=(x<<1)+v[i];
19     }
20     return x;
21 }
22 int bfs(int v){
23     vis[v]=1;
24     q.push(v);
25     while (!q.empty()) {
26         int x=q.front();q.pop();
27         if(x==0 || x== 65535)   return vis[x]-1;//vis數組保存的是當前步數+1
28         for (int i=0; i<16; i++) {//分別翻轉16個棋子
29             int xt=x^mp[i];
30             if(vis[xt]) continue;
31             vis[xt]=vis[x]+1;
32             q.push(xt);
33         }
34     }
35     return -1;
36 }
37 int main(){
38     for (int i=0; i<4; i++) {
39         gets(chess[i]);
40         for (int j=0; j<4; j++) {
41             if(chess[i][j]==b)    v.push_back(1);
42             else v.push_back(0);
43         }
44     }
45     int x=bfs(change());
46     if(x!=-1)   printf("%d\n",x);
47     else printf("Impossible\n");
48     return 0;
49 }

POJ 1753 Flip Game(狀態壓縮+BFS)