POJ1753 Flip Game(翻转问题且纯枚举暴力翻转)

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: 
  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

这是我写出来的第一道翻转问题的题目,这是第一道写出来的,而我已经见过其他三道也是翻转的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。。。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值