POJ1753--HDU2209--深搜翻牌类游戏

Problem Description
有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌。但是麻烦的是,每当你翻一张纸牌(由正翻到反,或者有反翻到正)时,他左右两张纸牌(最左边和最右边的纸牌,只会影响附近一张)也必须跟着翻动,现在给你一个乱的状态,问你能否把他们整理好,使得每张纸牌都正面朝上,如果可以,最少需要多少次操作。
Input
有多个case,每个case输入一行01符号串(长度不超过20),1表示反面朝上,0表示正面朝上。
Output
对于每组case,如果可以翻,输出最少需要翻动的次数,否则输出NO。
 
Sample Input
 
   
01 011
Sample Output
 
   
NO 1
/*
每张牌都要正面朝上,每张牌最多只可能翻动一次
朴素做法是暴力深搜。枚举每张牌翻与不翻。
*/
#include 
#include 
#include 
using namespace std;
char A[100];
bool map[100];
int step,len;
bool flag;
bool judge()
{
	for(int i=1;i<=len;i++)
	{
		if(map[i]!=0)return 0;
	}
	return 1;
}
void flip(int u)
{
	map[u-1]=!map[u-1];
	map[u]=!map[u];
	map[u+1]=!map[u+1];
}
void dfs(int u,int deep)
{
	if(deep==step)
	{
		flag=judge();
		return;
	}
	if(flag||u>len)return;
	flip(u);
	dfs(u+1,deep+1);
	flip(u);
	dfs(u+1,deep);
}
int main()
{
	while(cin>>A)
	{
		flag=false;
		len=strlen(A);
		for(int i=1;i<=len;i++)
		{
			map[i]=(A[i-1]=='1');//map[i]=i表示正面朝上
		}
		//最多翻len-1次
		for(step=0;step<=len;step++)
		{
			dfs(1,0);
			if(flag)break;
		}
		if(flag)printf("%d\n",step);
		else printf("NO\n");
	}
	return 0;
}

高效做法是枚举第一张牌翻不翻,接下来的操作也就确定了。。

#include 
#include 
#include 
#include 
using namespace std;
#define inf 0x3f3f3f3f
bool vis[28];
bool val[28];
char A[28];
inline int min(int a,int b)
{
    return a>b?b:a;
}
int main()
{
    while(cin>>A)
    {
        int len=strlen(A);
        for(int i=0;i



接下来是POJ1753.是类型题。做法很多。先上暴力深搜。回头再更新0-1集合和枚举法。不然代码多了有点乱。

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
#include 
#include 
using namespace std;
bool map[6][6],flag;
int step;
int heng[]={-1,1,0,0,0};
int zong[]={0,0,0,1,-1};
bool judge()
{
	for(int i=1;i<=4;i++)
	{
		for(int j=1;j<=4;j++)
		{
			if(map[i][j]!=map[1][1])
			{
				return 0;
			}
		}
	}
	return 1;
}
void flip(int r,int c)
{
	for(int i=0;i<5;i++)
	{
		map[r+heng[i]][c+zong[i]]=!map[r+heng[i]][c+zong[i]];
	}
}	
void dfs(int r,int c,int deep)
{
	if(deep==step)
	{
		if(judge())
		{
			flag=1;
		}
		return;
	}
	if(r>4||flag)return;
	flip(r,c);
	if(c<4)
	{
		dfs(r,c+1,deep+1);
	}
	else dfs(r+1,1,deep+1);
	flip(r,c);
	if(c<4)
	{
		dfs(r,c+1,deep);
	}
	else dfs(r+1,1,deep);
}
int main()
{
	flag=false;
	for(int i=1;i<=4;i++)
	{
		for(int j=1;j<=4;j++)
		{
			map[i][j]=(getchar()=='b');
		}
		getchar();
	}
	for(step=0;step<=16;step++)
	{
		dfs(1,1,0);
		if(flag)break;
	}
	if(flag)printf("%d\n",step);
	else printf("Impossible\n");
}


 


 

你可能感兴趣的:(搜索_dfs,枚举)