牛客算法竞赛入门课第一节习题 Flip Game(状态压缩+枚举)

题目链接:https://ac.nowcoder.com/acm/problem/106350

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).
牛客算法竞赛入门课第一节习题 Flip Game(状态压缩+枚举)_第1张图片

链接:https://ac.nowcoder.com/acm/problem/106350
来源:牛客网

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.

输入描述:
The input consists of 4 lines with 4 characters “w” or “b” each that denote game field position.

输出描述:
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).
输入

bwwb
bbwb
bwwb
bwww

输出

4

翻译:
给定4 * 4的只有b和w组的。每次把可以把b按成w,或者把w按成b。并且其上下左右也会翻转。求4*4全部为b或w,翻转的最小次数。

分析:
模型转化:b和w可以看成1和0,对于每一个按不不按可以用1和0表示。枚举第一行的按的状态,则第二行按的情况由第一行来决定。依次类推,最后特判最后一行。

代码:

#include
#include
#include
#include
using namespace std;
int a[10],b[10];///把输入的b和w转化为1和0(1001),a[i]表示每一行的状态,b[i]表示的是a[i]相反的状态(0110)。把所有转化为b或w,a和b就可以表示转化为1种状态
int x[10],c[10];///x[i]枚举每一行按的状态,c[i]记录每一行的状态
int res;
int num[100];
int calcl(int x)
{
    int sum=0;
    while(x>0)
    {
        if(x&1)
            sum++;
        x>>=1;
    }
    return sum;
}
void deal(int a[])
{
    memset(c,0,sizeof(c));
    int sum=0;
    for(x[1]=0; x[1]<=(1<<4)-1; x[1]++)
    {
        sum=num[x[1]];
        c[1]=x[1]^a[1]^(x[1]>>1)^((x[1]<<1)&0xf);///&上1111防止溢出(0x表示16进制f=15,即1111)
        c[2]=x[1]^a[2];
        for(int i=2; i<=4; i++)
        {
            x[i]=c[i-1];///要保证最后全部为b或w,所以第i行的x[]等于i-1行的c[],c中的0或1表示w或b,而x中的0或1表示不按或按
            c[i]=x[i]^c[i]^(x[i]>>1)^((x[i]<<1)&0xf);
            c[i+1]=x[i]^a[i+1];
            sum+=num[x[i]];
        }
        if(c[4]==0)
            res=min(res,sum);
    }
}
int main()
{
    for(int i=0; i<=(1<<4)-1; i++)
        num[i]=calcl(i);///计算每一种状态需要翻转的次数
    for(int i=1; i<=4; i++)
    {
        for(int j=0; j<4; j++)
        {
            char mp;
            cin>>mp;
            if(mp=='b')
                a[i]|=(1<<j);
            else
                b[i]|=(1<<j);
        }
    }
    res=0x3f3f3f3f;
    deal(a);
    deal(b);
    if(res>16)
        cout<<"Impossible\n";
    else
        cout<<res<<endl;
    return 0;
}

你可能感兴趣的:(常用技巧)