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
  • 相关阅读:
    VB中String的用法及原理
    SQL中的left outer join,inner join,right outer join用法详解
    SqlServer,Oracle 常用函数比较
    sql 使用视图的好处
    修改数据库的兼容级别
    sql server2008修改登录名下的默认架构名
    SQL事务回滚 ADO BeginTrans, CommitTran 以及 RollbackTrans 方法
    sql事务(Transaction)用法介绍及回滚实例
    SQL Server更新表(用一张表的数据更新另一张表的数据)
    VB如何连接SQL Server数据库
  • 原文地址:https://www.cnblogs.com/shuguangzw/p/5173036.html
Copyright © 2011-2022 走看看