解题报告:POJ 1753 Flip Game 简单深搜dfs

来源:互联网 发布:长空栈道 知乎 编辑:程序博客网 时间:2024/06/10 21:59

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

Source

Northeastern Europe 2000

类型:简单深搜DFS

 

题目大意:

小时候玩的一种棋,游戏规则是每一步选一个棋子,将这个棋子和所有和它相邻的棋子翻转。现在题目给你一个初始状态,问你最少多少步可以获得全黑或者全白的状态。

 

思路:

所有的情况就是翻转棋子数从0到16,然后DFS深搜选择翻转的棋子,最后判断是否到达结束状态,到达则输出并且结束,如果最后也没有达到这个状态,则输出Impossibel 。最近POJ网站好像出了点问题,一直登不上= =


AC代码:

#include<cstdio>#include<cstring>using namespace std;int Map[5][5];int A[4][2]={0,1,0,-1,1,0,-1,0};char str[10];int k;int check(){    int x=Map[0][0];    for(int i=0;i<4;i++)        for(int j=0;j<4;j++)            if(Map[i][j]!=x)                return 0;    return 1;}void oper(int a,int b){    Map[a][b]=!Map[a][b];    for(int i=0;i<4;i++)    {        int x=a+A[i][0],y=b+A[i][1];        if(x>=0&&x<4&&y>=0&&y<4)            Map[x][y]=!Map[x][y];    }}void dfs(int x,int y,int m,int n){    if(k)        return ;    if(m==n)    {        if(check())            k=1;    }    else    {        if(y==4)            y=0,x++;        if(x==4)            return ;        for(int i=x;i<4&&!k;i++)            for(int j=y;j<4&&!k;j++)            {                oper(i,j);                dfs(i,j+1,m+1,n);                oper(i,j);            }    }}int main(){    while(scanf("%s",str)==1)    {        k=0;        for(int j=0;j<4;j++)        {            if(str[j]=='w')                Map[0][j]=0;            else                Map[0][j]=1;        }        for(int i=1;i<=3;i++)        {            scanf("%s",str);            for(int j=0;j<4;j++)            {                if(str[j]=='w')                    Map[i][j]=0;                else                    Map[i][j]=1;            }        }        for(int i=0;i<=16;i++)        {            dfs(0,0,0,i);            if(k)            {                printf("%d\n",i);                break;            }        }        if(!k)            printf("Impossible\n");    }}



0 0