Flip Game(DFS、状压)
Flip Game(DFS、状压)
链接:https://ac.nowcoder.com/acm/problem/106350
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld
空间限制:C/C++ 65536K,其他语言131072K
64bit IO Format: %lld
题目描述
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:
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.
- Choose any one of the 16 pieces.
- 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.
输入描述:
The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.
输出描述:
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).
示例1
输入
bwwb
bbwb
bwwb
bwww
输出
4
题意:翻棋子,每次翻转连带着上、下、左、右一起翻转。当出现全黑或是全白的时候结束,问实现全黑或全白时最少需要翻几次。
1 #include <bits/stdc++.h> 2 typedef long long LL; 3 #define pb push_back 4 #define mst(a) memset(a,0,sizeof(a)) 5 const int INF = 0x3f3f3f3f; 6 const double eps = 1e-8; 7 const int mod = 1e9+7; 8 const int maxn = 1e5+10; 9 using namespace std; 10 11 char G[10][10]; 12 int mp[10][10]; 13 int ans = INF; 14 15 int judge() 16 { 17 int t = mp[1][1]; 18 for(int i=1;i<=4;i++) 19 { 20 for(int j=1;j<=4;j++) 21 { 22 if(mp[i][j]!=t) return 0; 23 } 24 } 25 return 1; 26 } 27 28 void change(int x,int y) 29 { 30 mp[x][y]^=1; 31 mp[x+1][y]^=1; 32 mp[x-1][y]^=1; 33 mp[x][y+1]^=1; 34 mp[x][y-1]^=1; 35 } 36 37 void DFS(int x, int y, int step) 38 { 39 if(judge()) 40 { 41 ans = min(ans, step); 42 return ; 43 } 44 45 y++; 46 if(y>4) 47 { 48 y = 1; 49 x++; 50 } 51 if(x>4) return ; 52 53 DFS(x, y, step); //该位置不翻 54 55 change(x, y); //改变 56 DFS(x, y, step+1); //该位置翻动 57 change(x, y); //复原 58 59 } 60 61 int main() 62 { 63 #ifdef DEBUG 64 freopen("sample.txt","r",stdin); //freopen("data.out", "w", stdout); 65 #endif 66 67 for(int i=1;i<=4;i++) 68 { 69 scanf("%s",G[i]+1); 70 for(int j=1;j<=4;j++) 71 { 72 if(G[i][j]=='b') mp[i][j] = 1; 73 else mp[i][j] = 0; 74 } 75 } 76 DFS(1,0,0); 77 if(ans==INF) printf("Impossible\n"); 78 else printf("%d\n",ans); 79 80 return 0; 81 }
或者只用枚举第一行,然后根据上一行的情况,下一行要进行的操作其实已经被确定了,假设我们要把灯全弄亮,上一行有灭的灯a[2][3],在下一行时就要在a[3][3]处操作一次,使得a[2][3]亮。
1 #include <bits/stdc++.h> 2 typedef long long LL; 3 #define pb push_back 4 #define mst(a) memset(a,0,sizeof(a)) 5 const int INF = 0x3f3f3f3f; 6 const double eps = 1e-8; 7 const int mod = 1e9+7; 8 const int maxn = 1e5+10; 9 using namespace std; 10 11 int a[10], b[10]; //原数组转成01和转成01之后反色 12 int num[100]; 13 int ans; 14 int get_num(int x) 15 { 16 int cnt = 0; 17 while (x > 0) 18 { 19 if (x & 1) cnt++; 20 x >>= 1; 21 } 22 return cnt; 23 } 24 25 void solve(int a[]) 26 { 27 int c[10]; //用来存当前按了之后什么情况 28 int x[10]; //用来枚举每一行怎么按 29 for (x[1] = 0; x[1] <= (1<<4)-1; x[1]++) //如果让第一行这么按 30 { 31 int cnt = num[x[1]]; 32 c[1] = a[1] ^ (x[1]) ^ (x[1] >> 1) ^ ((x[1] << 1) & 0xf); //处理本行 33 c[2] = a[2] ^ x[1]; 34 for (int i = 2; i <= 4; i++) 35 { 36 x[i] = c[i-1]; //上一行灯的状态 37 cnt += num[x[i]]; //看上一行有多少灯亮,我就这一行按对应的灯把它全灭,按了num[x[i]]个灯,此时上一行全灭 38 c[i] = c[i] ^ (x[i]) ^ (x[i] >> 1) ^ ((x[i] << 1) & 0xf); //本行状态 39 c[i+1] =a[i+1] ^ x[i]; //下一行状态 40 } 41 if (c[4] == 0) ans = min(ans, cnt); //如果最后一行也全灭,说明这种方法是可行的 42 } 43 } 44 45 int main() 46 { 47 #ifdef DEBUG 48 freopen("sample.txt","r",stdin); //freopen("data.out", "w", stdout); 49 #endif 50 51 for (int i = 0; i <= ((1 << 4) -1); i++) //记录每种状态里有多少个1 52 num[i] = get_num(i); 53 for (int i = 1; i <= 4; i++) 54 for (int j = 0; j < 4; j++) 55 { 56 char c; 57 scanf(" %c", &c); // " %c"消除回车 58 if (c == 'b') a[i] |= (1 << j); 59 else b[i] |= (1 << j); 60 } 61 62 ans = 0x7f7f7f7f; 63 solve(a); 64 solve(b); 65 if (ans > 16) printf("Impossible\n"); 66 else printf("%d\n", ans); 67 68 return 0; 69 }
该题也可以用状压。由于棋子是4*4,状态比较少,完全可以用16个bite来保存,用位运算方便表示和改变状态。
具体看:https://blog.csdn.net/qq_43408238/article/details/89475878
-