本题是广搜的题,一共有6种倒法;
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=100+10;
int v[maxn][maxn][maxn];
int d[6][2]={{0,1},{0,2},{1,2},{2,1},{1,0},{2,0}};
int full[3];
struct node{
int cup[3];
int step;
};
void pour(int &a,int &b,int Fullb)
{
if(a+b<=Fullb)//如果a+b还小于b的全部的话
{
b+=a,a=0;//将水倒入a中
}
else
{
a-=(Fullb-b);//将b瓶倒满
b=Fullb;
}
}
int ok(node s)
{
if(s.cup[0]+s.cup[1]==full[0]&&s.cup[0]==s.cup[1])
return 1;
if(s.cup[0]+s.cup[2]==full[0]&&s.cup[0]==s.cup[2])
return 1;
if(s.cup[1]+s.cup[2]==full[0]&&s.cup[1]==s.cup[2])
return 1;
return 0;
}
int bfs(node s)
{
queue<node> q;
q.push(s);
while(!q.empty())
{
s=q.front();q.pop();
if(ok(s))
{
return s.step;
}
for(int i=0;i<6;i++)
{
node th=s;
pour(th.cup[d[i][0]],th.cup[d[i][1]],full[d[i][1]]);
if(v[th.cup[0]][th.cup[1]][th.cup[2]]==0)
{
v[th.cup[0]][th.cup[1]][th.cup[2]]=1;
th.step++;
q.push(th);
}
}
}
return -1;
}
int main()
{
freopen("M.txt","r",stdin);
while(scanf("%d %d %d",&full[0],&full[1],&full[2]),full[0]+full[1]+full[2])
{
memset(v,0,sizeof(v));
node s;
s.cup[0]=full[0];
s.cup[1]=s.cup[2]=s.step=0;
int ans=bfs(s);
if(ans==-1)
printf("NO\n");
else
printf("%d\n",ans);
}
return 0;
}