【题目描述】
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N毫升和M毫升可乐的体积为S(S<101)毫升(正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。
【输入】
三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。
【输出】
如果能平分的话请输出最少要倒的次数,否则输出"NO"。
【样例输入】
7 4 3
4 1 3
0 0 0
【样例输出】
NO
3
题目链接:https://cn.vjudge.net/problem/HDU-1495
bfs,模拟6种操作,3个杯子互相倒水
代码如下:
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
static const int MAXN=100;
bool vis[MAXN][MAXN][MAXN];
struct Node{
int a,b,c;
int step;
};
int s,n,m;
void bfs(Node node)
{
queue<Node> Q;
vis[node.a][node.b][node.c]=true;
Q.push(node);
while(!Q.empty())
{
Node now=Q.front();
Q.pop();
if((now.a==0 && now.b==s/2 && now.c==s/2) || (now.a==s/2 && now.b==0 && now.c==s/2) || (now.a==s/2 && now.b==s/2 && now.c==0))
{
cout<<now.step<<endl;
return;
}
if(now.a)
{
if(now.b!=n)
{
Node next;
next.a=now.a-min(now.a,n-now.b);
next.b=now.b+min(now.a,n-now.b);
next.c=now.c;
next.step=now.step+1;
if(!vis[next.a][next.b][next.c])
{
vis[next.a][next.b][next.c]=true;
Q.push(next);
}
}
if(now.c!=m)
{
Node next;
next.a=now.a-min(now.a,m-now.c);
next.c=now.c+min(now.a,m-now.c);
next.b=now.b;
next.step=now.step+1;
if(!vis[next.a][next.b][next.c])
{
vis[next.a][next.b][next.c]=true;
Q.push(next);
}
}
}
if(now.b)
{
if(now.a!=s)
{
Node next;
next.b=now.b-min(now.b,s-now.a);
next.a=now.a+min(now.b,s-now.a);
next.c=now.c;
next.step=now.step+1;
if(!vis[next.a][next.b][next.c])
{
vis[next.a][next.b][next.c]=true;
Q.push(next);
}
}
if(now.c!=m)
{
Node next;
next.b=now.b-min(now.b,m-now.c);
next.c=now.c+min(now.b,m-now.c);
next.a=now.a;
next.step=now.step+1;
if(!vis[next.a][next.b][next.c])
{
vis[next.a][next.b][next.c]=true;
Q.push(next);
}
}
}
if(now.c)
{
if(now.a!=s)
{
Node next;
next.c=now.c-min(now.c,s-now.a);
next.a=now.a+min(now.c,s-now.a);
next.b=now.b;
next.step=now.step+1;
if(!vis[next.a][next.b][next.c])
{
vis[next.a][next.b][next.c]=true;
Q.push(next);
}
}
if(now.b!=n)
{
Node next;
next.c=now.c-min(now.c,n-now.b);
next.b=now.b+min(now.c,n-now.b);
next.a=now.a;
next.step=now.step+1;
if(!vis[next.a][next.b][next.c])
{
vis[next.a][next.b][next.c]=true;
Q.push(next);
}
}
}
}
cout<<"NO"<<endl;
return;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0),cout.tie(0);
while(cin>>s>>n>>m && s && n && m)
{
if(s&1)
{
cout<<"NO"<<endl;
continue;
}
memset(vis,false,sizeof(vis));
bfs(Node{s,0,0,0});
}
return 0;
}