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:
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
分析
这题与八数码问题很相似,所以根据前篇八数码的文章的思路稍加改动下就可以适用,状态用16个bit来表示即可,即用一整形就可表示状态。先开始用双向广搜版本改,卒。。后来用单向广搜终于调出问题所在。
1. 全黑值为(1 << 16) -1
,先开始写成`(1 << 16 -1),优先级弄错,调了好久才发现是这问题。
2. 移位操作会影响原int的值,所以toggle()函数处的逻辑最好写成个函数,不能直接写,否则卒。。
实现
#include <iostream>
#include <cstring>
#include <cstdio>
#define N 16
#define R 4
#define C 4
#define MAXN 65546 //2^N + 10次
#define NO_PARENT -1 //没有父节点
struct node
{
int data; //当前节点数字排列
int parentIndex; //父节点在数组a中的下标,可据此找到上一步的移动信息。
int d; //当前节点是由哪个点翻动引起的变化
int deep; //搜索深度
} a[MAXN];
bool visited[MAXN];
int toggleMask[N]; //以序号为i的点进行翻转时通过异或操作翻转所需要的掩码。
int targets[] = { (1 << 16) - 1, 0 };
inline bool getTarget(node* a)
{
return (a->data == targets[0] || a->data == targets[1]);
}
int toggle(int data, int i)
{
return data ^= toggleMask[i];
}
void initToggleMasks()
{
memset(toggleMask, 0, sizeof(toggleMask));
for (int i = 0; i < N; i++) {
int mask = 0;
i % C != 0 ? (mask |= (1 << (i - 1))) : 0; //第0列无左
(i + 1) % C != 0 ? ((mask |= (1 << (i + 1)))) : 0; //最后一列无右
i < C ? 0 : (mask |= (1 << (i - C))); //第0行无上
i >= (N - C) ? 0 : (mask |= (1 << (i + C))); //最后一行无下
mask |= (1 << i);
toggleMask[i] = mask;
}
}
int bfs(node* firstNode)
{
memset(visited, 0, sizeof(visited));
int head = 0, tail = 1; //首、尾指针
a[head].data = firstNode->data;
a[head].parentIndex = NO_PARENT;
a[head].d = -1;
a[head].deep = 0;
if (getTarget(&a[head])) return a[head].deep;
visited[a[head].data] = 1;
while (head < tail) {
int data = a[head].data;
for (int i = 0; i < N; i++) {
int newData = toggle(data, i);
if (!visited[newData]) {
visited[newData] = 1;
a[tail].data = newData; //将移动后的新结点信息放入对列中。
a[tail].parentIndex = head;
a[tail].d = i;
a[tail].deep = a[head].deep + 1;
if (getTarget(&a[tail])) return a[tail].deep;
tail++;
}
}
head++; //处理下一个入列结点
}
return -1;
}
int main()
{
// freopen("in.txt", "r", stdin);
node firstNode;
firstNode.data = 0;
initToggleMasks();
char ch[C + 1];
for (int i = 0; i < R; i++) {
scanf("%s", ch);
for (int j = 0; j < C; j++) {
firstNode.data <<= 1;
if (ch[j] == 'b') { firstNode.data |= 1; }
}
}
int deep = bfs(&firstNode);
if (deep == -1) {
printf("Impossible\n");
}
else {
printf("%d\n", deep);
}
return 0;
}