Flip Game
来源:互联网 发布:c语言创建多个进程 编辑:程序博客网 时间:2024/06/14 01:59
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:
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).
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
bwwbbbwbbwwbbwww
Sample Output
4
BFS
#include<iostream>#include<queue>using namespace std;struct Point{int x, y, m;int p[4][4];};int v[4][4],c[4][4];bool IsWB(int a[4][4]);void Change(int a[4][4], int i, int j);void Copy(int a[4][4], int b[4][4]);void BFS();int main(){int i, j;char chess[4][4];for (i = 0; i < 4; i++){for (j = 0; j < 4; j++){cin >> chess[i][j];if (chess[i][j] == 'w')c[i][j] = 0;else c[i][j] = 1;}getchar();}if (IsWB(c)) cout << "0\n";else BFS();return 0;}bool IsWB(int a[4][4]){int i, j;for (i = 0; i < 4; i++){for (j = 0; j < 4; j++){if (a[i][j] != a[0][0])return 0;}}return 1;}void Change(int a[4][4],int i,int j){int p=i, q=j;if (p >= 0 && p < 4 && q >= 0 && q < 4)a[p][q] = !a[p][q];p = i - 1; q = j;if (p >= 0 && p < 4 && q >= 0 && q < 4)a[p][q] = !a[p][q];p = i; q = j - 1;if (p >= 0 && p < 4 && q >= 0 && q < 4)a[p][q] = !a[p][q];p = i; q = j + 1;if (p >= 0 && p < 4 && q >= 0 && q < 4)a[p][q] = !a[p][q];p = i + 1; q = j;if (p >= 0 && p < 4 && q >= 0 && q < 4)a[p][q] = !a[p][q];}void Copy(int a[4][4], int b[4][4]){for (int i = 0; i < 4; i++){for (int j = 0; j < 4; j++){a[i][j] = b[i][j];}}}void BFS(){int i, j;Point t;queue<Point> q;for (i = 0; i < 4; i++){for (j = 0; j < 4; j++){Copy(t.p, c);Change(t.p, i, j);t.m = 1; t.x = i; t.y = j;q.push(t);}}while (!q.empty()){t = q.front();//将被删除的坐标if (IsWB(t.p)) break;q.pop();Point f;for (i = t.x; i < 4; i++){if (i == t.x)j = t.y + 1;else j = 0;for (; j < 4; j++){Copy(f.p, t.p);Change(f.p, i, j);f.m = t.m+1; f.x = i; f.y = j;q.push(f);}}}//whileif (!q.empty()) cout << t.m << endl;else cout << "Impossible\n";}
0 0
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- flip game
- Flip Game
- Flip Game
- FLIP GAME
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- Flip Game
- 实战 TestNG 监听器
- NBOJv2 1004 蛤玮打扫教室(线段树区间更新区间最值查询)
- MySQL常用命令
- POJ1577
- Mutable and Immutable Collections的区别
- Flip Game
- <JAVA与模式>之组合模式
- 关于对Java Struts2+poi插件 实现的 导出Excel功能
- SpringMVC拦截器教程(亲测有用)
- android项目工程下各文件夹的含义
- 118. Pascal's Triangle
- 将JNI模块打包为AAR模块
- Python中的lambda和Map混合运用
- servlet之qq(二)