poj 1753 -- Flip Game

Flip Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29059   Accepted: 12568

Descriptionios

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.

Inputide

The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.

Outputthis

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 Inputspa

bwwb
bbwb
bwwb
bwww

Sample Outputcode

4


這個題也是用位運算搜索(枚舉)

  0xc800,0xe400,0x7200,0x3100,
  0x8c80,0x4e40,0x2720,0x1310,
  0x08c8,0x04e4,0x0272,0x0131,
  0x008c,0x004e,0x0027,0x0013,

用十六進制表示比較方便,這就是十六種狀態,能夠枚舉每種便可,用異或操做,能夠改變每種狀態。


 1 /*======================================================================
 2  *           Author :   kevin
 3  *         Filename :   FlipGame.cpp
 4  *       Creat time :   2014-05-12 20:25
 5  *      Description :
 6  ========================================================================*/
 7 #include <iostream>
 8 #include <algorithm>
 9 #include <cstdio>
10 #include <cstring>
11 #include <queue>
12 #include <cmath>
13 #define M 6
14 #define Max 0xffff
15 using namespace std;
16 int cnt[Max+100];
17 int dir[M][M]={
18     0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
19     0x0000,0xc800,0xe400,0x7200,0x3100,0x0000,
20     0x0000,0x8c80,0x4e40,0x2720,0x1310,0x0000,
21     0x0000,0x08c8,0x04e4,0x0272,0x0131,0x0000,
22     0x0000,0x008c,0x004e,0x0027,0x0013,0x0000,
23     0x0000,0x0000,0x0000,0x0000,0x0000,0x0000
24 };
25 int BFS(int t)
26 {
27     queue<int>pq;
28     cnt[t] = 1;
29     pq.push(t);
30     while(!pq.empty()){
31         int num = pq.front();
32         pq.pop();
33         for(int i = 1; i <= 4; i++){
34             for(int j = 1; j <= 4; j++){
35                 int d = num^dir[i][j];
36                 if(d == 0x0000 || d == 0xffff){
37                     cnt[d] = cnt[num];
38                     return cnt[d];
39                 }
40                 if(!cnt[d]){
41                     cnt[d] = cnt[num]+1;
42                     pq.push(d);
43                 }
44             }
45         }
46     }
47     return -1;
48 }
49 void Init()
50 {
51     char str[M];
52     int number = 0,steps;
53     while(cin>>str){
54         number = 0;
55         memset(cnt,0,sizeof(cnt));
56         for(int j = 0; j < 4; j++){
57             number = number<<1;
58             if(str[j] == 'w'){
59                 number = number|1;
60             }
61         }
62         for(int i = 0; i < 3; i++){
63             cin>>str;
64             for(int j = 0; j < 4; j++){
65                 number = number<<1;
66                 if(str[j] == 'w'){
67                     number = number|1;
68                 }
69             }
70         }
71         if(number == 0x0000 || number == 0xffff){
72             printf("0\n");
73             continue;
74         }
75         else{
76             steps = BFS(number);
77         }
78         if(steps == -1){
79             printf("Impossible\n");
80         }
81         else{
82             printf("%d\n",steps);
83         }
84     }
85 }
86 int main(int argc,char *argv[])
87 {
88     Init();
89     return 0;
90 }
View Code
相關文章
相關標籤/搜索