大家一定觉的运动以后喝可乐是一件很惬意的事情,但是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
题解:
每一步一共有六种状态,分别是s->n, s->m, n->s, n->m, m->s, m->s;
将每一步的六种状态放入队列即可~要记得路径标记,不然会死循环的~~
#include<bits/stdc++.h>
using namespace std;
int s, n, m;
bool vis[110][110][110];
struct bu{
int x, y, z, step;
};
int bfs(int c){
queue<bu> q;
bu a, ne;
int t;
a.x = s; a.y = a.z = a.step = 0;
q.push(a);
vis[a.x][a.y][a.z] = 1;
while(!q.empty()){
a = q.front();
q.pop();
if(a.x == c && a.y == c){
//cout << "a.step = " <<a.step << endl;
return a.step;
}
if(a.x && a.y != n){//s - > n
t = n - a.y;
if(t > a.x){
ne.y = a.y + a.x;
ne.x = 0;
}
else{
ne.y = n;
ne.x = a.x - t;
}
ne.z = a.z;
ne.step = a.step + 1;
if(!vis[ne.x][ne.y][ne.z]){
q.push(ne);
// cout << "ne.x = " << ne.x << " ne.y = " << ne.y << " ne.z = " << ne.z << " ne.step = " << ne.step << endl;
vis[ne.x][ne.y][ne.z] = 1;
}
}
if(a.x && a.z != m){//s -> b
t = m - a.z;
if(t > a.x){
ne.z = a.z + a.x;
ne.x = 0;
}
else{
ne.z = m;
ne.x = a.x - t;
}
ne.y = a.y;
ne.step = a.step + 1;
if(!vis[ne.x][ne.y][ne.z]){
q.push(ne);
// cout << "ne.x = " << ne.x << " ne.y = " << ne.y << " ne.z = " << ne.z << " ne.step = " << ne.step << endl;
vis[ne.x][ne.y][ne.z] = 1;
}
}
if(a.y && a.z != m){//a -> b
t = m - a.z;
if(t > a.y){
ne.z = a.z + a.y;
ne.y = 0;
}
else{
ne.z = m;
ne.y = a.y - t;
}
ne.x = a.x;
ne.step = a.step + 1;
if(!vis[ne.x][ne.y][ne.z]){
q.push(ne);
// cout << "ne.x = " << ne.x << " ne.y = " << ne.y << " ne.z = " << ne.z << " ne.step = " << ne.step << endl;
vis[ne.x][ne.y][ne.z] = 1;
}
}
if(a.z && a.y != n){//b -> a
t = n - a.y;
if(t > a.z){
ne.y = a.z + a.y;
ne.z = 0;
}
else{
ne.y = n;
ne.z = a.z - t;
}
ne.x = a.x;
ne.step = a.step + 1;
if(!vis[ne.x][ne.y][ne.z]){
q.push(ne);
// cout << "ne.x = " << ne.x << " ne.y = " << ne.y << " ne.z = " << ne.z << " ne.step = " << ne.step << endl;
vis[ne.x][ne.y][ne.z] = 1;
}
}
if(a.y && a.x != s){//a -> s
t = s - a.x;
if(t > a.y){
ne.x = a.x + a.y;
ne.y = 0;
}
else{
ne.x = s;
ne.y = a.y - t;
}
ne.z = a.z;
ne.step = a.step + 1;
if(!vis[ne.x][ne.y][ne.z]){
q.push(ne);
// cout << "ne.x = " << ne.x << " ne.y = " << ne.y << " ne.z = " << ne.z << " ne.step = " << ne.step << endl;
vis[ne.x][ne.y][ne.z] = 1;
}
}
if(a.y && a.x != s){//b -> s
t = s - a.x;
if(t > a.z){
ne.x = a.z + a.x;
ne.z = 0;
}
else{
ne.x = s;
ne.z = a.z - t;
}
ne.y = a.y;
ne.step = a.step + 1;
if(!vis[ne.x][ne.y][ne.z]){
q.push(ne);
// cout << "ne.x = " << ne.x << " ne.y = " << ne.y << " ne.z = " << ne.z << " ne.step = " << ne.step << endl;
vis[ne.x][ne.y][ne.z] = 1;
}
}
}
return 0;
}
int main(){
while(scanf("%d%d%d", &s, &n, &m)!=EOF && s!=0 || n!=0 || m!=0){
if(n < m) swap(n, m);
memset(vis, 0, sizeof(vis));
if(s%2){
printf("NO\n");
}
else{
int sum = bfs(s/2);
if(sum == 0)
printf("NO\n");
else{
printf("%d\n", sum);
}
}
}
return 0;
}