CUC-SUMMER-2-J

J - Flip Game
POJ - 1753

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: Choose any one of the 16 pieces.
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).

CUC-SUMMER-2-J_第1张图片

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


题意:好题,翻棋子,每次翻一个棋子左右和上下相邻的棋子,黑白转换,求使所有棋子同样颜色,最少翻几次

解法:宽搜,类似CUC-SUMMER-2-E,对起始状态的分别对每一个棋子进行翻转,记录翻转后状态并存入队列,存之前要判断翻转后状态之前是否标记过,如果标记过证明一定有更少的翻转次数达到此状态,所以返回,直到棋子颜色一致,或能到达的所有状态都已经达到。存储棋子状态用二进制,因为只有黑白两种情况,与1抑或则翻转,与0抑或不变。

代码:

#include
#include
using namespace std;
typedef long long ll;
#define maxn 65535
struct node{
    int step;
    int visit;
};
node Node[maxn+1];
int start=0;
queue q;
int ans=-1;
bool check(int i,int j){
    if(i<0||i>3)
        return false;
    if(j<0||j>3)
        return false;
    return true;
}
int BFS()
{
    while(!q.empty()){
        node now;
        int temp=q.front();
        now=Node[temp];
        q.pop();
        if(temp==0||temp==maxn)
            return now.step;
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                int loc=i*4+j;
                int next=temp;
                next^=1<<(15-loc);
                if(check(i,j+1))
                    next^=1<<(15-(loc+1));
                if(check(i,j-1))
                    next^=1<<(15-(loc-1));
                if(check(i-1,j))
                    next^=1<<(15-(loc-4));
                if(check(i+1,j))
                    next^=1<<(15-(loc+4));
                if(Node[next].visit==0){
                    q.push(next);
                    Node[next].visit=1;
                    Node[next].step=now.step+1;
                }
            }
        }
    }
    return ans;
}
int main()
{
    for(int i=0;i<=maxn;i++){
        Node[i].step=0;
        Node[i].visit=0;
    }
    char c;
    for(int i=0;i<16;i++){
        cin>>c;
        start*=2;
        if(c=='b')
            start++;
    }
    Node[start].visit=1;
    q.push(start);
    ans=BFS();
    if(ans==-1)
        cout<<"Impossible"<

你可能感兴趣的:(CUC-SUMMER-2-J)