题目:A:Flip Game
总时间限制: 1000ms 内存限制: 65536kB
描述
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:
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).
样例输入
bwwb
bbwb
bwwb
bwww
样例输出
4
解题思路:法一:这道题目和程序设计实习MOOC第十一周的题目A画家问题很类似,使用的是枚举,画家问题是涂成某一种颜色所需最少涂的砖块数,而这里是要考虑涂成全白或全黑两种情况下更少的那一个,代码如下。法二:使用广度优先搜索方法,从初始状态开始,翻动一个棋子之后生成所有的状态都加入到队列中,一边记录层数,一边依次扩展,直到第一个出现全白或全黑的状态,则是所求最少的翻转数目,或者队列为空了,则输出“Impossible”,由于某一个棋子翻转两次就和没翻转过一样,所以每个棋子最多翻转一次,因此每一步按顺序只翻转0/1个棋子,第i步分为翻/不翻第i个棋子,若走到第j步就看见目标节点,则之后的棋子都是不翻转的,最多走十六步就结束了(状态判重,一样的状态不能再加入队列),每一个状态可以存储成int型,黑白分别用1、0表示,十六个棋子的颜色即二进制的16位,int型32位,完全够用,取某一位也很方便,如1025,对应0000010000000001,取从左到右的第12位,即倒数第5位,1025%(2^5) = 1,1/(2^4) =0,第十二位值为0,修改某一位同理也很容易,判重也很容易,一个值对应一个状态,使用标记数组记录,为a[2^16]即可,代码略。
//方法一:枚举法
#include<iostream>
using namespace std;
int main()
{
int t, n;
int num;//存储最少需要涂画的砖数
t = 2;//全黑全白两种情况
n = 4;//4*4大小
char ch;
int a[n+1][n+2];//存储初始状态
for(int r = 1; r < n + 1; r++)
{
for(int c = 1; c < n + 1; c++)
{
cin >> ch;
if(ch == 'b')
a[r][c] = 0;
else//ch == "w"
a[r][c] = 1;
}
}
num = 400;//初始为很大的值,它表示不存在方案
int b[n+1][n+2];//存储当前某一种涂画的方法
for(int c = 1; c < n + 1; c++)
b[0][c] = 0;
for(int r = 0; r < n + 1; r++)
{
b[r][0] = 0;
b[r][n + 1] = 0;
}
//枚举出n*n的第一行(行0)(对应(n+1)*(n+2)的行1)的2^n种情况
int c, count = 1 << n;//注意,直接写成2^n不行,该用左移来表示2的n次方
for(c = 1; c < n + 1; c++)
b[1][c] = 0;
while(count--)
{
//判断当前情况是否满足条件
for(int r = 1; r < n; r++)//根据b第一行和a数组,计算b其他行的值
for(int c1 = 1; c1 < n + 1; c1++)
b[r + 1][c1] = (a[r][c1] + b[r][c1] + b[r - 1][c1] + b[r][c1 - 1] + b[r][c1 + 1]) % 2;
bool flag = true;
for(int c1 = 1; c1 < n + 1; c1++)//判断所计算的b数组能否将最后一行的灯都熄灭
{
if((b[n][c1 - 1] + b[n][c1] + b[n][c1 + 1] + b[n - 1][c1])%2 != a[n][c1])
{
flag = false;
break;//不满足,跳出当前循环,转入下一个情况
}
}
if(flag)//满足,则记录涂画的砖数
{
int num_temp = 0;
for(int r = 1; r < n + 1; r++)
for(int c1 = 1; c1 < n + 1; c1++)
if(b[r][c1] == 1)
num_temp++;
if(num_temp < num)
num = num_temp;//存储更小的涂画砖数
}
//到下一个情况
b[1][1]++;
c = 1;
while(b[1][c] > 1)
{
b[1][c] = 0;
c++;
b[1][c]++;
}
}
//此时得到的num会是上面一种情况最少涂砖数
//下面是全为另一种颜色的情况,代码段与上面是一样的
for(int r = 1; r < n + 1; r++)
{
for(int c = 1; c < n + 1; c++)
{
if(a[r][c] == 0)
a[r][c] = 1;
else//a[r][c] == 1
a[r][c] = 0;
}
}
for(int c = 1; c < n + 1; c++)
b[0][c] = 0;
for(int r = 0; r < n + 1; r++)
{
b[r][0] = 0;
b[r][n + 1] = 0;
}
//枚举出n*n的第一行(行0)(对应(n+1)*(n+2)的行1)的2^n种情况
count = 1 << n;//注意,直接写成2^n不行,该用左移来表示2的n次方
for(c = 1; c < n + 1; c++)
b[1][c] = 0;
while(count--)
{
//判断当前情况是否满足条件
for(int r = 1; r < n; r++)//根据b第一行和a数组,计算b其他行的值
for(int c1 = 1; c1 < n + 1; c1++)
b[r + 1][c1] = (a[r][c1] + b[r][c1] + b[r - 1][c1] + b[r][c1 - 1] + b[r][c1 + 1]) % 2;
bool flag = true;
for(int c1 = 1; c1 < n + 1; c1++)//判断所计算的b数组能否将最后一行的灯都熄灭
{
if((b[n][c1 - 1] + b[n][c1] + b[n][c1 + 1] + b[n - 1][c1])%2 != a[n][c1])
{
flag = false;
break;//不满足,跳出当前循环,转入下一个情况
}
}
if(flag)//满足,则记录涂画的砖数
{
int num_temp = 0;
for(int r = 1; r < n + 1; r++)
for(int c1 = 1; c1 < n + 1; c1++)
if(b[r][c1] == 1)
num_temp++;
if(num_temp < num)
num = num_temp;//存储更小的涂画砖数
}
//到下一个情况
b[1][1]++;
c = 1;
while(b[1][c] > 1)
{
b[1][c] = 0;
c++;
b[1][c]++;
}
}
if(num == 400)//没有找到方案
cout << "Impossible" << endl;
else
cout << num << endl;
system("pause");
return 0;
}