HDU 1495 非常可乐 BFS搜索

时间:2022-04-08 17:08:06

题意:有个为三个杯子(杯子没有刻度),体积为s,n,m,s=m+n,

刚开始只有体积为s的杯子装满可乐,可以互相倒,问你最少的次数使可乐均分,如果没有结果,输出-1;

分析:直接互相倒就完了,BFS模拟

注:写的很丑

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <queue>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn=+;
const int INF=0x3f3f3f3f;
int a[maxn][maxn][maxn];
struct asd
{
int x,y,z;
} o,t;
queue<asd>q;
int main()
{
int s,n,m;
while(~scanf("%d%d%d",&s,&n,&m),s)
{
if(s%)
{
printf("NO\n");
continue;
}
memset(a,-,sizeof(a));
a[s][][]=;
o.x=s,o.y=,o.z=;
while(!q.empty())q.pop();
q.push(o);
int ans=-;
while(!q.empty())
{
o=q.front();
q.pop();
int cnt=;
if(o.x==s/)cnt++;
if(o.y==s/)cnt++;
if(o.z==s/)cnt++;
if(cnt==)
{
ans=a[o.x][o.y][o.z];
break;
}
if(o.x)
{
if(o.y<n)
{
int k=n-o.y;
if(o.x<=k)t.y=o.y+o.x,t.x=;
else t.y=n,t.x=o.x-k;
t.z=o.z;
if(a[t.x][t.y][t.z]==-)
a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+,q.push(t);
}
if(o.z<m)
{
int k=m-o.z;
if(o.x<=k)t.z=o.z+o.x,t.x=;
else t.z=m,t.x=o.x-k;
t.y=o.y;
if(a[t.x][t.y][t.z]==-)
a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+,q.push(t);
}
}
if(o.y)
{
if(o.x<s)
{
int k=s-o.x;
if(o.y<=k)t.x=o.x+o.y,t.y=;
else t.x=s,t.y=o.y-k;
t.z=o.z;
if(a[t.x][t.y][t.z]==-)
a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+,q.push(t);
}
if(o.z<m)
{
int k=m-o.z;
if(o.y<=k)t.z=o.z+o.y,t.y=;
else t.z=m,t.y=o.y-k;
t.x=o.x;
if(a[t.x][t.y][t.z]==-)
a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+,q.push(t);
}
}
if(o.z)
{
if(o.y<n)
{
int k=n-o.y;
if(o.z<=k)t.y=o.y+o.z,t.z=;
else t.y=n,t.z=o.z-k;
t.x=o.x;
if(a[t.x][t.y][t.z]==-)
a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+,q.push(t);
}
if(o.x<s)
{
int k=s-o.x;
if(o.z<=k)t.x=o.z+o.x,t.z=;
else t.x=s,t.z=o.z-k;
t.y=o.y;
if(a[t.x][t.y][t.z]==-)
a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+,q.push(t);
}
}
}
if(ans==-)printf("NO\n");
else printf("%d\n",ans);
}
return ;
}