zoukankan      html  css  js  c++  java
  • POJ

    Pots

    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 ≤ ≤ 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 jis 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.

    Input

    On the first and only line are the numbers AB, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

    Output

    The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

    Sample Input

    3 5 4

    Sample Output

    6
    FILL(2)
    POUR(2,1)
    DROP(1)
    POUR(2,1)
    FILL(2)
    POUR(2,1)


    题意:倒水问题。给你两个标有容量的空杯子,求最少需要多少步能够倒出含指定容量的水。并输出倒水步骤(升级版)。
    思路:看到最少步数和输出具体步骤就应该想到BFS。用二维数组b来记录两个杯子的容量。分六步,倒满1、倒满2、倒空1、倒空2、1倒入2、2倒入1。倒入水时要保证不会溢出,所以倒入要分全部倒入和部分倒入两种情况。步骤较多,所以代码啰嗦些。。

    #include<stdio.h>
    #include<queue>
    using namespace std;
    
    int b[105][105],re[10005];
    struct Node{
        int x,y,s,c,f,d;
    }node[10005];
    
    int main()
    {
        int x,y,z,c,f,i;
        queue<Node> q;
        scanf("%d%d%d",&x,&y,&z);
        if(z==0) printf("0
    ");   //一定注意。。
        else{
        b[0][0]=1;
        node[1].x=0;
        node[1].y=0;
        node[1].s=0;
        node[1].c=1;
        node[1].f=1;
        node[1].d=0;
        q.push(node[1]);
        c=1;f=0;
        while(q.size()){
            for(i=1;i<=6;i++){
                if(i==1&&q.front().x<x&&b[x][q.front().y]==0){
                    b[x][q.front().y]=1;
                    node[++c].x=x;
                    node[c].y=q.front().y;
                    node[c].s=q.front().s+1;
                    node[c].c=c;
                    node[c].f=q.front().c;
                    node[c].d=1;
                    if(node[c].x==z||node[c].y==z){
                        f=node[c].s;
                        break;
                    }
                    q.push(node[c]);
                }
                else if(i==2&&q.front().y<y&&b[q.front().x][y]==0){
                    b[q.front().x][y]=1;
                    node[++c].x=q.front().x;
                    node[c].y=y;
                    node[c].s=q.front().s+1;
                    node[c].c=c;
                    node[c].f=q.front().c;
                    node[c].d=2;
                    if(node[c].x==z||node[c].y==z){
                        f=node[c].s;
                        break;
                    }
                    q.push(node[c]);
                }
                else if(i==3&&q.front().x>0&&b[0][q.front().y]==0){
                    b[0][q.front().y]=1;
                    node[++c].x=0;
                    node[c].y=q.front().y;
                    node[c].s=q.front().s+1;
                    node[c].c=c;
                    node[c].f=q.front().c;
                    node[c].d=3;
                    if(node[c].x==z||node[c].y==z){
                        f=node[c].s;
                        break;
                    }
                    q.push(node[c]);
                }
                else if(i==4&&q.front().y>0&&b[q.front().x][0]==0){
                    b[q.front().x][0]=1;
                    node[++c].x=q.front().x;
                    node[c].y=0;
                    node[c].s=q.front().s+1;
                    node[c].c=c;
                    node[c].f=q.front().c;
                    node[c].d=4;
                    if(node[c].x==z||node[c].y==z){
                        f=node[c].s;
                        break;
                    }
                    q.push(node[c]);
                }
                else if(i==5&&q.front().x>0&&q.front().y<y){
                    int tx=q.front().x<y-q.front().y?0:q.front().x+q.front().y-y;
                    int ty=q.front().x<y-q.front().y?q.front().x+q.front().y:y;
                    if(b[tx][ty]==0){
                        b[tx][ty]=1;
                        node[++c].x=tx;
                        node[c].y=ty;
                        node[c].s=q.front().s+1;
                        node[c].c=c;
                        node[c].f=q.front().c;
                        node[c].d=5;
                        if(node[c].x==z||node[c].y==z){
                            f=node[c].s;
                            break;
                        }
                        q.push(node[c]);
                    }
                }
                else if(i==6&&q.front().x<x&&q.front().y>0){
                    int tx=x-q.front().x<q.front().y?x:q.front().x+q.front().y;
                    int ty=x-q.front().x<q.front().y?q.front().x+q.front().y-x:0;
                    if(b[tx][ty]==0){
                        b[tx][ty]=1;
                        node[++c].x=tx;
                        node[c].y=ty;
                        node[c].s=q.front().s+1;
                        node[c].c=c;
                        node[c].f=q.front().c;
                        node[c].d=6;
                        if(node[c].x==z||node[c].y==z){
                            f=node[c].s;
                            break;
                        }
                        q.push(node[c]);
                    }
                }
            }
            if(f!=0) break;
            q.pop();
        }
        if(f==0) printf("impossible
    ");
        else{
            printf("%d
    ",f);
            for(i=1;i<=f;i++){
                re[i]=node[c].d;
                c=node[c].f;
            }
            for(i=f;i>=1;i--){
                if(re[i]==1) printf("FILL(1)
    ");
                else if(re[i]==2) printf("FILL(2)
    ");
                else if(re[i]==3) printf("DROP(1)
    ");
                else if(re[i]==4) printf("DROP(2)
    ");
                else if(re[i]==5) printf("POUR(1,2)
    ");
                else if(re[i]==6) printf("POUR(2,1)
    ");
            }
        }
        }
        return 0;
    } 
  • 相关阅读:
    立则存
    如何在UI控件上绑定一个变量
    欢迎大家使用www.eugene.org.cn访问我的blog
    属性与字段变量的区别
    在C#中联合Excel的Com编程入门
    我和我的土匪奶奶 全集 全 下载 txt
    大菲波数
    Hat's Fibonacci
    Fibonacci 取余,直接做超时
    How Many Fibs? 字符串转换处理
  • 原文地址:https://www.cnblogs.com/yzm10/p/7242590.html
Copyright © 2011-2022 走看看