POJ 1753 Flip Game
1题目
===================
Flip Game
Time Limit:1000MS | Memory Limit:65536K | |
Total Submissions:25618 | Accepted:11072 |
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
===================
2思路
这是一个枚举题。首先要注意翻转序列不会重复已经翻过的位置,因为重复翻同一位置, 相当于取消这个操作,这样得到的翻转序列更短。其次是翻转序列是无序的,也就是是 说每一个位置要么被翻转过,要么没有。一共16个位置,只要从16个位置中选0个或者 1个或者2个,。。。,进行翻转,当出现成功时就停止。这时候问题其实转化为在0到15 个数中任意选k个数(0<=k<=15)。我的代码是基于以上思路。
参考了其他人的思路,大部分是通过广度优先搜索做的,还有一个技巧是可以把整个矩阵 看作是一个16位的数字,每位是0或1, 翻转的过程其实是和一个数做异或操作。
3代码
/*
* Problem: POJ 1753 Flip Game
* Lang: ANSI C
* Time: 469MS
* Memory: 388K
* Code Length: 1358B
* Author: minix
*/
#include <stdio.h>
#define N 20
#define M 4
int data[N];
char rect[M][M];
void fan(char tmp[M][M], int i, int j) {
if (i<0 || j<0 || i>=M || j>=M)
return;
if (tmp[i][j] == 'b') tmp[i][j]='w';
else tmp[i][j] = 'b';
}
void flip(char tmp[M][M], int n) {
int row=n/M, col=n%M;
fan(tmp, row, col);
fan(tmp, row-1, col);
fan(tmp, row+1, col);
fan(tmp, row, col-1);
fan(tmp, row, col+1);
}
int achieve(char tmp[M][M]) {
int i, j, c;
c = tmp[0][0];
for (i=0; i<M; i++)
for (j=0; j<M; j++)
if (tmp[i][j]!=c)
return 0;
return 1;
}
int cmk (int *p, int start, int end, int n, int begin) {
int i, j;
char tmp[M][M];
if (n == 0) {
for (i=0; i<M; i++)
for (j=0; j<M; j++)
tmp[i][j] = rect[i][j];
for (i=begin-1; i>=0; i--) {
flip (tmp, p[i]);
}
if (achieve (tmp))
return 1;
}
for (i=start; i<=end; i++) {
p[begin] = i;
if (cmk (p, i+1, end, n-1, begin+1) == 1)
return 1;
}
return 0;
}
int main() {
int i, j;
for (i=0; i<M; i++) {
for (j=0; j<M; j++)
scanf ("%c", &rect[i][j]);
getchar();
}
for (i=0; i<M*M; i++)
if (cmk (data, 0, 15, i, 0) == 1)
break;
if (i != M*M)
printf ("%d\n", i);
else
printf ("Impossible\n");
return 0;
}