题目大意:就是给你2个杯子和一瓶水,问如何将这瓶水二等分,并且输出最少的倒水次数
解题大意:最终的结果一定是最大的杯子和第二大的杯子各放一半水。然后每一次三个瓶子的装水情况下一步如何倒水一共有六种倒法,然后bfs模拟这个倒水,记得用一个数组纪录倒水后各个瓶子的装水情况。
AC代码如下:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
int a,b,c,step;
};
int a,b,c,vis[110][110];
queue<node> Q;
int bfs()
{
node now,xia;
int i;
while(!Q.empty())
Q.pop();
now.a=a;
now.b=0;
now.c=0;
now.step=0;
vis[now.a][now.b]=1;
Q.push(now);
while(!Q.empty())
{
now=Q.front();
Q.pop();
if(now.a==a/2 && now.b==a/2)
return now.step;
if(now.a && now.b<b)
{
if(now.a>=b-now.b)
{
xia.a=now.a-(b-now.b);
xia.b=b;
xia.c=now.c;
}
else
{
xia.a=0;
xia.b=now.b+now.a;
xia.c=now.c;
}
xia.step=now.step+1;
if(!vis[xia.a][xia.b])
{
vis[xia.a][xia.b]=1;
Q.push(xia);
}
}
if(now.a && now.c<c)
{
if(now.a>=c-now.c)
{
xia.a=now.a-(c-now.c);
xia.b=now.b;
xia.c=c;
}
else
{
xia.a=0;
xia.b=now.b;
xia.c=now.c+now.a;
}
xia.step=now.step+1;
if(!vis[xia.a][xia.b])
{
vis[xia.a][xia.b]=1;
Q.push(xia);
}
}
if(now.b && now.c<c)
{
if(now.b>=c-now.c)
{
xia.a=now.a;
xia.b=now.b-(c-now.c);
xia.c=c;
}
else
{
xia.a=now.a;
xia.b=0;
xia.c=now.c+now.b;
}
xia.step=now.step+1;
if(!vis[xia.a][xia.b])
{
vis[xia.a][xia.b]=1;
Q.push(xia);
}
}
if(now.b && now.a<a)
{
if(now.b>=a-now.a)
{
xia.a=a;
xia.b=now.b-(a-now.a);
xia.c=now.c;
}
else
{
xia.a=now.a+now.b;
xia.b=0;
xia.c=now.c;
}
xia.step=now.step+1;
if(!vis[xia.a][xia.b])
{
vis[xia.a][xia.b]=1;
Q.push(xia);
}
}
if(now.c && now.a<a)
{
if(now.c>=a-now.a)
{
xia.a=a;
xia.b=now.b;
xia.c=now.c-(a-now.a);
}
else
{
xia.a=now.a+now.c;
xia.b=now.b;
xia.c=0;
}
xia.step=now.step+1;
if(!vis[xia.a][xia.b])
{
vis[xia.a][xia.b]=1;
Q.push(xia);
}
}
if(now.c && now.b<b)
{
if(now.c>=b-now.b)
{
xia.a=now.a;
xia.b=b;
xia.c=now.c-(b-now.b);
}
else
{
xia.a=now.a;
xia.b=now.b+now.c;
xia.c=0;
}
xia.step=now.step+1;
if(!vis[xia.a][xia.b])
{
vis[xia.a][xia.b]=1;
Q.push(xia);
}
}
}
return 0;
}
int main()
{
int cnt;
while(cin>>a>>b>>c && a+b+c)
{
memset(vis,0,sizeof(vis));
if(a%2)
{
cout<<"NO"<<endl;
continue;
}
if(c>b)
cnt=c,c=b,b=cnt;
cnt=bfs();
if(!cnt)
cout<<"NO"<<endl;
else
cout<<cnt<<endl;
}
return 0;
}