非常可乐
Time Limit: 2000/1000 MS (Java/Others) | Memory Limit: 32768/32768 K (Java/Others) |
---|
Problem Description
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。
Input
三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。
Output
如果能平分的话请输出最少要倒的次数,否则输出"NO"。
Sample Input
7 4 3
4 1 3
0 0 0
Sample Output
NO
3
bfs关键是状态的标记,这里用三维数组存状态,因为有三个瓶子
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int b[3], ans, half;
int book[101][101][101];
struct node
{
int pot[3], step;//三个瓶子内可乐的体积和步数
};
int bfs()
{
node now, next;
now.pot[0]=b[0], now.pot[1]=0, now.step=0, now.pot[2]=0;
queue<node>p;
p.push(now);
while(!p.empty())
{
now=p.front(), p.pop();
if((now.pot[0]==half&&now.pot[1]==half)||(now.pot[0]==half&&now.pot[2]==half)||(now.pot[1]==half&&now.pot[2]==half))//如果有任何两个瓶子装了half量的可乐
{
ans=now.step;
return 1;
}
for(int i=0; i<3; i++)//模拟三个瓶子往外倒水
{
if(now.pot[i]==0)//空瓶子
continue;
for(int j=0; j<3; j++)//模拟三个瓶子接受水
{
if(i==j)//不能自己倒自己
continue;
next=now;
if(now.pot[i]>b[j]-now.pot[j])//如果i能把j倒满
{
next.pot[i]-=b[j]-now.pot[j];
next.pot[j]=b[j];
}
else//如果i不能把j倒满
{
next.pot[j]+=now.pot[i];
next.pot[i]=0;
}
if(!book[next.pot[0]][next.pot[1]][next.pot[2]])//如果这个状态没访问过
{
book[next.pot[0]][next.pot[1]][next.pot[2]]=1;
next.step+=1;
p.push(next);
}
}
}
}
return 0;
}
int main()
{
while(scanf("%d%d%d", &b[0], &b[1] ,&b[2]), b[0]||b[1]||b[2])//分别是可乐瓶体积和两个杯子的体积
{
half=b[0]/2;
if(b[0]%2==1)//可乐瓶体积是奇数怎样也无法评分
{
printf("NO\n");
continue;
}
memset(book, 0, sizeof(book));
if(bfs())
printf("%d\n", ans);
else
printf("NO\n");
}
return 0;
}