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: 

  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的矩阵,里面填充了黑(b)白(w)两种颜色,现在想要通过翻动一些小的格子,使矩阵只有一种颜色(黑的反面是白,白的反面是黑)。当翻动某个格子的时候,它四周的四个格子都会翻动,求最少的翻动次数。

思路:我们可以尝试翻动每一个格子,来寻找能达到目标状态的方案数,并找到最少的步数。因为要枚举每一个格子是翻还是不翻,我们需要对每个格子处理后的状态进行了解,通过画图,我们可以知道每个格子最多只能翻一次,因为翻第二次的时候就又回到了初态。但是翻动一次同时想要得到其他状态时就可以从它的四周某一个来翻,因此我们就枚举每一个点翻与不翻,知道最后一个点为止,因此最大步数是16,以此我们就可以进行一个剪枝。

代码如下:

#include<cstdio>
#include<cstring>
#define inf 0x3f3f3f3f
int minn,dir[5][2]={0,0,0,1,1,0,0,-1,-1,0};
char str[5][5];
bool check()   //检查整个矩阵是否只有一种颜色
{
    char c=str[0][0];
    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
        if(str[i][j]!=c)
        return 0;
    return 1;
}
void swap1(int x,int y)  //翻转这个点,及其四周的点
{
    for(int i=0;i<5;i++)
    {
        int xx=x+dir[i][0];
        int yy=y+dir[i][1];
        if(xx<0||xx>=4||yy<0||yy>=4)
            continue;
        if(str[xx][yy]=='w')
            str[xx][yy]='b';
        else if(str[xx][yy]=='b')
            str[xx][yy]='w';
    }
}
void dfs(int x,int y,int step)
{
    if(step>16)   //翻了16次  已经不能再求出了
        return;
    if(check())   //检查是否达到目标状态
    {
        if(step<minn)   //维护最小值
            minn=step;
        return;
    }
    if(y>=4)   //因为是按行走的,当走到行末时,就需要跳到下一行的第一个
    {
        x++;
        y=0;
    }
    if(x>=4)  //矩阵找完了
        return;
    for(int i=0;i<2;i++)  //每个点的两种状态
    {
        if(i==0)   //翻
        {
            swap1(x,y);  
            dfs(x,y+1,step+1);
            swap1(x,y);   //把翻过的点恢复
        }
        else    //不翻
            dfs(x,y+1,step);
    }
}
int main()
{
    minn=inf;
    for(int i=0;i<4;i++)
        scanf("%s",str[i]);
    dfs(0,0,0);    //从第一个点开始枚举
    if(minn<=16)    //有最小值
        printf("%d\n",minn);
    else
        printf("Impossible\n");
}

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值