HDU-1495-非常可乐

知识点:BFS;
*csdn 有数论的做法,emm,太弱看不懂,就不管了!
*fish 练习题解~

题面:
在这里插入图片描述
AC代码:

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int half;
int s[3],sg[101][101][101];
struct wine
{
	int cup[3];
	int s;
};
void bfs()
{
	queue<wine> q;
	wine n;
	n.cup[0] = s[0];
	n.cup[1] = n.cup[2] = n.s = 0;
	q.push(n);
	
	while(!q.empty())
	{
		wine now;
		now = q.front();
		q.pop();
		for(int i = 0; i < 3; i++)
		{
			if(now.cup[i] > 0)
			{
				for(int j = 0; j < 3 ;j++)
				{
					wine tmp;
					tmp = now;
					if(i == j) continue;
					if(tmp.cup[i] > s[j] - tmp.cup[j])
					{
						tmp.cup[i] -= s[j] - tmp.cup[j];
						tmp.cup[j] = s[j];
					}
					else
					{
						tmp.cup[j] += tmp.cup[i];
						tmp.cup[i] = 0;
					}
					if(!sg[tmp.cup[0]][tmp.cup[1]][tmp.cup[2]])
					{
						sg[tmp.cup[0]][tmp.cup[1]][tmp.cup[2]] = 1;
						tmp.s++;
						if((tmp.cup[0] == tmp.cup[1] && tmp.cup[0] == half) ||(tmp.cup[2] == tmp.cup[1] && tmp.cup[1] == half)||(tmp.cup[2] == tmp.cup[0] && tmp.cup[0] == half))
						{
							cout << tmp.s << endl;
							return ;
						}
						q.push(tmp);
					}
				}
			}
		}
	}
	cout << "NO" << endl;
}
int main()
{
	while(cin >>s[0] >> s[1] >> s[2], s[1] + s[2] + s[3])
	{
		memset(sg,0,sizeof(sg));
		if(s[0] & 1) cout << "NO" << endl;
		else
		{
			sg[s[0]][s[1]][s[2]] = 1;
			half = s[0] >> 1;
			bfs();
		}
		  
	}
	return 0;
}

完全参考: 此dalao代码!

反思!
练习上指出使用 BFS来解决此题;
但是不知如何去分情况,类似的情况出现在这次的周赛HDU - 2717
使用BFS,定会用某些情况去讨论,加深对”搜索“的理解
查明需要讨论的情况,这是做bfs的key!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值