zoukankan      html  css  js  c++  java
  • POJ 3414 Pots

    简单$bfs$。

    仔细一些就能过。

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<set>
    #include<queue>
    #include<stack>
    #include<iostream>
    using namespace std;
    typedef long long LL;
    const double pi=acos(-1.0),eps=1e-8;
    void File()
    {
        freopen("D:\in.txt","r",stdin);
        freopen("D:\out.txt","w",stdout);
    }
    template <class T>
    inline void read(T &x)
    {
        char c = getchar(); x = 0;while(!isdigit(c)) c = getchar();
        while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar();  }
    }
    
    const int INF=0x7FFFFFFF;
    const int maxn=110;
    struct X
    {
        int a,b;
        int op;
        int fa;
    }s[1000000];
    int A,B,C,t,sz;
    int f[maxn][maxn];
    
    void bfs()
    {
        queue<int>q; sz=0;
    
        for(int i=0;i<=A;i++) for(int j=0;j<=B;j++) f[i][j]=INF;
        f[0][0]=0;
    
        s[sz].a=0; s[sz].b=0; s[sz].fa=-1; s[sz].op=-1;
        q.push(0);
    
        t=-1;
        while(!q.empty())
        {
    
            int h=q.front(); q.pop();
            if(s[h].a==C||s[h].b==C) { t=h; break; }
    
            //FILL(a) 1
            if(s[h].a!=A)
            {
                if(f[s[h].a][s[h].b]+1<f[A][s[h].b])
                {
                    f[A][s[h].b]=f[s[h].a][s[h].b]+1;
                    sz++;
                    s[sz].a=A;
                    s[sz].b=s[h].b;
                    s[sz].fa=h;
                    s[sz].op=1;
                    q.push(sz);
                }
            }
    
            //FILL(b) 4
            if(s[h].b!=B)
            {
                if(f[s[h].a][s[h].b]+1<f[s[h].a][B])
                {
                    f[s[h].a][B]=f[s[h].a][s[h].b]+1;
                    sz++;
                    s[sz].a=s[h].a;
                    s[sz].b=B;
                    s[sz].fa=h;
                    s[sz].op=4;
                    q.push(sz);
                }
            }
    
            //DROP(a) 2
            if(s[h].a!=0)
            {
                if(f[s[h].a][s[h].b]+1<f[0][s[h].b])
                {
                    f[0][s[h].b]=f[s[h].a][s[h].b]+1;
                    sz++;
                    s[sz].a=0;
                    s[sz].b=s[h].b;
                    s[sz].fa=h;
                    s[sz].op=2;
                    q.push(sz);
                }
            }
    
            //DROP(b) 5
            if(s[h].b!=0)
            {
                if(f[s[h].a][s[h].b]+1<f[s[h].a][0])
                {
                    f[s[h].a][0]=f[s[h].a][s[h].b]+1;
                    sz++;
                    s[sz].a=s[h].a;
                    s[sz].b=0;
                    s[sz].fa=h;
                    s[sz].op=5;
                    q.push(sz);
                }
            }
    
            //POUR(a,b) 3
            if(s[h].a!=0&&s[h].b!=B)
            {
                int f1,f2;
                if(s[h].a>=B-s[h].b) f1=s[h].a-(B-s[h].b), f2=B;
                else f1=0, f2=s[h].b+s[h].a;
    
                if(f[s[h].a][s[h].b]+1<f[f1][f2])
                {
                    f[f1][f2]=f[s[h].a][s[h].b]+1;
                    sz++;
                    s[sz].a=f1;
                    s[sz].b=f2;
                    s[sz].fa=h;
                    s[sz].op=3;
                    q.push(sz);
                }
            }
    
            //POUR(b,a) 6
            if(s[h].b!=0&&s[h].a!=A)
            {
                int f1,f2;
                if(s[h].b>=A-s[h].a) f1=A, f2=s[h].b-(A-s[h].a);
                else f1=s[h].a+s[h].b, f2=0;
    
                if(f[s[h].a][s[h].b]+1<f[f1][f2])
                {
                    f[f1][f2]=f[s[h].a][s[h].b]+1;
                    sz++;
                    s[sz].a=f1;
                    s[sz].b=f2;
                    s[sz].fa=h;
                    s[sz].op=6;
                    q.push(sz);
                }
            }
        }
    }
    
    void out(int op)
    {
        if(op==-1) return ;
        if(op==1) printf("FILL(1)
    ");
        if(op==2) printf("DROP(1)
    ");
        if(op==3) printf("POUR(1,2)
    ");
        if(op==4) printf("FILL(2)
    ");
        if(op==5) printf("DROP(2)
    ");
        if(op==6) printf("POUR(2,1)
    ");
    
    }
    int main()
    {
        while(~scanf("%d%d%d",&A,&B,&C))
        {
            bfs();
            if(t==-1) printf("impossible
    ");
            else
            {
                printf("%d
    ",f[s[t].a][s[t].b]);
    
                int now=t; stack<int>st;
                while(now!=-1) st.push(now), now=s[now].fa;
    
                while(!st.empty())
                {
                    out(s[st.top()].op);
                    st.pop();
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    每日优鲜三面:在Spring Cloud实战中,如何用服务链路追踪Sleuth?
    一文就能看懂的Nginx操作详解,你还在查漏补缺吗!
    火花思维三面:说说Redis分布式锁是如何实现的!
    【秋招必备】Dubbo面试题(2021最新版)
    【秋招必备】Elasticsearch面试题(2021最新版)
    熬了一通宵!你竟然都没有弄懂陌陌面试官问的Java虚拟机内存?
    react-native-vector-icons 使用记录
    git
    在iOS项目中嵌入RN代码
    UITabBar 图标上下跳动
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5831582.html
Copyright © 2011-2022 走看看