M - 非常可乐

24 篇文章 0 订阅
14 篇文章 0 订阅

本题是广搜的题,一共有6种倒法;

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=100+10;
int v[maxn][maxn][maxn];
int d[6][2]={{0,1},{0,2},{1,2},{2,1},{1,0},{2,0}};
int full[3];
struct node{
	int cup[3];
	int step;
};

void pour(int &a,int &b,int Fullb)
{
	if(a+b<=Fullb)//如果a+b还小于b的全部的话
	{
		b+=a,a=0;//将水倒入a中 
	 } 
	 else
	 {
	 	a-=(Fullb-b);//将b瓶倒满
		 b=Fullb; 
	 }
}
int ok(node s)
{
	if(s.cup[0]+s.cup[1]==full[0]&&s.cup[0]==s.cup[1])
		return 1;
	if(s.cup[0]+s.cup[2]==full[0]&&s.cup[0]==s.cup[2])
		return 1;
	if(s.cup[1]+s.cup[2]==full[0]&&s.cup[1]==s.cup[2])
		return 1;
	return 0;
}
int bfs(node s)
{
	queue<node> q;
	q.push(s);
	while(!q.empty())
	{
		s=q.front();q.pop();
		if(ok(s))
		{
			return s.step;
		}
			
		for(int i=0;i<6;i++)
		{
			node th=s;
			pour(th.cup[d[i][0]],th.cup[d[i][1]],full[d[i][1]]);
			if(v[th.cup[0]][th.cup[1]][th.cup[2]]==0)
			{
				v[th.cup[0]][th.cup[1]][th.cup[2]]=1;
				th.step++;
				q.push(th);
			}
		}
	}
	return -1;
}
int main()
{
	freopen("M.txt","r",stdin);
	while(scanf("%d %d %d",&full[0],&full[1],&full[2]),full[0]+full[1]+full[2])
	{
		memset(v,0,sizeof(v));
		node s;
		s.cup[0]=full[0];
		s.cup[1]=s.cup[2]=s.step=0;
		int ans=bfs(s);
		if(ans==-1)
			printf("NO\n");
		else
			printf("%d\n",ans);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值