POJ 题目1753 Flip Game(DFS)

Flip Game

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 33029   Accepted: 14435

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

ac代码

#include<stdio.h>
#include<string.h>
int map[10][10],step,flag=0;
int dx[5]={0,0,0,-1,1};
int dy[5]={1,-1,0,0,0};
int jud()
{
	int i,j;
	for(i=1;i<=4;i++)
		for(j=1;j<=4;j++)
		{
			if(map[i][j]!=map[1][1])
				return 0;
		}
	return 1;
}
void fun(int x,int y)
{
	int i,j;
	for(i=0;i<5;i++)
	{
		int fx=x+dx[i];
		int fy=y+dy[i];
		map[fx][fy]=!map[fx][fy];
	}
}
void dfs(int x,int y,int cnt)
{
	if(cnt==step)
	{
		flag=jud();
		return;
	}
	if(flag)
		return;
	if(x==5)
		return;
	fun(x,y);
	if(y<4)
		dfs(x,y+1,cnt+1);
	else
		dfs(x+1,1,cnt+1);
	fun(x,y);
	if(y<4)
		dfs(x,y+1,cnt);
	else
		dfs(x+1,1,cnt);
}
int main()
{
	int i,j;
	char c;
	for(i=1;i<=4;i++)
	{
		for(j=1;j<=4;j++)
		{
			scanf("%c",&c);
			if(c=='b')
				map[i][j]=1;
		}
		getchar();
	}
	for(step=0;step<=16;step++)
	{
		dfs(1,1,0);
		if(flag)
			break;
	}
	if(flag)
		printf("%d\n",step);
	else
		printf("Impossible\n");
}
时间: 2024-11-08 03:18:21

POJ 题目1753 Flip Game(DFS)的相关文章

POJ 1753 Flip Game (DFS + 枚举)

题目:http://poj.org/problem?id=1753 这个题在开始接触的训练计划的时候做过,当时用的是DFS遍历,其机制就是把每个棋子翻一遍,然后顺利的过了,所以也就没有深究. 省赛前一次做PC2遇到了几乎一模一样的题,只不过是把棋盘的界限由4X4改为了5X5,然后一直跑不出结果来,但是当时崔老湿那个队过了,在最后总结的时候,崔老湿就说和这个题一样,不过要枚举第一行进行优化. 我以为就是恢复第一行然后第二行以此类推,不过手推一下结果是6不是4,就知道这个有问题. 问了崔老湿,问了+

POJ 1753 Flip Game (DFS)

Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32104   Accepted: 13988 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

POJ 题目2245 Lotto(DFS水)

Lotto Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6549   Accepted: 4153 Description In the German Lotto you have to select 6 numbers from the set {1,2,...,49}. A popular strategy to play Lotto - although it doesn't increase your chan

POJ 1753 Flip Game (高斯消元 枚举自由变元求最小步数)

题目链接 题意:4*4的黑白棋,求把棋全变白或者全变黑的最小步数. 分析:以前用状态压缩做过. 和上题差不多,唯一的不同是这个终态是黑棋或者白棋, 但是只需要把给的初态做不同的两次处理就行了. 感觉现在还只是会套模板,不能独立的思考,好伤心.... 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath&g

POJ 1753 Flip Game(状态压缩+BFS)

题目网址:http://poj.org/problem?id=1753 题目: Flip Game 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 ei

[ACM] POJ 1753 Flip Game (枚举,BFS,位运算)

Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29921   Accepted: 12975 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

POJ 1753 Flip Game(高斯消元法,枚举自由变元)

题目: Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34731   Accepted: 15207 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

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京

POJ题目(转)

http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (