牛客算法競賽入門課第一節習題 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).
在這裏插入圖片描述

鏈接: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<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章