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

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

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 52748   Accepted: 22168

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

Northeastern Europe 2000

題目大意:給出一個4*4的棋盤,棋盤上有兩種顏色的棋子,黑色和白色,現在有一種操作:當你選擇一個棋子,那麼他的上下左右的棋子就會改變他的顏色為其原本對立的顏色。

問經過最短几步後棋盤中的棋子能變為同一顏色。

思路:思路參考自https://blog.csdn.net/u013480600/article/details/25505697感謝大佬!!

因為棋盤4*4,所以我們直接把整個棋盤的16個棋子的狀態作為程式中BFS的一個狀態即可。我們把棋子從第一行到最後一行每個b看成一個二進位制的1,所以最終總共有1<<16個不同的狀態。

       令vis[1<<16]來判斷當前某個狀態是否已經出現(其實vis陣列可以不需要),令dist[1<<16]來記錄從原始狀態到當前某個狀態的最小步數。

        當要翻轉(i,j)格子的時候,那麼對應16位二進位制數中的i*4+j位二進位制位.(想想是不是).且(i,j)周圍的4個對應格子也會相應的翻轉,不過要注意(i,j)周圍的4個格子不一定有位置.可能(i,j)是邊界.

程式碼:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
int vis[1<<16+1],dist[1<<16+1];
int dr[]={1,0,-1,0};
int dc[]={0,-1,0,1};

int BFS(int st)
{
    if(st==0||st+1==(1<<16)) return 0;
    queue<int> Q;
    vis[st]=1;
    dist[st]=0;
    Q.push(st);

    while(!Q.empty())
    {
        int s=Q.front();Q.pop();

        for(int i=0;i<16;i++)
        {
            int ns=s;
            int r=i/4,c=i%4;
            ns^=1<<i;

            for(int d=0;d<4;d++)
            {
                int nr=r+dr[d];
                int nc=c+dc[d];
                if(nr>=0&&nr<4&&nc>=0&&nc<4)
                    ns^=1<<(nr*4+nc);
            }
            if(ns==0 || ns+1==(1<<16)) return dist[s]+1;
            if(vis[ns]==0)
            {
                Q.push(ns);
                vis[ns]=1;
                dist[ns]=dist[s]+1;
            }
        }
    }
    return -1;
}

int main()
{
    char maze[5][5];
    for(int i=0;i<4;i++)
        scanf("%s",maze[i]);
    int st=0;
    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
            if(maze[i][j]=='b')
                st=st|(1<<(i*4+j));

    int ans=BFS(st);
    if(ans==-1) printf("Impossible");
    else printf("%d",ans);
    return 0;
}