Flip Game POJ - 1753

问题:

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,你可以你可以选择任意一个棋子,把他的颜色变成和原来颜色相反的颜色,他周围的棋子也会跟着变换(上下左右),问最少多少次能把所有棋子的颜色变成相同的,如果能出输出最少的变换次数,否则输出“Impossible”。

思路:

就是去用深搜模拟这过程,遍历所有的棋子,当遍历到一个棋子时,你可以选择变换颜色,或者不变换颜色,当都变成同一颜色是,保留最少次数。

代码:

#define N 6
#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char k[N][N];
int book[N][N];
int bl,ma;
void dfs(int xy,int bnum,int step)
{
    if(bnum==16||bnum==0)//当都变成相同颜色
    {
        if(step<ma)//保留最少次数
            ma=step;
        return ;
    }
    if(xy>=16)return;
    dfs(xy+1,bnum,step);  //走到这个位置不变换颜色,
    //下面为变换颜色,我们去统计黑棋数量
    int x=xy/4;
    int y=xy%4;//在二维数组中的位置
    int bum=bnum;
    book[x][y]++;      //变换次数+1
    if(k[x][y]=='b'&&(book[x][y]%2)==0)bum++;//如果黑棋转变了偶数次,还是黑棋,黑棋数量就+1
    else if(k[x][y]=='w'&&(book[x][y]%2)==1)bum++;//白棋变换了奇数次,也是黑棋,+1
    else bum--;                             //否则就会变成白棋,黑棋-1
                                            //下面的也是相同
    //遍历周围
    if(x>=1)
    {
        book[x-1][y]++;
        if(k[x-1][y]=='b'&&(book[x-1][y]%2)==0)bum++;
        else if(k[x-1][y]=='w'&&(book[x-1][y]%2)==1)bum++;
        else bum--;
    }
    if(x<3)
    {
        book[x+1][y]++;
        if(k[x+1][y]=='b'&&(book[x+1][y]%2)==0)bum++;
        else if(k[x+1][y]=='w'&&(book[x+1][y]%2)==1)bum++;
        else bum--;
    }
    if(y>=1)
    {
        book[x][y-1]++;
        if(k[x][y-1]=='b'&&(book[x][y-1]%2)==0)bum++;
        else if(k[x][y-1]=='w'&&(book[x][y-1]%2)==1)bum++;
        else bum--;
    }
    if(y<3)
    {
        book[x][y+1]++;
        if(k[x][y+1]=='b'&&(book[x][y+1]%2)==0)bum++;
        else if(k[x][y+1]=='w'&&(book[x][y+1]%2)==1)bum++;
        else bum--;
    }
    dfs(xy+1,bum,step+1);
    //还原
    book[x][y]--;
    if(x>=1)book[x-1][y]--;
    if(x<3) book[x+1][y]--;
    if(y>=1)book[x][y-1]--;
    if(y<3) book[x][y+1]--;
}
int main()
{
    while(~scanf("%s",k[0]))
    {
        for(int i=1; i<4; i++)
            scanf("%s",k[i]);
        bl=0;
        ma=0x3f3f3f3f;
        for(int i=0; i<4; i++)
            for(int j=0; j<4; j++)
                if(k[i][j]=='b')
                    bl++;           //计算黑棋个数
        memset(book,0,sizeof(book));//保留变换次数
        dfs(0,bl,0);
        if(ma==0x3f3f3f3f)          //不能变成相同颜色
            printf("Impossible\n");
        else
            printf("%d\n",ma);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lxxdong/article/details/81217840
今日推荐