思路:
可以相互倒可乐,所以有6中方式。将每一种方式列举出来就行。但是判断是否均分时需要让容量小的杯子中没有可乐,即可乐瓶中的可乐 和 容量大的杯子中的可乐体积相同。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
int vis[110][110];
int s,n,m,ans,k;
struct node
{
int sum; //在这个状态下可乐瓶中剩余的可乐
int x,y; //两个杯子中可乐的容量
int step;
};
void bfs(int x,int y)
{
queue<node> q;
node w,h;
w.x = x;
w.y = y;
w.step = 0;
w.sum = s;
vis[w.x][w.y] = 1;
q.push(w);
while(!q.empty())
{
w = q.front();
q.pop();
for(int i=1;i<=6;i++)
{
if(i==1) //把s倒入n中
{
if(w.sum > n-w.x) //如果s中的水比n杯剩余的容量大
{
h.x = n;
h.sum = w.sum-n;
}
else //s中的水全部倒入n中
{
h.x = w.x+w.sum;
h.sum = 0;
}
h.y = w.y;
}
else if(i==2) //把s倒入m中
{
if(w.sum > m-w.y) //s中的水比m杯空余的容量大
{
h.y = m;
h.sum = w.sum-(m-w.y);
}
else
{
h.y = w.y+w.sum;
h.sum = 0;
}
h.x = w.x;
}
else if(i==3) //n->s
{
if(w.x > s-w.sum)
{
h.sum = s;
h.x = w.x-(s-w.sum);
}
else
{
h.x = 0;
h.sum = w.sum+w.x;
}
h.y = w.y;
}
else if(i==4) //n->m
{
if(w.x > m-w.y)
{
h.x = w.x-(m-w.y);
h.y = m;
}
else
{
h.x = 0;
h.y = w.y+w.x;
}
h.sum = w.sum;
}
else if(i==5) //m->s
{
if(w.y > s-w.sum)
{
h.y = w.y-(s-w.sum);
h.sum = s;
}
else
{
h.y = 0;
h.sum = w.sum+w.y;
}
h.x = w.x;
}
else if(i==6) //m->n
{
if(w.y > n-w.x)
{
h.x = n;
h.y = w.y-(n-w.x);
}
else
{
h.x = w.x+w.y;
w.y = 0;
}
h.sum = w.sum;
}
if(vis[h.x][h.y])
continue;
vis[h.x][h.y] = 1;
h.step = w.step+1;
if(h.x==k && h.sum==k) //那个小的杯子需要为空
{
ans = h.step;
return ;
}
q.push(h);
}
}
}
int main()
{
while(~scanf("%d%d%d",&s,&n,&m) && s+n+m)
{
ans = 0;
memset(vis,0,sizeof(vis));
if(s%2)
printf("NO\n");
else
{
if(n<m) // n为容量大的杯子
swap(n,m);
k = s/2;
bfs(0,0);
if(ans)
printf("%d\n",ans);
else
printf("NO\n");
}
}
return 0;
}