poj 1753:Flip Game

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的方格,里面有黑白棋,要求通过某种翻转使其全黑或全白。

翻转时如果东西南北有棋,则东西南北的棋子也需要翻转。

表示看到此题时极其的崩溃啊,感觉很难很难的样子。像奥数似的。

但是,知道解题方案的我,实在是佩服各位大神的思路啊啊啊啊!


我们可以把黑白棋分别用1,0标记。例如此题给出的数据:1001|1101|1001|1000

于是我们可以很巧妙的用二进制知识,把1001110110011000看成一个二进制。

则全白时为0000|0000|0000|0000,全黑时为11111|11111|11111|1111

换成10进制全白为0,全黑为65536.

为了简化问题,我们可以考虑一个2×2的方格,例如10|01。我们的思路就是先一个一个数遍历看一次操作是否会出现全黑或全白.

for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
{
     翻转;
}  
如此操作后分别为01|11,01|00,00|10,11|10。

没有出现0000或1111啊。

于是我们再对01|11操作,进行翻转。如果没有出现全黑或全白,就对01|00操作...

那怎么就知道impossible呢?我们可以想到如果impossible的话,程序就会一直循环,因为一直没有得到想要的结果。所以,我们需要把每次的状态记下来,然后每次操作时把新状态加入队列,新数据运行后没有出现全黑或全白就从队列中移除。出现的话就把所需步骤的步数输出(可以用一个数组来记录,例如step[status],表示到达status需要的步骤),结束程序。这样,当队列为空时,说明impossible。

首先把4*4的字符数组转换为2进制数:

其中status表示当前的数,即二进制代表的数。

<span style="font-family:Comic Sans MS;"><strong> for(int i=0;i<4;i++)
    for(int j=0;j<4;j++)
    {
        cin>>word;
        status<<=1;
        if(word=='b')
        status+=1;
    }</strong></span>
重要的还有位操作这块儿,因为1^=0,,0^=1;

所以可以通过位操作来实现翻转。先实现列的翻转,要考虑i==0和i==3时的特殊情况。行翻转时也是。

AC代码:

<span style="font-family:Comic Sans MS;"><strong>#include <iostream>
#define max 65536
#include <string.h>
using namespace std;

int front=0,rear=0;
int que[max*2];
bool visited[max];
int step[max];
int status;
int tmp;
int main()
{
    char word;
    status=0;
    memset(visited,false,sizeof(visited));
    for(int i=0;i<4;i++)
    for(int j=0;j<4;j++)
    {
        cin>>word;
        status<<=1;
        if(word=='b')
        status+=1;
    }
    if(status==0||status==65535)
    {
        cout<<0<<endl;
        return 0;
    }
    que[rear++]=status;
    visited[status]=true;
    step[status]=0;
    while(front<rear)
    {
        tmp=que[front++];
        status=tmp;
        for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
        {
            tmp=status;
            if(i==0) tmp^=1<<(11-j);
            else if(i==3)tmp^=1<<(7-j);
            else
            {
                tmp^=1<<(11-4*i-j);
                tmp^=1<<(19-4*i-j);
            }
            if(j==0)tmp^=3<<(14-4*i);
            else if(j==3) tmp^=3<<(12-4*i);
            else
            {
                tmp^=7<<(14-4*i-j);
            }
            if(tmp==0||tmp==65535)
            {
                cout<<step[status]+1<<endl;
                return 0;
            }
            if(!visited[tmp])
            {
                que[rear++]=tmp;
                visited[tmp]=true;
                step[tmp]=step[status]+1;
            }
        }
    }
    cout<<"Impossible"<<endl;
    return 0;
}</strong></span>


以后做2*2,4*4,8*8,方格,并且方格只有两种状态的题,我们就可以用这种方法啦。:)

加油!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值