B - 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

题意:

给你一个矩阵,b代表黑棋,w代表白棋,让你求翻转多少次可以把棋盘上的所有棋子变成同色,即全是黑色或者全是白色,每次翻转一枚棋子,它的上下左右的棋子也会翻转,即黑色变成白色,白色变成黑色;

思路:

这个棋盘正好只有黑白以用二进两种颜色棋子,正好可以用1和0来代表棋子的颜色,然后翻转可以用位运算处理,可以方便处理;用16位二制来表示每一个位置的棋子的情况;

代码如下:

#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;

#define INF 0x1f1f1f1f

int d[5][2] = {0, 0, 0, 1, 1, 0, 0, -1, -1, 0};    //代表搜索的五个点,包括自己这个点;
int eff[16];       //记录搜索过的点;
int dp[1<<16|1];

const int st = (1<<16)-1;

bool check(int x, int y)     //判断是否越界;
{
    return x >= 0 && x < 4 && y >= 0 && y < 4;
}

void init()
{
    int tot = 0;
    for (int i = 0; i < 4; ++i)
    {
        for (int j = 0; j < 4; ++j)
        {
            int sta = 0;
            for (int k = 0; k < 5; ++k)//五个搜索点;
            {
                int x = i+d[k][0];
                int y = j+d[k][1];
                if (check(x, y))//判断;
                    sta |= (1<<(x*4+y));
            }
            eff[tot++]=sta;//标记搜过了;
        }
    }
}

void bfs()
{
    for (int i = 0; i <= st; ++i)
    {
        if (dp[i] == INF)
            continue;
        for (int j = 0; j < 16; ++j)
        {
            int sta = i^eff[j];
            dp[sta] = min(dp[sta], dp[i]+1);
        }
    }
}

int main()
{
    memset(dp, INF, sizeof(dp));
    dp[0] = 0;
    char c[5];
    int p = 0;
    for (int i = 0; i < 4; ++i)
    {
        scanf("%s",c);
        for (int j = 0; j < 4; ++j)
        {
            if (c[j] == 'w')
                p=p^(1<<(i*4+j));//把白棋标记为1,然后再把1移到白棋的位置,再和p亦或,就可以把白棋的位置变为1;
        }
    }
    init();
    bfs();
    if (dp[p] == INF && dp[p^st] == INF)
        printf("Impossible\n");
    else
        printf("%d\n", min(dp[p],dp[p^st]));
    return 0;
}

猜你喜欢

转载自blog.csdn.net/titi2018815/article/details/81223236