HDU 1495 非常可乐 BFS搜索

题意:有个为三个杯子(杯子没有刻度),体积为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=100+5;
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%2)
        {
            printf("NO\n");
            continue;
        }
        memset(a,-1,sizeof(a));
        a[s][0][0]=0;
        o.x=s,o.y=0,o.z=0;
        while(!q.empty())q.pop();
        q.push(o);
        int ans=-1;
        while(!q.empty())
        {
            o=q.front();
            q.pop();
            int cnt=0;
            if(o.x==s/2)cnt++;
            if(o.y==s/2)cnt++;
            if(o.z==s/2)cnt++;
            if(cnt==2)
            {
                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=0;
                    else t.y=n,t.x=o.x-k;
                    t.z=o.z;
                    if(a[t.x][t.y][t.z]==-1)
                        a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+1,q.push(t);
                }
                if(o.z<m)
                {
                    int k=m-o.z;
                    if(o.x<=k)t.z=o.z+o.x,t.x=0;
                    else t.z=m,t.x=o.x-k;
                    t.y=o.y;
                    if(a[t.x][t.y][t.z]==-1)
                        a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+1,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=0;
                    else t.x=s,t.y=o.y-k;
                    t.z=o.z;
                    if(a[t.x][t.y][t.z]==-1)
                        a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+1,q.push(t);
                }
                if(o.z<m)
                {
                    int k=m-o.z;
                    if(o.y<=k)t.z=o.z+o.y,t.y=0;
                    else t.z=m,t.y=o.y-k;
                    t.x=o.x;
                    if(a[t.x][t.y][t.z]==-1)
                        a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+1,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=0;
                    else t.y=n,t.z=o.z-k;
                    t.x=o.x;
                    if(a[t.x][t.y][t.z]==-1)
                        a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+1,q.push(t);
                }
                if(o.x<s)
                {
                    int k=s-o.x;
                    if(o.z<=k)t.x=o.z+o.x,t.z=0;
                    else t.x=s,t.z=o.z-k;
                    t.y=o.y;
                    if(a[t.x][t.y][t.z]==-1)
                        a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+1,q.push(t);
                }
            }
        }
        if(ans==-1)printf("NO\n");
        else printf("%d\n",ans);
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/shuguangzw/p/5173036.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值