zoukankan      html  css  js  c++  java
  • (简单) POJ 3414 Pots,BFS+记录路径。

      Description

      You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

    1. FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
    2. DROP(i)      empty the pot i to the drain;
    3. POUR(i,j)    pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).

      Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

      就是对两个杯子进行操作了,装满,倒空,倒过去。两个水杯里的水表示一个状态,然后BFS就好,要记录路径的话,记住每一个的父亲,然后最后反着来一遍就好了。

    代码如下:

    #include<iostream>
    #include<cstring>
    #include<cmath>
    #include<queue>
    
    using namespace std;
    
    int A,B,C;
    int rem[105][105];
    int shorem[10005];
    
    struct state
    {
        int a,b;
        int type;
        int faa,fab;
        int num;
    
        state() {}
    };
    
    state sta[105][105];
    
    void showans(state *e)
    {
        int cou=0;
    
        while(e->a||e->b)
        {
            shorem[cou++]=e->type;
    
            e=&sta[e->faa][e->fab];
        }
    
        cout<<cou<<endl;
        for(int i=cou-1;i>=0;--i)
            switch(shorem[i])
            {
                case 1:
                    cout<<"FILL(1)
    ";
                    break;
                case 2:
                    cout<<"FILL(2)
    ";
                    break;
                case 3:
                    cout<<"DROP(1)
    ";
                    break;
                case 4:
                    cout<<"DROP(2)
    ";
                    break;
                case 5:
                    cout<<"POUR(1,2)
    ";
                    break;
                case 6:
                    cout<<"POUR(2,1)
    ";
                    break;
            }
    
    }
    
    void slove()
    {
        queue <state *> que;
        state *temp;
        int t1,t2,t3;
    
        sta[0][0].faa=sta[0][0].fab=-1;
        sta[0][0].type=0;
        sta[0][0].num=0;
        que.push(&sta[0][0]);
    
        while(!que.empty())
        {
            temp=que.front();
            que.pop();
    
            if(temp->a==C||temp->b==C)
            {
                showans(temp);
                return;
            }
    
            t1=temp->a;
            t2=temp->b;
            
            if(sta[A][t2].num==-1)
            {
                sta[A][t2].num=temp->num+1;
                sta[A][t2].faa=t1;
                sta[A][t2].fab=t2;
                sta[A][t2].type=1;
    
                que.push(&sta[A][t2]);
            }
            if(sta[t1][B].num==-1)
            {
                sta[t1][B].num=temp->num+1;
                sta[t1][B].faa=t1;
                sta[t1][B].fab=t2;
                sta[t1][B].type=2;
    
                que.push(&sta[t1][B]);
            }
            if(sta[0][t2].num==-1)
            {
                sta[0][t2].num=temp->num+1;
                sta[0][t2].faa=t1;
                sta[0][t2].fab=t2;
                sta[0][t2].type=3;
    
                que.push(&sta[0][t2]);
            }
            if(sta[t1][0].num==-1)
            {
                sta[t1][0].num=temp->num+1;
                sta[t1][0].faa=t1;
                sta[t1][0].fab=t2;
                sta[t1][0].type=4;
    
                que.push(&sta[t1][0]);
            }
            t3=min(t1,B-t2);
            if(sta[t1-t3][t2+t3].num==-1)
            {
                sta[t1-t3][t2+t3].num=temp->num+1;
                sta[t1-t3][t2+t3].faa=t1;
                sta[t1-t3][t2+t3].fab=t2;
                sta[t1-t3][t2+t3].type=5;
    
                que.push(&sta[t1-t3][t2+t3]);
            }
            t3=min(t2,A-t1);
            if(sta[t1+t3][t2-t3].num==-1)
            {
                sta[t1+t3][t2-t3].num=temp->num+1;
                sta[t1+t3][t2-t3].faa=t1;
                sta[t1+t3][t2-t3].fab=t2;
                sta[t1+t3][t2-t3].type=6;
    
                que.push(&sta[t1+t3][t2-t3]);
            }
        }
    
        cout<<"impossible
    ";
    }
    
    int main()
    {
        ios::sync_with_stdio(false);
    
        for(int i=0;i<=100;++i)
            for(int j=0;j<=100;++j)
            {
                sta[i][j].a=i;
                sta[i][j].b=j;
            }
    
        while(cin>>A>>B>>C)
        {
            for(int i=0;i<=100;++i)
                for(int j=0;j<=100;++j)
                    sta[i][j].num=-1;
    
            slove();
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    [hdu5312]数的拆分,数学推导
    [POJ1038]状压DP
    [hdu2112]最短路
    [hdu1532]最大流
    [hdu5256]LIS模型
    [hdu5255]枚举
    [hdu5254]BFS
    [hdu5270]按位统计,容斥,归并
    Elasticsearch在Centos 7上的安装与配置
    手动安装java1.8
  • 原文地址:https://www.cnblogs.com/whywhy/p/4229897.html
Copyright © 2011-2022 走看看