zoukankan      html  css  js  c++  java
  • 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
    ");
                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
    ");
            else printf("%d
    ",ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    js 支持 超大上G,多附件上传
    c#.net 支持 超大上G,多附件上传
    ASP.NET 支持 超大上G,多附件上传
    C# 支持 超大上G,多附件上传
    HTML编辑器可以如何直接复制word的图文内容到编辑器中?
    在线编辑器可以如何直接复制word的图文内容到编辑器中?
    2018-2-13-WPF-DelegateCommand-出现Specified-cast-is-not-valid
    2018-8-10-C#-判断文件编码
    2018-8-10-C#-判断文件编码
    2019-1-29-C#-Task.Run-和-Task.Factory.StartNew-区别
  • 原文地址:https://www.cnblogs.com/shuguangzw/p/5173036.html
Copyright © 2011-2022 走看看