hdu 1495——非常可乐

记忆化搜索+宽搜


#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

struct Node
{
	int a,b,c;
	int step;
};

int a,b,c;
int dp[105][105][105];
int flag;

int min(int a,int b)
{
	return a<b?a:b;
}

bool Judge(Node &x)
{
	int flag=0;
	if(x.a==a/2)
		flag++;
	if(x.b==a/2)
		flag++;
	if(x.c==a/2)
		flag++;
	if(flag==2)
		return 1;
	return 0;
}

void Put(Node& x,int i)
{
	if(i==1)//a b
	{
		int temp=min(x.a,b-x.b);
		x.a-=temp;
		x.b+=temp;
	}
	if(i==2)//a c
	{
		int temp=min(x.a,c-x.c);
		x.a-=temp;
		x.c+=temp;
	}
	if(i==3)//b a
	{
		int temp=min(x.b,a-x.a);
		x.b-=temp;
		x.a+=temp;
	}
	if(i==4)//b c
	{
		int temp=min(x.b,c-x.c);
		x.b-=temp;
		x.c+=temp;
	}
	if(i==5)//c a
	{
		int temp=min(x.c,a-x.a);
		x.c-=temp;
		x.a+=temp;
		
	}
	if(i==6)//c b
	{
		int temp=min(x.c,b-x.b);
		x.c-=temp;
		x.b+=temp;	
	}
}

void bfs()
{
	Node start,temp1,temp2;
	int i;
	memset(dp,0,sizeof(dp));
	queue<Node> q;
	start.a=a;
	start.b=start.c=start.step=0;
	q.push(start);
	
	while(!q.empty())
	{
		temp1=q.front();
		q.pop();
		if(Judge(temp1))
		{
			flag=1;
			cout<<temp1.step<<endl;
			return;
		}
		for(i=1;i<=8;i++)
		{
			temp2=temp1;
			Put(temp2,i);
			if(!dp[temp2.a][temp2.b][temp2.c])
			{
				dp[temp2.a][temp2.b][temp2.c]=1;
				temp2.step=temp1.step+1;
				q.push(temp2);
			}
		}
	}
}


int main()
{
	while(cin>>a>>b>>c&&a+b+c)
	{
		flag=0;
		if(a%2==1)
		{
			cout<<"NO"<<endl;
			continue;
		}
		bfs();
		if(flag==0)
			cout<<"NO"<<endl;
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值