大家一定觉的运动以后喝可乐是一件很惬意的事情,但是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<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int Max = 101;
//一共三个杯子,知道总和,所以只需要前两个杯子的状态,就可以进行判断了
int s,n,m;
int vis[Max][Max];
typedef struct node {
int x,y,z;//x代表可乐罐,y,z代表两个杯子
int step;
}Node;
//模拟的顺序是:x to y,x to z,y to x,y to z,z to x,z to y
int bfs() {
memset(vis,0,sizeof(vis));
Node now,next;
queue<Node> q;
now.x = s,now.y = 0,now.z = 0,now.step = 0;
vis[now.x][now.y] = 1;
q.push(now);
while(q.size()) {
now = q.front();
q.pop();
if((now.x == now.y && now.z == 0) || (now.x == now.z && now.y == 0) || (now.y == now.z && now.x == 0))
return now.step;
for(int i = 0;i < 6;i++) {
switch(i) {
case 0:
if(now.x != 0 && now.y != n)
next.x = now.x - (n - now.y),next.y = n,next.z = now.z;
break;
case 1:
if(now.x != 0 && now.z != m)
next.x = now.x - (m - now.z),next.y = now.y,next.z = m;
break;
case 2:
if(now.y != 0)
next.x = now.x + now.y,next.y = 0,next.z = now.z;
break;
case 3:
if(now.y != 0 && now.z != m) {
if(now.y + now.z > m)
next.x = now.x,next.y = now.y - (m - now.z),next.z = m;
else
next.x = now.x,next.y = 0,next.z = now.z + now.y;
}
break;
case 4:
if(now.z != 0)
next.x = now.x + now.z,next.y = now.y,next.z = 0;
break;
case 5:
if(now.z != 0 && now.y != n) {
if(now.y + now.z > n)
next.x = now.x,next.y = n,next.z = now.z - (n - now.y);
else
next.x = now.x,next.y = now.y + now.z,next.z = 0;
}
break;
}
if(vis[next.x][next.y])
continue;
vis[next.x][next.y] = 1;
next.step=now.step + 1;
q.push(next);
}
}
return -1;
}
int main() {
while(~scanf("%d %d %d",&s,&n,&m)) {
if(!s && !n && !m)
break;
memset(vis,0,sizeof(vis));
int ans = bfs();
if(ans == -1)
printf("NO\n");
else
printf("%d\n",ans);
}
return 0;
}