POJ1753

来源:互联网 发布:方维生鲜o2o源码 编辑:程序博客网 时间:2024/06/01 08:19

一道枚举题,刚开始想用位运算。。。。。想的很完美,但是发现实现有点困难,暴力。。。。。


Description

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

bwwbbbwbbwwbbwww

Sample Output

4


#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define inf 99999999char temp[5][5];int f[5][5];int ans=inf;void fan(int x,int y)//将xy位置和与xy位置的棋子全翻过来{    f[x][y]=!f[x][y];    if(x-1>=0)        f[x-1][y]=!f[x-1][y];    if(x+1<=3)        f[x+1][y]=!f[x+1][y];    if(y-1>=0)        f[x][y-1]=!f[x][y-1];    if(y+1<=3)        f[x][y+1]=!f[x][y+1];}int p()//判断是否已经全部都是一个颜色{    int x=f[0][0];    for(int i=0;i<4;i++)    {        for(int j=0;j<4;j++)        {            if(f[i][j]!=x)                return 0;        }    }    return 1;}int dfs(int x,int y,int t){    if(p())    {        if(ans>t)            ans=t;            return 0;    }    if(x>=4||y>=4)        return 0;    int xx=(x+1)%4;//从1-4    int yy=y+(x+1)/4;//表示第几行    dfs(xx,yy,t);//表示不拿当前这个位置的情况    fan(x,y);//把xy翻过来,然后表示拿当前这个位置的情况    dfs(xx,yy,t+1);    fan(x,y);//翻回来    return 0;}int main(){    memset(f,0,sizeof f);    memset(temp,0,sizeof temp);    //memset(vis,0,sizeof vis);    for(int i=0;i<4;i++)        scanf("%s",temp[i]);    for(int i=0;i<4;i++)    {        for(int j=0;j<4;j++)        {            if(temp[i][j]=='b')            {                f[i][j]=0;            }            else if(temp[i][j]=='w')                f[i][j]=1;        }    }    dfs(0,0,0);    if(ans==inf)    {        printf("Impossible\n");    }    else printf("%d\n",ans);}


0 0