Flip Game
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 40726 | Accepted: 17687 |
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).
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
这是我写出来的第一道翻转问题的题目,这是第一道写出来的,而我已经见过其他三道也是翻转的POJ上的题目,然后我从暑假开始看到现在都没写出来,因为我真的连题解都看不懂,特别是他们那时间复杂度的计算,真难。后来看到这题是放在基础基础基础枚举分类里面的,我开始慌了,这。。。又是翻转,我选择死亡。。。但是慢慢发现,瞎暴力一通是可以出来的,就看你怎么暴力了,因为这道题一共就16个点,那么每个点有翻转和不翻转两种情况,所以一共有2的16次方种情况,计算机跑这种数据简直是so easy。那么怎么暴力呢,这种题目,用递归很好写,所以我采用的是递归的方法,一次AC,然而那三道翻转问题数据量可没这么平易近人,简直是。。。反正欲哭无泪。
代码如下:
#include<stdio.h>
#define INF 0x3fffffff
int ans = INF;
char chess[6][6];
int map[6][6];
void func(int x, int y)
{
int i , j , k;
for(k = 0; k < 2; k++)
{
map[x][y] = k;
if(x == 3 && y == 3)
{
int cnt = 0;
char t[6][6];
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++)
t[i][j] = chess[i][j];
for(i = 0; i < 4; i++)
{
for(j = 0; j < 4; j++)
{
if(map[i][j] == 1)
{
t[i][j] = (t[i][j] == 'b' ? 'w' : 'b');
if(i + 1 < 4)
t[i + 1][j] = (t[i + 1][j] == 'b' ? 'w' : 'b');
if(i - 1 >= 0)
t[i - 1][j] = (t[i - 1][j] == 'b' ? 'w' : 'b');
if(j + 1 < 4)
t[i][j + 1] = (t[i][j + 1] == 'b' ? 'w' : 'b');
if(j - 1 >= 0)
t[i][j - 1] = (t[i][j - 1] == 'b' ? 'w' : 'b');
cnt++;
}
}
}
int flag = 1;
for(i = 0; i < 4 && flag; i++)
{
for(j = 0; j < 4 && flag; j++)
{
if(t[i][j] != t[0][0])
{
flag = 0;
}
}
}
if(flag == 1 && ans > cnt)
ans = cnt;
}
else
func(y == 3 ? x + 1 : x, y == 3 ? 0 : y + 1);
}
}
int main()
{
for(int i = 0; i < 4; i++)
scanf("%s",&chess[i]);
func(0, 0);
if(ans != INF)
printf("%d\n",ans);
else
printf("Impossible\n");
}
然后看到别人说用BFS+状态压缩时间只需50ms以内,简直是膜拜,因为我并不能想的懂怎么用BFS。。。用DFS要跑280ms。。。