Flip Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21178 | Accepted: 9172 |
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
bwwb bbwb bwwb bwww
Sample Output
4
Source
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
这题的翻牌只有两种可能,翻或不翻,所以相当于我们在遍历一棵16层的二叉树,通过深度优先找出符合条件的所有方案,找出最理想的结果。
这题我花了很久,看了网上的解题报告才搞定,汗~~~~~我主要卡在深度优先遍历这棵树上,本来也是用递归的想法去遍历,但是递归没有处理好,弄了很久,后来看了网上的解题报告后,才把深度优先遍历的递归实现出来,平时对递归还是要加强学习,练习。
//////////////////////////////////////////////////////////////////////////////////////
代码:
/***********************
* 程序名:Flip Game.cpp
* 功能:ACM
************************/
#include <iostream>
using namespace std;
//void flip(int [][4], int, int);
//int success_fail(int [][4]);
void dfs(int, int, int );
int minimal = 99;
int pices[4][4];
int main()
{
char string[4][4];
int i, j, t;
for(i = 0; i < 4; i++) {
cin>>string[i];
for(j = 0; j < 4; j++) {
if(string[i][j] == 'w') {
pices[i][j] = 0;
}
else {
pices[i][j] = 1;
}
}
}
dfs(0, 0, 0);
if(minimal != 99) {
cout<<minimal<<endl;
}
else {
cout<<"Impossible"<<endl;
}
return 0;
}
int success_fail()
{
int SUM = 0, i, j;
for(i = 0; i < 4; i++) {
for(j = 0; j < 4; j++) {
SUM += pices[i][j];
}
}
if(SUM == 0 || SUM == 16) {
return 1;
}
else {
return 0;
}
}
void flip(int i, int j)
{
pices[i][j] = (pices[i][j]+1)%2;
if(i > 0) {
pices[i-1][j] = (pices[i-1][j]+1)%2;
}
if(i < 3) {
pices[i+1][j] = (pices[i+1][j]+1)%2;
}
if(j > 0) {
pices[i][j-1] = (pices[i][j-1]+1)%2;
}
if(j < 3) {
pices[i][j+1] = (pices[i][j+1]+1)%2;
}
}
void dfs(int x, int y, int step)
{
int temp_x, temp_y;
//cout<<"dfs"<<endl;
//cout<<"x="<<x<<"y="<<y<<endl;
if(success_fail()) {
//cout<<"success"<<endl;
if(minimal > step) {
minimal = step;
}
}
else {
if(x != 4) { //判断是否越界,由于需要访问最后一个点,所以临界点在最后一个点后面
temp_x = x;
temp_y = y;
if((y+1) > 3) {
x += 1;
y = 0;
}
else {
y += 1;
}
flip(temp_x, temp_y);//实现深度优先遍历,递归实现
dfs(x, y, step+1);
flip(temp_x, temp_y);
dfs(x, y, step);
}
}
}