Flip Game / POJ 1753

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: ios

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

 

Inputthis

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

Outputspa

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 Inputcode

bwwb
bbwb
bwwb
bwww

Sample Outputblog

4

題意ip

一個4*4黑白棋盤,每次能夠將一個格子和其周圍的4個格子反轉成相反的顏色,問:至少反轉多少次,整個棋盤一種顏色。

題解input

//方法一
dfs 暴力枚舉
枚舉每一個格子翻或不翻,若是最後同色,看下操做次數,更新答案

//方法二⭐️
二進制枚舉 & 狀態壓縮

Codestring

 1 #include<cstdio>
 2 #include<cstring>
 3 
 4 int ret = 20, dir[5][2]={0,0,0,1,1,0,0,-1,-1,0};
 5 char str[5][5];
 6 
 7 bool check(){
 8     char c = str[0][0];
 9     for(int i = 0; i < 4; i++)
10         for(int j = 0; j < 4; j++)
11             if(str[i][j] != c)
12                 return 0;
13     return 1;
14 }
15 
16 void change(int x, int y){
17     for(int i = 0; i < 5; i++){
18         int tx = x + dir[i][0];
19         int ty = y + dir[i][1];
20         if(tx < 0 || tx >= 4 || ty < 0 || ty >= 4)
21             continue;
22         if(str[tx][ty] == 'w')
23             str[tx][ty] = 'b';
24         else if(str[tx][ty] == 'b')
25             str[tx][ty] = 'w';
26     }
27 }
28 
29 void dfs(int x,int y,int step){
30     if(step > 16)
31         return;
32     if(check()){
33         if(step < ret)
34             ret = step;
35         return;
36     }
37     if(y >= 4){
38         x++;
39         y=0;
40     }
41     if(x >= 4)
42         return;
43     for(int i = 0; i < 2; i++){
44         if(i == 0){
45             change(x, y);
46             dfs(x, y+1, step+1);
47             change(x, y);
48         }
49         else
50             dfs(x, y+1, step);
51     }
52 }
53 int main(){
54     for(int i = 0; i < 4; i++)
55         scanf("%s", str[i]);
56     dfs(0, 0, 0);
57     if(ret <= 16)
58         printf("%d\n", ret);
59     else
60         printf("Impossible\n");
61     return 0;
62 }
DFS
 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 
 5 char mp[10][10];
 6 int m[10][10];
 7 int ret = 20;
 8 
 9 bool check(){
10     for(int i = 0; i < 4; i++){
11         for(int j = 0; j < 4; j++){
12             if(m[i][j] != m[0][0]) return false;
13         }
14      }
15      return true;
16 }
17 
18 void change(int dep){
19     int x = dep / 4;
20     int y = dep%4;
21     m[x][y] ^= 1;
22     if(x-1 >= 0) m[x-1][y] ^= 1;
23     if(x+1 < 4) m[x+1][y] ^= 1;
24     if(y-1 >= 0) m[x][y-1] ^= 1;
25     if(y+1 < 4) m[x][y+1] ^= 1;
26 }
27 
28 int main(){
29      int n;
30      scanf("%d", &n);
31      ret = 20;
32      for(int i = 0; i < 4; i++) scanf("%s", mp[i]);
33      for(int i = 0; i < 4; i++){
34          for(int j = 0; j < 4; j++){
35             if(mp[i][j] == 'b')  m[i][j] = 0;
36             else    m[i][j] = 1;
37          }
38      }
39      for(int mask = 0; mask < (1 << 16); mask++){
40          int used = 0;
41          for(int i = 0; i < 16; i++){
42              if(mask & (1 << i)){
43                  change(i);
44                  used++;
45              }
46          }
47          if(check()){
48              if(used < ret){
49                  ret = used;
50              }
51          }
52          for(int i = 0; i < 16; i++){
53              if(mask & (1 << i)){
54                  change(i);
55              }
56          }
57      }
58      if(ret > 16){
59          printf("Impossible\n");
60      }
61      else printf("%d\n",ret);
62      return 0;
63 }
二進制枚舉
相關文章
相關標籤/搜索