POJ 1753 Flip Game 暴力 深搜

Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 59468   Accepted: 24750

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).

POJ 1753 Flip Game 暴力 深搜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

Source

Northeastern Europe 2000    
  1 #include <iostream>
  2 #include <cstdio>
  3 
  4 using namespace std;
  5 
  6 char str[6];
  7 int a[6][6];
  8 int ans=17;
  9 
 10 void flip(int x,int y)
 11 {
 12     a[x][y]=!a[x][y];
 13     a[x-1][y]=!a[x-1][y];
 14     a[x+1][y]=!a[x+1][y];
 15     a[x][y-1]=!a[x][y-1];
 16     a[x][y+1]=!a[x][y+1];
 17 }
 18 
 19 bool same_color(void)
 20 {
 21     for(int i=1;i<=4;++i)
 22     {
 23         for(int j=1;j<=4;++j)
 24         {
 25             if(a[i][j]!=a[1][1])
 26             {
 27                 return false;
 28             }
 29         }
 30     }
 31     return true;
 32 }
 33 
 34 void dfs(int x,int y,int t)
 35 {
 36     // 判断是否同色,同色满足,返回
 37     if(same_color())
 38     {
 39         if(ans>t)
 40         {
 41             ans=t;
 42         }
 43         return;
 44     }
 45     // 深搜截止条件
 46     if(x>4)
 47     {
 48         return;
 49     }
 50 
 51     if(y==4)
 52     {
 53         // 当前不反转
 54         dfs(x+1,1,t);
 55         // 当前反转
 56         flip(x,y);
 57         dfs(x+1,1,t+1);
 58         // 状态还原
 59         flip(x,y);
 60     }
 61     else
 62     {
 63         dfs(x,y+1,t);
 64         flip(x,y);
 65         dfs(x,y+1,t+1);
 66         flip(x,y);
 67     }
 68 
 69 }
 70 
 71 int main()
 72 {
 73     for(int i=1;i<=4;++i)
 74     {
 75         scanf("%s",str+1);
 76         for(int j=1;j<=4;++j)
 77         {
 78             // a->0,b->1
 79             if(str[j]=='w')
 80             {
 81                 a[i][j]=0;
 82             }
 83             else
 84             {
 85                 a[i][j]=1;
 86             }
 87             //a[i][j]=str[j]-'a';
 88             //printf("%d ",a[i][j]);
 89         }
 90         //printf("\n");
 91     }
 92 
 93 
 94     dfs(1,1,0);
 95 
 96     if(ans==17)
 97     {
 98         printf("Impossible\n");
 99     }
100     else
101     {
102         printf("%d\n",ans);
103     }
104 
105 
106 
107     return 0;
108 }

 

上一篇:Linux 150命令之查看文件及内容处理命令 cat tac less head tail cut


下一篇:CF1208F Bits And Pieces