HDU 1495非常可乐(BFS)

当s为奇数时直接输出NO   否则容易超时。。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<string.h>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<ctime>
#include<queue>
#define hash hashh
using namespace std;
typedef long long ll;
#define inf 1000000000+7

#define sp  system("pause")

int vis[110][120][120];
class node
{
public:
	int a[3],cot;
};
int b[3];
 node pour(node &x, int from, int to)
{
	node por = x;
	if (!por.a[from])return x;
	if (por.a[to] == b[to])return x;
	if (b[to] - por.a[to] > por.a[from])
	{
		por.a[to] += por.a[from];
		por.a[from] = 0;
		return por;
	}
	else
	{
		por.a[from] -= (b[to] - por.a[to]);
		por.a[to] = b[to];
		return por;
	}
}

 bool cheak(node &x)
{
	if (x.a[0] == 0 && x.a[1] == x.a[2])return true;
	if (x.a[1] == 0 && x.a[0] == x.a[2])return true;
	if (x.a[2] == 0 && x.a[1] == x.a[0])return true;
	return false;
}

int main()
{
	while (scanf("%d %d %d",&b[0],&b[1],&b[2])!=EOF)
	{

		if (b[0] == 0 && b[1] == 0 && b[2] == 0)break;
		if (b[0] & 1)
		{
			printf("NO\n");
			continue;
		}
		memset(vis, 0, sizeof vis);
		queue<node>q;
		node start;
		start.cot = 1;
		start.a[0] = b[0];
		start.a[1] = 0;
		start.a[2] = 0;
		node in;
		in = pour(start, 0, 1);
		vis[start.a[0]][start.a[1]][start.a[2]] = 1;
		if (!vis[in.a[0]][in.a[1]][in.a[2]])
		{
			q.push(in);
			vis[in.a[0]][in.a[1]][in.a[2]] = 1;
		}
		in = pour(start, 0, 2);
		if (!vis[in.a[0]][in.a[1]][in.a[2]])
		{
			q.push(in);
			vis[in.a[0]][in.a[1]][in.a[2]] = 1;
		}
		int ans = -1;
		while (!q.empty())
		{
			start = q.front();
			if (cheak(start))
			{
				ans = start.cot;
				break;
			}
			q.pop();
			start.cot++;
			in = pour(start, 0, 2);
			if (!vis[in.a[0]][in.a[1]][in.a[2]])
			{
				q.push(in);
				vis[in.a[0]][in.a[1]][in.a[2]] = 1;
			}
			in = pour(start, 0, 1);
			if (!vis[in.a[0]][in.a[1]][in.a[2]])
			{
				q.push(in);
				vis[in.a[0]][in.a[1]][in.a[2]] = 1;
			}
			in = pour(start, 1, 0);
			if (!vis[in.a[0]][in.a[1]][in.a[2]])
			{
				q.push(in);
				vis[in.a[0]][in.a[1]][in.a[2]] = 1;
			}
			in = pour(start, 1, 2);
			if (!vis[in.a[0]][in.a[1]][in.a[2]])
			{
				q.push(in);
				vis[in.a[0]][in.a[1]][in.a[2]] = 1;
			}
			in = pour(start, 2, 1);
			if (!vis[in.a[0]][in.a[1]][in.a[2]])
			{
				q.push(in);
				vis[in.a[0]][in.a[1]][in.a[2]] = 1;
			}
			in = pour(start, 2, 0);
			if (!vis[in.a[0]][in.a[1]][in.a[2]])
			{
				q.push(in);
				vis[in.a[0]][in.a[1]][in.a[2]] = 1;
			}
		}
		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、付费专栏及课程。

余额充值