poj 1753--Flip Game

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

bwwb
bbwb
bwwb
bwww

Sample Output

4
题目大意是给出4×4的一个正方形,里面有黑白两种棋子,现在要求翻面棋子,翻的同时周围一个单位的棋子也同样改变状态,问最少要翻多少次使得所以棋子是一样的状态。
在网上看到很多用位运算来表示每一位的状态的,方法很巧妙。一开始做的时候没有想到,用的最直接的搜索,时间也很垃圾,求不喷。贴一下最初的代码,位运算方法的网上有很多。

View Code
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <stack>
 6 using namespace std;
 7 int dx[]={-1,1,0,0,0};
 8 int dy[]={0,0,1,-1,0};
 9 int map[10][10],n,flag;
10 void filp(int row,int col)
11 {
12     int i,x,y;
13     for(i=0;i<5;i++)
14     {
15         x=row+dx[i];
16         y=col+dy[i];
17         map[x][y]=!map[x][y];
18     }
19 }
20 int judge()
21 {
22     int i,j;
23     for(i=0;i<4;i++)
24         for(j=0;j<4;j++)
25             if(map[i][j]!=map[0][0])
26                 return 0;
27     return 1;
28 }
29 void dfs(int row,int col,int cnt)
30 {
31     if(cnt==n)
32     {
33         flag=judge();
34         return ;
35     }
36     if(flag||row>=4)
37         return;
38     if(row<4)
39     {
40         if(col<3)
41         {
42             filp(row,col);
43             dfs(row,col+1,cnt+1);
44             if(flag) return;
45             filp(row,col);
46             dfs(row,col+1,cnt);
47                         if(flag) return;
48         }
49         else 
50         {
51             filp(row,col);
52             dfs(row+1,0,cnt+1);
53             if(flag) return;
54             filp(row,col);
55             dfs(row+1,0,cnt);
56                         if(flag) return;
57         }
58     }
59     else
60         return;
61 }
62 int main()
63 {
64     char s[5][5];
65     int i,j;
66     flag=0;
67     for(i=0;i<4;i++)
68     {
69         scanf("%s",s[i]);
70         for(j=0;j<4;j++)
71         {
72             if(s[i][j]=='w')
73                 map[i][j]=1;
74             else
75                 map[i][j]=0;
76         }
77     }
78     for(n=0;n<=16;n++)
79     {
80         dfs(0,0,0);
81         if(flag)
82             break;
83     }
84     if(n<=16)
85         printf("%d\n",n);
86     else
87         printf("Impossible\n");
88     return 0;
89 }

 

posted @ 2013-03-11 11:25  Tamara.c  阅读(202)  评论(0编辑  收藏  举报