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

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



接下来是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 <iostream>
#include <cstdio>
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");
}


 


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值