HDOJ 1495 倒可乐(BFS)

每日打卡(1/2)

传送门:点击打开链接

题目大意:有一瓶可乐和两个杯子,瞎倒以后,要求平分这瓶可乐。

    需要注意的是:可乐也可以放在瓶子里面喝。

思路:

    感觉比之前倒水的那道题目简单,但是题意有点模糊不清,没有说放在瓶子里也能喝。

    主要就是一个循环6次的BFS,思路以及解法都和倒水那题差不多

#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 105;
int s,n,m,ans;
struct node{
	int s,x,y,tot;
};
bool vis[maxn][maxn][maxn];

int bfs()
{
	node a;
	a.s= s;
	a.x = 0;
	a.y = 0;
	a.tot = 0;
	queue<node> q;
	q.push(a);
	while(!q.empty())
	{
		a = q.front();
//		cout<<a.x<<" "<<a.y<<" "<<a.s<<" "<<a.tot<<endl;
		q.pop();
		if((a.x==a.y&&!a.s)||(a.x==a.s&&!a.y)||(a.y==a.s&&!a.x))
		{
			return a.tot;
		}
		node next;
		for(int i=0;i<6;i++)//1往23,2往13,3往12
		{
			switch(i)
			{
				case 0:
					if(a.s>n-a.x)
					{
						next.s = a.s-(n-a.x);
						next.x = n;
						next.y = a.y;
					}
					else{
						next.s = 0;
						next.x = a.s+a.x;
						next.y = a.y;
					}
					break;
				case 1:
					if(a.s>m-a.y)
					{
						next.s = a.s-(m-a.y);
						next.y = m;
						next.x = a.x;
					}
					else{
						next.s = 0;
						next.y = a.s+a.y;
						next.x = a.x;
					}
					break;
				case 2:
					if(a.x>m-a.y)
					{
						next.s = a.s; 
						next.x = a.x-(m-a.y);
						next.y = m;
					}
					else{
						next.s = a.s;
						next.x = 0;
						next.y = a.x+a.y;
					}
					break;
				case 3:
					if(a.y>n-a.x)
					{
						next.s = a.s;
						next.y = a.y-(n-a.x);
						next.x = n;
					}
					else{
						next.s = a.s;
						next.y = 0;
						next.x = a.x+a.y;
					}
					break;
				case 4:
					next.s = a.s+a.x;
					next.x = 0;
					next.y = a.y;
					break;
				case 5:
					next.s = a.s+a.y;
					next.y = 0;
					next.x = a.x;
					break;
			}
			next.tot = a.tot+1;
			if(!vis[next.x][next.y][next.s])
			{
				q.push(next);
				vis[next.x][next.y][next.s] = 1;
			}
		} 
	}
	return -1;
}

int main()
{
	while(~scanf("%d%d%d",&s,&n,&m)&&s&&n&&m)
	{
		memset(vis,0,sizeof(vis));
		ans = bfs();
		if(ans==-1) cout<<"NO"<<endl;
		else cout<<ans<<endl;
	}
	return 0;
}

这是搜索专题的最后一题了,(最后一题BFS实在是太弱智了,不想做),到目前为止共做11题。

底下进入下一阶段最短路的练习~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

总想玩世不恭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值