非常可乐
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
中文题题意就是题面意思了偶,,,,,,,,,,,,,,
思路:这三个杯子的操作无非就是三种了,从S杯子倒到N杯子,从S杯子倒到M杯子,从N杯子倒到M杯子,反过来还有三种操作,所以一种有六种操作,这样的话就很简单明了了,简单的BFS模拟就行。
直接上代码,啊哈哈哈哈哈哈哈。。。
#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
int s , a , b ;
int ans ,vis[101][101][101];
struct node{
int x , y , z;
int step;
};
int bfs(){
if(s % 2) return -1;
queue<node>q;
node now , next ,st;
memset(vis , 0 ,sizeof(vis));
int s1 = s / 2;
st.step = 0;
st.x = s;
st.y = 0;
st.z = 0;
st.step = 0;
q.push(st);
vis[st.x][st.y][st.z] = 1;
while(!q.empty()){
now = q.front();
if((now.x == s1 && now.y == s1)|| (now.x == s1 && now.z == s1 )||(now.y == s1 && now.z == s1)){
return now.step;
}
//x->y
if(now.x && a - now.y > 0){
if(now.x > a - now.y){
next.x = now.x - (a - now.y);
next.y = a;
next.z = now.z;
}else{
next.x = 0;
next.y = now.y + now.z;
next.z = now.z;
}
if(!vis[next.x][next.y][next.z]){
vis[next.x][next.y][next.z] = 1;
next.step = now.step + 1;
q.push(next);
}
}
//y->x
if(now.y && s - now.x > 0){
if(now.y > s - now.x){
next.x = s;
next.y = now.y - (s - now.x);
next.z = now.z;
}else{
next.x = now.x + now.y;
next.y = 0;
next.z = now.z;
}
if(!vis[next.x][next.y][next.z]){
vis[next.x][next.y][next.z] = 1;
next.step = now.step + 1;
q.push(next);
}
}
//x->z
if(now.x && b - now.z > 0){
if(now.x > b - now.z){
next.x = now.x - (b - now.z);
next.y = now.y;
next.z = b;
}else{
next.x = 0;
next.y = now.y;
next.z = now.z + now.x;
}
if(!vis[next.x][next.y][next.z]){
vis[next.x][next.y][next.z] = 1;
next.step = now.step + 1;
q.push(next);
}
}
//z->x
if(now.z && s - now.x > 0){
if(now.z > s - now.x){
next.x = s;
next.y = now.y;
next.z = now.z - (s - now.x);
}else{
next.x = now.x + now.z;
next.y = now.y;
next.z = 0;
}
if(!vis[next.x][next.y][next.z]){
vis[next.x][next.y][next.z] = 1;
next.step = now.step + 1;
q.push(next);
}
}
//y->z
if(now.y && b - now.z > 0){
if(now.y > b - now.z){
next.x = now.x;
next.y = now.y - (b - now.z);
next.z = b;
}else{
next.x = now.x;
next.y = 0;
next.z = now.z + now.y;
}
if(!vis[next.x][next.y][next.z]){
vis[next.x][next.y][next.z] = 1;
next.step = now.step + 1;
q.push(next);
}
}
//z->y
if(now.z && a - now.y > 0){
if(now.z > a - now.y){
next.x = now.x;
next.y = a;
next.z = now.z - (a - now.y);
}else{
next.x = now.x;
next.y = now.y + now.z;
next.z = 0;
}
if(!vis[next.x][next.y][next.z]){
vis[next.x][next.y][next.z] = 1;
next.step = now.step + 1;
q.push(next);
}
}
q.pop();
}
return -1;
}
int main(){
while(cin >> s >>a >>b){
if(!a & !b &!s)break;
if(a < b) swap(a , b);
int ans = bfs();
if(ans == -1) cout << "NO\n";
else cout << ans <<endl;
}
}