【状压BFS】——poj1753——Flip Game

                                                 Flip Game

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 39684   Accepted: 17233

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颗中的一颗,把选中的这颗以及它四周的棋子一并反过来,当所有的棋子都是同一个颜色朝上时,游戏就完成了。

现在给定一个初始状态,要求输出能够完成游戏所需翻转的最小次数,

如果初始状态已经达到要求输出0。如果不可能完成游戏,输出Impossible。

思路:

1、如果用一个4*4的数组存储每一种状态,不但存储空间很大,而且在穷举状态时也不方便记录。

    因为每一颗棋子都只有两种状态,所以可以用二进制0和1表示每一个棋子的状态,则棋盘的状态就可以用一个16位的整数唯一标识。

    而翻转的操作也可以通过通过位操作来完成。显然当棋盘状态id为0(全白)或65535(全黑)时,游戏结束。

2、对于棋盘的每一个状态,都有十六种操作,首先要判断这十六种操作之后是否有完成的情况,

    如果没有,则再对这十六种操作的结果分别再进行上述操作,显然这里就要用到队列来存储了。

    而且在翻转的过程中有可能会回到之前的某种状态,而这种重复的状态是不应该再次入队的,

    所以维护 Visit[i]数组来判断 id==i 的状态之前是否已经出现过,如果不是才将其入队。

    如果游戏无法完成,状态必定会形成循环,由于重复状态不会再次入队,所以最后的队列一定会是空队列。

3、由于0^1=1,1^1=0,0^0=0

     所以翻转的操作可以通过异或操作来完成,而翻转的位置可以通过移位来确定。

 

分析:

根据输入要求,b代表黑棋(black),w代表白棋(white)。

因为总共才16个位置,且只有黑白两种表示,

此时,可对每一次状态进行二进制压缩(其中b代表1,w代表0),

例如: 

bwwb  

bbwb  

bwwb  

bwww 

即可表示为1001 1101 1001 1000,其十进制值为40344。

同时,计算可知根据黑白棋的摆放情况,总共有2^16种不同的状态。(每个位置最多翻转一次,因为翻两次后就又变回了之前的状态

每一次经过有效的操作后,状态都会发生改变,

此时,可借助二进制位运运算实现状态的改变,即对原有状态(相应的十进制表示)进行异或操作,以此来改变其对应二进制数的相关位置的值(1变0,0变1)。 

例如: 

先假设前一个状态为:

wwww  

wwww  

wwww  

wwww 

即二进制表示为0000 0000 0000 0000,十进制对应为0。

若此时选定左上角第一个棋子进行操作,根据规则,它右边和下边的也要同时进行变换(因为其左边和上边为空,不做考虑),

之后,相应的状态用二进制表示,

应变为:1100 1000 0000 0000,十进制值为51200。

这个过程相当于对十进制数51200进行对十进制数0的异或操作,即next=0^(51200),

而51200这个数则可以根据对十进制数1进行相应的左移操作得到。

同时,我们知道,棋牌总共有16个位置,也就是说相应的不同的操作也有16种,即有16个不同的数经过异或操作用来改变前一个状态的值。

那么,就先将这16个数枚出来

 

代码如下: 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

int t[20]=
{
    51200,58368,29184,12544,
    35968,20032,10016,4880  ,
    2248  ,1252 ,626    ,305    ,
    140    ,78    ,39      ,19      ,
};
//当前状态^变化状态
//0^1=1
//1^1=0
//0^0=0

int M=1<<16;
bool vist[(1<<16)];

struct node
{
    int k;//当前状态
    int step;
};

void bfs(int ans)
{
    queue<node>q;
    node p;

    p.k=ans;
    p.step=0;
    q.push(p);

    while(!q.empty())
    {
        p=q.front();
        q.pop();

        if(p.k==0||p.k==(M-1))//结束状态
        {
            printf("%d\n",p.step);
            return;
        }

        for(int i=0;i<16;i++)
        {
            node p1;
            p1.step=p.step+1;
            p1.k=p.k^t[i];
            if(!vist[p1.k])
            {
                vist[p1.k]=true;
                q.push(p1);
            }
        }
    }

    printf("Impossible\n");
}

int main()
{
    int ans=0;
    int cnt=15;//二进制位计位器
    for(int i=0;i<4;i++)
    {
        char ch[10];
        scanf("%s",ch);

        for(int j=0;j<4;j++)
        {
            if(ch[j]=='w')
            {
                ans|=(1<<cnt);
            }
            cnt--;
        }
    }
    memset(vist,false,sizeof(vist));
    vist[ans]=true;

    bfs(ans);

    return 0;
}

 

 

题后感:

既然存在双向广搜

那么为什么不可以将其特殊化出一个逆单向广搜,从目标状态开始搜索

posted @ 2016-07-21 08:01  琥珀川||雨露晨曦  阅读(146)  评论(0)    收藏  举报