poj1753 Flip Game(枚举Enum+dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents

题目链接:http://poj.org/problem?id=1753

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

题意:找出能使4X4的棋盘中的棋子同色的最小步数!

代码如下:

#include <iostream>
#include <cstring>
using namespace std;
int chess[7][7];//其实利用的只有中心的4x4
int x[5] = {0,0,1,0,-1};
int y[5] = {0,1,0,-1,0};
int flag, step;
int judge(int chess[7][7])//判断颜色是否全部相同
{
	for(int i = 1; i <= 4; i++)
	{
		for(int j = 1; j <= 4; j++)
		{
			if(chess[i][j] != chess[1][1])
				return 0;
		}
	}
	return 1;
}
void flip(int row, int col)//翻棋
{
	for(int i = 0; i <= 4; i++)
	{
		if(chess[row+x[i]][col+y[i]] == 1)
			chess[row+x[i]][col+y[i]] = 0;
		else
			chess[row+x[i]][col+y[i]] = 1;
	}
	return;
}
void dfs(int row,int col, int deep)//深搜固定步数看是否能同色
{
	if(deep == step)
	{
		flag = judge(chess);
		return;
	}
	if(flag || row == 5)
		return;
	flip(row,col);
	if(col < 4)
		dfs(row,col+1,deep+1);
	else
		dfs(row+1,1,deep+1);
	flip(row,col);//不符合就翻回之前的状态
	if(col < 4)
		dfs(row,col+1,deep);
	else
		dfs(row+1,1,deep);
	return;
}
int main()
{
	char temp;
	int i, j;
	memset(chess,0,sizeof(chess));
	for(i = 1; i <= 4; i++)
	{
		for(j = 1; j <= 4; j++)
		{
			cin >>temp;
			if(temp == 'b')
				chess[i][j] = 1;
		}
	}
	for(step = 0; step <= 16; step++)
	{//对每一步进行枚举(Enum)
		dfs(1,1,0);
		if(flag)
			break;
	}
	if(flag)
		cout<<step<<endl;
	else
		cout<<"Impossible"<<endl;
	return 0;
}

poj1753 Flip Game(枚举Enum+dfs),布布扣,bubuko.com

时间: 2024-10-05 05:16:11

poj1753 Flip Game(枚举Enum+dfs)的相关文章

poj2965 The Pilots Brothers&#39; refrigerator(直接计算或枚举Enum+dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=2965 Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to open a refrigerator. There are 16 handles

c# 枚举enum

1 定义枚举 enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }; 默认情况下,枚举中的每个元素的基础类型的值是int,如果不指定值,则其值以1递增;Days.Sunday 的值为 0,Days.Monday 的值为 1,依此类推. public enum TimeOfDay { Moning=0; AfterNoon=1; Evening=2; } 使用枚举: public string ge

poj2965 The Pilots Brothers&#39; refrigerator 枚举或DFS

http://poj.org/problem?id=2965 一.枚举每一个‘+’点,对该点和该点所在的同一行和同一列所有点进行操作,开关本身状态改变了7次,开关同一行.同一列的开关状态改变了4次,其他开关状态改变了2次. 然后,用一个数组存取每个操作,如果为奇数,则证明该点为必须点,否则为不必要的. 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 char map[4][4]; 5 int ans[4

C++和Java中枚举enum的用法

在C++和java中都有枚举enum这个关键字,但是它们之间又不太一样.对于C++来说,枚举是一系列命名了的整型常量,而且从枚举值转化为对应的整型值是在内部进行的.而对于Java来说,枚举更像一个类的命名的实例,你可以自定义枚举的成员,枚举值转化为对应的整型值是再外部进行的.下面以我之前的一篇博客8.1 Implement Blackjack 实现21点纸牌来说明,里面有一个扑克牌花色的枚举类: // C++ defination enum Suit {Club, Diamond, Heart,

HDU 5024 Wang Xifeng&#39;s Little Plot (枚举 + DFS记忆化搜索)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 513    Accepted Submission(s): 338 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

Java 语法 索引 ----- 枚举(Enum)

enum Speed { STOP, SLOW, NORMAL, FAST } Speed s = Speed.SLOW; switch(s) { case SLOW: break; } 参考文献: Java Quick Syntax Reference by Mikael Olsson Java 语法 索引 ----- 枚举(Enum)

POJ1753——Flip Game

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 either it's black or white side up. Each r

Lua 与C/C++ 交互系列:动态注册枚举enum到Lua Code中,在运行时在Lua Code中获取内省信息

在Lua 5.1 Reference Manual  对于Lua 值和类型的介绍.Lua是一个动态语言,在Lua中变量仅仅有值而没有类型.所以在Lua中的变量不需要声明.所以的值本身包含类型. 其实Lua 包含一种运行时类型识别,通过type()函数,可以在运行时获取值的类型. 信息来自: Lua 5.1 Reference Manual  Values and Types Lua is a dynamically typed language. This means that variable

mysql数据类型——枚举enum(‘F’,&#39;M&#39;)

ENUM(“value1”,“value2”,...) 说明:枚举,列值可赋予值列表中的某个成员 允许的属性:除通用属性外无其他属性 缺省值:如果列可为NULL,则为NULL:如果列为NOTNULL,则为第一个枚举值 存储需求:对1到255个成员的枚举1个字节,对255到65535个成员的枚举2个字节 比较:不区分大小写(MySQL3.22.1版以前区分大小写) mysql数据类型--枚举enum('F','M')