POJ1753:Flip Game(BFS、枚举、位运算)

Time Limit: 1000MS
Memory Limit: 65536K


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


解题分析

审题:全变成1或0,游戏结束,并不是像熄灯问题一样全变成0。

因为你不知道要变成全1还是全0,所以不能通过一行或一列确定整个棋盘的翻转状态。

从而着眼于整个棋盘,对每个棋盘状态,若只翻转一枚棋子,至多拓展出16种新状态,加之题目求最小翻转次数,想到广搜。

如何通过一枚棋子影响到周围棋子,即确定翻转后的状态?

有请位运算。

每个状态可以压缩到一个int里,例如:

bwbw
wwww
bbwb
bwwb

可以表示为:1010 0000 1101 1001
(怎么读入?左移一位读一位,最先读入的即在最高位),

翻转第三行第一列的b后,变为:

bwbw
bwww
wwwb
wwwb

即:             1010 1000 0001 0001,
需要异或:   0000 1000 1100 1000。

翻转棋子即异或一个这样改变状态的数。
16个棋子对应16个数:打表!
对每个棋子,把它自身和周围棋子对应的二进制位设为1即可。


AC代码

#include<iostream>
#include<cstring>
#include<queue>
const int MAXN=65535;
using namespace std;

/*int Direction[4][2]={{1,0},{-1,0},{0,-1},{0,1}};
int main()
{
    int tmp,x,y;
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            tmp=1<<((3-i)*4+(3-j));
            for(int k=0;k<4;k++){
                x=i+Direction[k][0];
                y=j+Direction[k][1];
                if(x>3||x<0||y>3||y<0)
                    continue;
                tmp^=1<<((3-x)*4+(3-y));
            }
            cout<<tmp<<' ';
        }
        cout<<endl;
    }
    return 0;
}
*/

int FlipMod[16]={
    51200,58368,29184,12544,
    35968,20032,10016,4880,
    2248,1252,626,305,
    140,78,39,19
};

struct ChessNode{
    int State;
    int Layers;
    ChessNode(int S,int L):State(S),Layers(L){}
};


int Visited[MAXN+5];

queue<ChessNode>q;

int BFS(int TmpState)
{
    memset(Visited,0,sizeof(Visited));
    q.push(ChessNode(TmpState,0));
    Visited[TmpState]=1;

    while(!q.empty()){
        ChessNode c=q.front();
        if(c.State==0||c.State==65535)
            return c.Layers;
        else{
            for(int i=0;i<16;i++){
                int NextState=c.State^FlipMod[i];
                if(!Visited[NextState]){
                    q.push(ChessNode(NextState,c.Layers+1));
                    Visited[NextState]=1;
                }
            }
            q.pop();
        }
    }

    return -1;
}

int main()
{
    char ChessBoard[4][4];
    for(int i=0;i<4;i++)
        cin>>ChessBoard[i];
    int NowState=0;
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            NowState<<=1;
            if(ChessBoard[i][j]=='b')
                NowState+=1;
        }
    }
    int Answers=BFS(NowState);
    if(Answers==-1)
        cout<<"Impossible"<<endl;
    else
        cout<<Answers<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Hrbust_cx/article/details/78153116