HDU-1495非常可乐(bfs + 倒水问题)

传送门

思路

用结构体存每一次的状态,然后去更新下一次;

分类讨论:
s -> n;
n -> s;
s -> m;
m -> s;
n -> m;
m -> n;

相似题目:QWQ

代码

#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>
#include <utility>
#include <stack>
#define me memset 

using namespace std;

typedef long long ll;
typedef pair<int,int>PII;

const int N = 210;
const int null = 0x3f3f3f3f;

int s,n,m;
bool st[N][N][N];

struct Edge
{
	int s,n,m,step;
}edge;

void bfs()
{
	queue<Edge> q;
	
	edge.s = s; 
	edge.n = 0;
	edge.m = 0;
	edge.step = 0;
	st[edge.s][edge.n][edge.m] = true;
	
	q.push(edge);
	
	while(q.size())
	{
		auto t = q.front();
		q.pop();
		//cout<<"chushi------>"<<t.s<<' '<<t.n<<' '<<t.m<<endl;
		
		if((t.n == t.m && t.s == 0) || (t.n == t.s && t.m == 0) || (t.m == t.s && t.n == 0))
		{
		//	cout << t.s << " " << t.n << " " << t.m << endl;
			cout << t.step << endl;
			return ;
		}
		
		//s -> n;
		if(t.n != n && t.s != 0)
		{
			Edge tt = t;
			int idx = n - tt.n;
			
			if(idx >= tt.s)
			{
				if(!st[0][tt.n + tt.s][tt.m])
				{
					st[0][tt.n + tt.s][tt.m] = true;
				
					tt.n += tt.s;
					tt.s = 0;
					tt.step ++;
					
					q.push(tt);
				}
			}
			else
			{
				if(!st[tt.s - idx][n][tt.m])
				{
					st[tt.s - idx][n][tt.m] = true;
					
					tt.s -= idx;
					tt.n = n;
					tt.step ++;
					
					q.push(tt);
				}
			}
		//	cout << "sn" << endl;
		//	cout << tt.s << " " << tt.n << " " << tt.m << endl;
		}
		//n -> s;
		if(t.s != s && t.n != 0)
		{
			Edge tt = t;
			int idx = s - tt.s;
			
			if(idx >= tt.n)
			{
				if(!st[tt.s + tt.n][0][tt.m])
				{
					st[tt.s + tt.n][0][tt.m] = true;
				
					tt.s += tt.n;
					tt.n = 0;
					tt.step ++;
					
					q.push(tt);
				}
			}
			else
			{
				if(!st[s][tt.n - idx][tt.m])
				{
					st[s][tt.n - idx][tt.m] = true;
					
					tt.n -= idx;
					tt.s = s;
					tt.step ++;
					
					q.push(tt);
				}
			}
		//	cout << "ns" << endl;
		//	cout << tt.s << " " << tt.n << " " << tt.m << endl;
		}
		//s -> m;
		if(t.m != m && t.s != 0)
		{
			Edge tt = t;
			int idx = m - tt.m;
			
			if(idx >= tt.s)
			{
				if(!st[0][tt.n][tt.m + tt.s])
				{
					st[0][tt.n][tt.m + tt.s] = true;
				
					tt.m += tt.s;
					tt.s = 0;
					tt.step ++;
					
					q.push(tt);
				}
			}
			else
			{
				if(!st[tt.s - idx][tt.n][m])
				{
					st[tt.s - idx][tt.n][m] = true;
					
					tt.s -= idx;
					tt.m = m;
					tt.step ++;
					
					q.push(tt);
				}
			}
		//	cout << "sm" << endl;
		//	cout << tt.s << " " << tt.n << " " << tt.m << endl;
		}
		//m -> s;
		if(t.s != s && t.m != 0)
		{
			Edge tt = t;
			int idx = s - tt.s;
			
			if(idx >= tt.m)
			{
				if(!st[tt.s + tt.m][tt.n][0])
				{
					st[tt.s + tt.m][tt.n][0] = true;
				
					tt.s += tt.m;
					tt.m = 0;
					tt.step ++;
					
					q.push(tt);
				}
			}
			else
			{
				if(!st[s][tt.n][tt.m - idx])
				{
					st[s][tt.n][tt.m - idx] = true;
					
					tt.m -= idx;
					tt.s = s;
					tt.step ++;
					
					q.push(tt);
				}
			}
		//	cout << "ms" << endl;
		//	cout << tt.s << " " << tt.n << " " << tt.m << endl;
		}
		//n -> m;
		if(t.m != m && t.n != 0)
		{
			Edge tt = t;
			int idx = m - tt.m;
			
			if(idx >= tt.n)
			{
				if(!st[tt.s][0][tt.m + tt.n])
				{
					st[tt.s][0][tt.m + tt.n] = true;
				
					tt.m += tt.n;
					tt.n = 0;
					tt.step ++;
					
					q.push(tt);
				}
			}
			else
			{
				if(!st[tt.s][tt.n - idx][m])
				{
					st[tt.s][tt.n - idx][m] = true;
					
					tt.n -= idx;
					tt.m = m;
					tt.step ++;
					
					q.push(tt);
				}
			}
		//	cout << "nm" << endl;
		//	cout << tt.s << " " << tt.n << " " << tt.m << endl;
		}
		//m -> n;
		if(t.n != n && t.m != 0)
		{
			Edge tt = t;
			int idx = n - tt.n;
			
			if(idx >= tt.m)
			{
				if(!st[tt.s][tt.n + tt.m][0])
				{
					st[tt.s][tt.n + tt.m][0] = true;
				
					tt.n += tt.m;
					tt.m = 0;
					tt.step ++;
					
					q.push(tt);
				}
			}
			else
			{
				if(!st[tt.s][n][tt.m - idx])
				{
					st[tt.s][n][tt.m - idx] = true;
					
					tt.m -= idx;
					tt.n = n;
					tt.step ++;
					
					q.push(tt);
				}
			}
		//	cout << "m n" << endl;
		//	cout << tt.s << " " << tt.n << " " << tt.m << endl;
		}
	}
	
	puts("NO");
}

int main()
{
	while(cin >> s >> n >> m)
	{
		if(s == 0 || n == 0 || m == 0) break;
		
		if(s % 2 == 1)
		{
			puts("NO");
			continue;
		}
		
		bfs();
		
		me(st,0,sizeof st);
	}
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

半碗无糖蓝莓冻

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

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

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

打赏作者

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

抵扣说明:

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

余额充值