POJ Flip Game (暴力枚举)

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;
}
扫描二维码关注公众号,回复: 9285966 查看本文章
发布了56 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/lovecyr/article/details/100514307