Flip Game

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 28602   Accepted: 12385

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).

Flip Game_第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

Source

Northeastern Europe 2000
本题分析参考了:在此鸣谢。
http://blog.csdn.net/luojinping/article/details/7066130
解题分析:
此题与poj上的
http://poj.org/problem?id=2965
相似
只是这道题的数据与方法有所不同,

1.如果用一个4*4的数组存储每一种状态,不但存储空间很大,而且在穷举状态时也不方便记录。因为每一颗棋子都只有两种状态,所以可以用二进制0和1表示每一个棋子的状态,则棋盘的状态就可以用一个16位的整数唯一标识。而翻转的操作也可以通过通过位操作来完成。显然当棋盘状态id为0(全白)或65535(全黑)时,游戏结束。

2.对于棋盘的每一个状态,都有十六种操作,首先要判断这十六种操作之后是否有完成的情况,如果没有,则再对这十六种操作的结果分别再进行上述操作,显然这里就要用到队列来存储了。而且在翻转的过程中有可能会回到之前的某种状态,而这种重复的状态是不应该再次入队的,所以维护isVis[i]数组来判断id==i的状态之前是否已经出现过,如果不是才将其入队。如果游戏无法完成,状态必定会形成循环,由于重复状态不会再次入队,所以最后的队列一定会是空队列。

3.由于0^1=1,1^1=0,所以翻转的操作可以通过异或操作来完成,而翻转的位置可以通过移位来确定。

此处也可以用压缩存储,此处先不提。
代码:
#include<iostream>
#define MAX 65536
using namespace std;
int que[MAX*2];
int front,rear;
bool isVis[MAX];    //记录状态是否已经出现过
int step[MAX];        //记录到达id==i的状态所需翻转次数
int main(){
    char color;
    int id=0,tmp;
    int i,j;
    for(i=0;i<4;i++){
        for(j=0;j<4;j++){     //输入初始状态并转换为id
            cin>>color;
            id<<=1;
            if(color=='b')id+=1;
        }
    }
    if(id==0||id==65535){cout<<0<<endl;return 0;}   //如果初始状态已经满足要求直接输出
    que[rear++]=id;      //将初始状态id入队
    isVis[id]=true;
    step[id]=0;            //到达初始状态所需次数为0
    while(front<rear){   //如果队列不为空,继续操作
        tmp=que[front++];   //将队头元素出队
        id=tmp;
        for(i=0;i<4;i++)
            for(j=0;j<4;j++){
                tmp=id;       //需要遍历队头元素的16种操作,每次都要还原tmp
                if(i==0)tmp^=1<<(11-4*i-j);          //翻转的位置为15-(4*(i+1)+j)
                else if(i==3)tmp^=1<<(19-4*i-j);   //翻转的位置为15-(4*(i-1)+j)
                else{
                    tmp^=1<<(11-4*i-j);
                    tmp^=1<<(19-4*i-j);
                }
                if(j==0)tmp^=3<<(14-4*i-j);          //翻转的位置为15-(4*i+j)、15-(4*i+j+1)
                else if(j==3)tmp^=3<<(15-4*i-j);  //翻转的位置为15-(4*i+j-1)、15-(4*i+j)
                else{
                    tmp^=7<<(14-4*i-j);                //翻转的位置为15-(4*i+j-1)、15-(4*i+j)、15-(4*i+j+1)
                }
                if(tmp==0||tmp==65535){
                    cout<<step[id]+1<<endl;
                    return 0;
                }
                if(!isVis[tmp]){      //如果是新的状态,入队
                    que[rear++]=tmp;
                    isVis[tmp]=true;
                    step[tmp]=step[id]+1;   //当前次数=到达队头元素所需次数+1
                }
            }
    }
    cout<<"Impossible"<<endl;
    return 0;
}

你可能感兴趣的:(Flip Game)