poj 1753 Flip Game

http://poj.org/problem?id=1753

题意:4*4的黑白棋子,给定棋子现在的状态怎样翻棋使棋子变成全黑or全白,但是每翻一次棋子,它的四周的都会改变成相反的棋子,是黑就会变白,是白就会变黑。

分析:一共最多有16步,所以可以枚举这16步,在每一种几步进行搜索,比如0步去搜索,1步去搜索可以不,2步去搜索可以不。。。。。

枚举+dfs

                                                                                      Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 31763   Accepted: 13822

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 GameConsider 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
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 int ans[6][6]={0};
 6 int r[]={1,0,-1,0};
 7 int c[]={0,1,0,-1};
 8 int step;
 9 bool flag=false;
10 bool judge()
11 {
12     int i,j,sum=0;
13      for(i=1;i<=4;i++)
14         for(j=1;j<=4;j++)
15             sum+=ans[i][j];
16       if(sum==0||sum==16)
17            return true;
18        else
19            return false;
20 }
21 void flip(int x,int y)
22 {
23        int i;
24       if(ans[x][y]==1)
25            ans[x][y]=0;
26       else
27          ans[x][y]=1;
28     for(i=0;i<=3;i++)
29      {
30       if(ans[x+r[i]][y+c[i]]==0)
31             ans[x+r[i]][y+c[i]]=1;
32        else
33          ans[x+r[i]][y+c[i]]=0;
34      }
35 }
36 void dfs(int x,int y,int d)
37 {
38      if(d==step)
39      {
40        flag=judge();
41            return ;
42      }
43 
44      if(flag||x>4)
45        {
46         return ;
47        }
48     flip(x,y);//翻棋
49      if(y<4)
50       dfs(x,y+1,d+1);
51      else
52        dfs(x+1,1,d+1);
53      flip(x,y);//翻回来.
54      if(y<4)
55       dfs(x,y+1,d);
56      else
57        dfs(x+1,1,d);
58      
59 }
60 int main()
61 {
62 
63        int i,j;
64       char temp;
65       for(i=1;i<=4;i++)
66       {
67         for(j=1;j<=4;j++)
68              {
69                  scanf("%c",&temp);
70                  if(temp=='b')
71                       ans[i][j]=1;
72              }
73              getchar();
74        }
75      for(step=0;step<=16;step++)//枚举16种步。
76        {
77 
78           dfs(1,1,0);
79            if(flag)
80               break;
81         }
82       if(flag)
83          printf("%d\n",step);
84       else
85          printf("Impossible\n");
86      return 0;
87 }

 

你可能感兴趣的:(game)