POJ-1753-Flip-Game

POJ 1753 Flip Game

题目链接http://poj.org/problem?id=1753

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~((1<<16)-1)这个数据范围包含了所以有的翻转条件,即对于每个数来说当前位为1则翻转0则不翻转,所以每次这样模拟一下翻转,如果成功记录下最小的翻转次数。

代码:

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 123456789;
void _copy(bool s1[10][10],bool s2[10][10]){   //s2复制到s1;
    for (int i = 1;i <= 4;i++)
        for (int j = 1;j <= 4;j++)
            s1[i][j] = s2[i][j];
}

bool check(bool s[10][10]){
    bool temp = s[1][1];
    for (int i = 1;i <= 4;i++)
        for (int j = 1;j <= 4;j++)
            if (s[i][j] != temp)
            return 0;
    return 1;
}

void change(bool s[10][10],int bit){//x,y这边注意别写崩了。
    int x = (bit-1)/4+1;
    int y = bit - 4*(x-1);
    s[x][y] = !s[x][y];
    s[x-1][y] = !s[x-1][y];
    s[x][y-1] = !s[x][y-1];
    s[x+1][y] = !s[x+1][y];
    s[x][y+1] = !s[x][y+1];
}

void show(bool s[10][10]){
    for (int i = 1;i <= 4;i++){
        for (int j = 1;j <= 4;j++){
            cout<<s[i][j]<<" ";
        }
        cout<<endl;
    }
}

int main()
{
    char ss[10][10];
    bool s[10][10];
    bool cp[10][10];
    for (int i = 1;i <= 4;i++)
        scanf("%s",ss[i]+1);
    for (int i = 1;i <= 4;i++)
        for (int j = 1;j <= 4;j++){
            if (ss[i][j] == 'b')        //1表示黑色,0表示白色;
                s[i][j] = true;
            else s[i][j] = false;
        }
    _copy(cp,s);
    //show(s);
    int ans = maxn;
    for (int i = 0;i <= (1<<16)-1;i++){
        _copy(s,cp);
        int c = 0;
        int n = i;
        int bit = 0;
        while (n){
            bit++;
            if (n%2 == 0){
                n/=2;
                continue;
            }
            c++;
            n/=2;
            change(s,bit);
        }
        if (check(s))
            ans = min(ans,c);
    }
    if (ans == maxn)
        cout<<"Impossible"<<endl;
    else cout<<ans<<endl;
    return 0;
}
posted @ 2016-04-03 18:27  Thecoollight  阅读(146)  评论(0编辑  收藏  举报