zoukankan      html  css  js  c++  java
  • 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

    设(i,j)为瓶1与瓶2在某一时刻的容量,那么从这点出发,可以到达的点有:

    (A, j) : FILL(1)

    (i, B) : FILL(2)

    (0, j): DROP(1)

    (i, 0): DROP(2)

    (i+j, 0) or (A, j-A+i) : POUR(2,1)

    (0, i+j) or (i-B+j, B) : POUR(1,2)

    判断这些点的访问状态,然后广搜下去,另外,要求出从源顶点到这些点的最短路径

    当i=C 或 j=C时,就找到了

    同时,为了直到到达(i, j)这一点时,所做的操作,我还设了一个operation变量,来记录达到一点所做的操作,供最后输出

    在题中,我用的是一维数组,因此(i, j)变为(i*(B+1))+j

    print_path函数,实质上输出了从源顶点到目标点的最短路径上的每个点的操作。

     



    原文:https://blog.csdn.net/kindlucy/article/details/5827794

    #include <iostream>
    #include <queue>
    using namespace std;
    
    int *color;
    int *pi;
    int *d;
    int *operation; //保存操作
    
    int BFS(int A,int B,int C)
    {
        if(C==0)
            return 0;
        int num[2]={A,B};
        int temp=(A+1)*(B+1);
        color=new int[temp];
        pi=new int[temp];
        d=new int[temp];
        operation=new int[temp];
        for(int i=0;i<temp;i++)
        {
            color[i]=0;
            pi[i]=0;
            d[i]=0;
            operation[i]=0;
        }
        color[0]=1;//(0,0)
        d[0]=0;
        queue<int> Q;
        Q.push(0);
        while(!Q.empty())
        {
            int u=Q.front();
            Q.pop();
            int v[2];
            v[0]=u/(B+1);//瓶A中的水容量
            v[1]=u-v[0]*(B+1);//瓶B中的水容量
            int i; //经过操作后,新的容量(i/(B+1),i%(B+1))
            for(int j=0;j<6;j++)
            {
                switch(j)
                {
                case 0:
                    i=num[0]*(B+1)+v[1];        //FILL(1)
                    break;
                case 1:
                    i=v[0]*(B+1)+num[1];        //FILL(2)
                    break;
                case 2:
                    i=v[1];                        //DROP(1)
                    break;
                case 3:
                    i=v[0]*(B+1);                //DROP(2)
                    break;
                case 4:
                    if(v[0]+v[1]<=num[0])
                        i=(v[0]+v[1])*(B+1);
                    else
                        i=(num[0]*(B+1))+v[1]-(num[0]-v[0]);
                    break;                        //POUR(2,1)
                case 5:
                    if(v[0]+v[1]<=num[1])
                        i=(v[0]+v[1]);
                    else
                        i=(v[0]-(num[1]-v[1]))*(B+1)+num[1];
                    break;                        //POUR(1,2)
                default:
                    break;
                }
                if(color[i]==0)
                {
                    d[i]=d[u]+1;
                    color[i]=1;
                    pi[i]=u;
                    operation[i]=j;
                    if(i/(B+1)==C || (i%(B+1))==C)
                        return i;
                    Q.push(i);
                }
            }
            color[u]=2;
        }
        return -1;
    }
    
    void print_path(int s,int v)
    {
        if(v==s)
            return;
        print_path(s,pi[v]);
        switch(operation[v])
        {
        case 0:
            cout << "FILL(1)";
            break;
        case 1:
            cout << "FILL(2)";
            break;
        case 2:
            cout << "DROP(1)";
            break;
        case 3:
            cout << "DROP(2)";
            break;
        case 4:
            cout << "POUR(2,1)";
            break;
        case 5:
            cout << "POUR(1,2)";
            break;
        default:
            break;
        }
        cout << endl;
    }
    
    int main()
    {
        int A,B,C;
        cin >> A >> B >> C;
        int output=BFS(A,B,C);
        if(output!=-1)
        {
            cout << d[output] << endl;
            print_path(0,output);
        }
        else
            cout << "impossible" << endl;
    }
    View Code
  • 相关阅读:
    thinkphp ajax分页加载更多最简单的实现方法
    thinkphp整合系列之phpexcel导入excel数据
    Nginx详细安装部署教程
    swoole 安装方法
    tpshop使用中遇到的问题
    Oracle 的分页查询 SQL 语句
    使用 PLSQL 提示动态执行表不可访问,本会话的自动统计被禁止
    JavaScript实现多张图片上传功能
    JavaScript控制页码的显示与隐藏
    Struts2 项目 Action 查询结果异常 org.apache.struts2.json.JSONException
  • 原文地址:https://www.cnblogs.com/DWVictor/p/10036736.html
Copyright © 2011-2022 走看看