POJ Flip Game (暴力枚举)

本文介绍了一款名为FlipGame的游戏,玩家需要在一个4x4的棋盘上通过最少的步骤将所有棋子翻转至同一颜色。文章详细解析了游戏规则,并提供了一个使用暴力枚举算法的解决方案,通过16位二进制数来表示棋盘状态,最终找到达到目标状态所需的最少操作数。

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

Source

Northeastern Europe 2000

题目大意:

在一个4*4的棋盘上摆有16个棋子,这种棋子有两面:一面为黑一面为白,给定一个初始化的一个棋谱,需要你将它用最小的步骤数翻转成全为白或全为黑的棋谱。其中要求选中的棋子及其上下左右四个方向的棋子同时翻转。

解题思路:

使用暴力枚举,由于共有16枚棋子因此一共有2^{16}种方法,可以利用16位二进制来模拟这2^{16}种方法,其中16位2进制的每一位对应一个位置的1棋子,而1代表翻动该位置的棋子,0代表不翻动。

代码:

#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
using namespace std;
int square[6][6] = {0};  //棋盘布局 1代表黑 0代表白
int squareTemp[6][6] = {0};   //存储临时棋盘

void Inc(int b[],int n)   //模拟16位二进制数自加
{
    for(int i = 0;i<n;i++){
        if(b[i])
            b[i] = 0;
        else
        {
            b[i] = 1;
            break;
        }
    }
}

int judge(int sq[6][6])   //用于判断是否满足最终要求
{
    int sum = 0;
    for(int i = 1;i<=4;i++)
        for(int j = 1;j<=4;j++)
            sum+=sq[i][j];
    if(sum==0||sum==16)      //全为黑或白
        return 1;
    else return 0;
}

int main()
{
    int b[17] = {0};   //存储16位二进制数来代表4*4棋盘上的16个位置,用于暴力枚举
    int pw = (int)pow(2,16);

    for(int i = 1;i<=4;i++)
        for(int j = 1;j<=4;j++)
    {
        char ch;
        cin>>ch;
        if(ch=='b')square[i][j] = 1;
        else if(ch=='w')square[i][j] = 0;
    }
    int minstep = 17;   //记录最小步骤
    for(int i = 0;i<pw;i++)
    {
        int step = 0;
        for(int j = 1;j<=4;j++)
            for(int k = 1;k<=4;k++)
                squareTemp[j][k] = square[j][k];
        for(int j = 0;j<16;j++)     //遍历数值b
            if(b[j]==1)
            {
                int posx = j%4 + 1;
                int posy = j/4 + 1;
                squareTemp[posx][posy] = !squareTemp[posx][posy];
                squareTemp[posx+1][posy] = !squareTemp[posx+1][posy];
                squareTemp[posx-1][posy] = !squareTemp[posx-1][posy];
                squareTemp[posx][posy+1] = !squareTemp[posx][posy+1];
                squareTemp[posx][posy-1] = !squareTemp[posx][posy-1];
                step++;
            }
        if(judge(squareTemp))
            if(step<minstep)
                minstep = step;
		Inc(b, 16);
    }

    if(minstep==17)
        cout<<"Impossible"<<endl;
    else cout<<minstep<<endl;

    return 0;
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡小涛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值