POJ-1753-Flip Game

原题:
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:
Choose any one of the 16 pieces.
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个棋子中翻转某一个棋子,但与此同时在这个棋子周围的四个棋子也都要翻转,问能否翻转到全白或者全黑。
若是能则输出最小的操作次数。
题解:
直接暴力枚举,我这里的暴力枚举就是直接枚举所有的方法,执行所有的方法,并比较最终结果。
更多细节见代码注释。
附上AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
char st[5][5],cp[5][5];
int vis;//vis代表黑色数量
int cnt;
int a[20];
int min(int a,int b)//返回较小值
{
    return a>b?b:a;
}
void cop()//复制st数组到cp数组里
{
    for(int i=1;i<=4;i++)
        for(int j=1;j<=4;j++)
            cp[i][j]=st[i][j];
}
void check()//检查数组里有几个黑色棋子
{
    for(int i=1;i<=4;i++)
        for(int j=1;j<=4;j++)
    {
        if(st[i][j]=='b')
            vis++;
    }
}
void change(int i,int j)//翻转第i行第j个棋子
{
    if(i<1||i>4||j<1||j>4)//超出棋盘范围则不翻转
        return ;
    if(cp[i][j]=='b')//黑变白
    {
        cp[i][j]='w';
        vis--;
    }
    else//白变黑
    {
        cp[i][j]='b';
        vis++;
    }
}
int main()
{
    for(int i=1;i<=4;i++)
        for(int j=1;j<=4;j++)
    {
        cin>>st[i][j];
    }
    vis=0;
    cnt=20;//因为对于每一个棋子最多翻转一次(翻转两次等于没有翻转),所以最多操作次数为16次
    cop();
    check();//初始化
    if(vis==0||vis==16){cout<<0<<endl;return 0;}
    for(a[1]=0;a[1]<=1;a[1]++)//枚举所有操作方法,0代表不操作,1代表翻转
    for(a[2]=0;a[2]<=1;a[2]++)
    for(a[3]=0;a[3]<=1;a[3]++)
    for(a[4]=0;a[4]<=1;a[4]++)
    for(a[5]=0;a[5]<=1;a[5]++)
    for(a[6]=0;a[6]<=1;a[6]++)
    for(a[7]=0;a[7]<=1;a[7]++)
    for(a[8]=0;a[8]<=1;a[8]++)
    for(a[9]=0;a[9]<=1;a[9]++)
    for(a[10]=0;a[10]<=1;a[10]++)
    for(a[11]=0;a[11]<=1;a[11]++)
    for(a[12]=0;a[12]<=1;a[12]++)
    for(a[13]=0;a[13]<=1;a[13]++)
    for(a[14]=0;a[14]<=1;a[14]++)
    for(a[15]=0;a[15]<=1;a[15]++)
    for(a[16]=0;a[16]<=1;a[16]++)
    {
        int ans=0;//记录当前方法的操作次数
        for(int i=1;i<=16;i++)
        {
            if(a[i]==1)//如果a【i】==1,则表示要反转此棋子及其周围四个棋子
            {
                ans++;
                int x=i/4+1;
                int y=i%4;
                if(y==0){y=4;x--;}//计算第i个棋子的坐标
                change(x,y);
                change(x+1,y);
                change(x-1,y);
                change(x,y-1);
                change(x,y+1);
            }
        }
        if(vis==0||vis==16)
            cnt=min(cnt,ans);//不断更新最小的操作次数
        cop();
        vis=0;
        check();//重置cp数组和vis(一定要重置,否则就是无限WA)
    }
    if(cnt==20)cout<<"Impossible"<<endl;//如果没有一种方法可以实现全黑或者全白,则输出impossible
    else cout<<cnt<<endl;
    return 0;
}

欢迎评论!

猜你喜欢

转载自blog.csdn.net/wjl_zyl_1314/article/details/83045995