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).
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
思路:这个题其实就是暴力,把所有的情况都给模拟出来,翻0~16个棋子一共是2^16种情况(在递归的时候要注意减少重复的情况,否则会超时)。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <ctime>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <cstring>
#include <list>
#define MAXN 21
using namespace std;
struct Node{
int x;
int y;
};
bool flag;
char MAP[MAXN][MAXN];
int xx[] = {1, -1, 0, 0};
int yy[] = {0, 0, 1, -1};
bool judge(){
for(int i = 0; i < 4; ++i)
for(int j = 0; j < 4; ++j)
if(MAP[i][j] != MAP[0][0])
return false;
return true;
}
void change(int x, int y){
if(MAP[x][y] == 'b')
MAP[x][y] = 'w';
else
MAP[x][y] = 'b';
for(int i = 0; i < 4; ++i){
int nx = x + xx[i];
int ny = y + yy[i];
if(nx >= 0 && ny >= 0 && ny < 4 && nx < 4){
if(MAP[nx][ny] == 'b')
MAP[nx][ny] = 'w';
else
MAP[nx][ny] = 'b';
}
}
}
void dfs(int x, int y, int deep, int max_deep) {
if (deep == max_deep) {
flag = judge();
return;
}
if (flag || x == 4 || y == 4)
return;
change(x, y);
if (y < 3)
dfs(x, y + 1, deep + 1, max_deep);
else
dfs(x + 1, 0, deep + 1, max_deep);
change(x, y);
if (y < 3)
dfs(x, y + 1, deep, max_deep);
else
dfs(x + 1, 0, deep, max_deep);
}
int main(){
for(int i = 0; i < 4; ++i){
for(int j = 0; j < 4; ++j)
cin >> MAP[i][j];
}
int step;
for(step = 0; step <= 16; ++step){
dfs(0, 0, 0, step);
if(flag)
break;
}
/*for(int i = 0; i < 4; ++i){
for(int j = 0; j < 4; ++j)
cout << MAP[i][j];
cout << endl;
}*/
if(flag)
cout << step << endl;
else
cout << "Impossible\n";
}