## POJ 1753 Flip Game（状态压缩+BFS）解题报告

2016-07-25 3,722 次浏览

# 题面

Flip Game
Time Limit: 1000MS Memory Limit: 65536K

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:

1. Choose any one of the 16 pieces.
2. 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

Source

Northeastern Europe 2000

# 解题思路

0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15

bwbw
wwww
bbwb
bwwb

0101
1111
0010
0110

0110010011111010

bwbw
wwww
bbwb
bwwb

0110010011111010

0001001100010000

0110010011111010
0001001100010000
——————————
0111011111101010

## 参考代码

#include <cstdio>
#include <queue>
using namespace std;

const int size = 4;
const int dir_num = 4;
struct info {
int value;    // 状态值
int step;    // 步数
};
int dir[dir_num][2] = {
{-1, 0}, {0, 1},
{1, 0}, {0, -1},
};
int pos[size*size];    // 16 种翻转状态
bool vis[1<<(size*size)];    //标记某状态是否已出现过

// 判断某个位置是否在棋盘内
bool Judge(int x, int y) {
if(x>=0 && x<size
&& y>=0 && y<size)
return true;
else return false;
}

// 预处理 16 种翻转状态
void Initialize() {
for(int i=0; i<size; ++i) {
for(int j=0; j<size; ++j) {
// i*size + j 即编号
int value = 1 << (i*size + j);
for(int k=0; k<dir_num; ++k) {
int next_x = i+dir[k][0];
int next_y = j+dir[k][1];
if(Judge(next_x, next_y))
// 加上 1<<(编号) 即可将此位置设置为1
value += 1 << (next_x*size + next_y);
}
pos[i*size+j] = value;
}
}
}

int BFS(int value) {
queue<info> q;
info s = {value, 0};
q.push(s);
vis[s.value] = true;
while(!q.empty()) {
info f = q.front();
q.pop();
// 盘面全黑 (0) 或全白 (2^16-1) 时结束，返回步数
if(f.value==0 || f.value==(1<<(size*size))-1)
return f.step;
// 搜索全部 16 个位置
for(int i=0; i<size*size; ++i) {
// 通过异或运算得到翻转后的状态
info next = {f.value^pos[i], f.step+1};
if(!vis[next.value]) {
q.push(next);
vis[next.value] = true;
}
}
}

return -1;    // 无法到达目标状态，返回 -1
}

int main(int argc, char const *argv[]) {
Initialize();
char str[5];
int value = 0;
for(int i=0; i<size; ++i) {
scanf("%s", str);
// 计算起始状态的值
for(int j=0; j<size; ++j) {
if(str[j] == 'w')
value += 1 << (i*size + j);
}
}
int ans = BFS(value);
if(ans >= 0) printf("%d\n", ans);
else printf("Impossible\n");

return 0;
}

bLue 创作，采用 知识共享署名 3.0，可自由转载、引用，但需署名作者且注明文章出处。