zoukankan      html  css  js  c++  java
  • Pots POJ

    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)
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    #include<stack>
    using namespace std;
    struct cup{
        int x,y;
        int step;
        int flag;//标记操作
        cup*pre; //标记路径 
    };
    queue<cup> q;
    stack<int> r;
    int a,b,e;
    int vis[117][117]={0};//标记当前状态是否到达过
    int ans;
    void print(){
        while(!r.empty()){
            int i=r.top();
            r.pop();
            switch(i){
                case 1:cout<<"FILL(1)"<<endl;break;
                case 2:cout<<"FILL(2)"<<endl;break;
                case 3:cout<<"DROP(1)"<<endl;break;
                case 4:cout<<"DROP(2)"<<endl;break;
                case 5:cout<<"POUR(1,2)"<<endl;break;
                case 6:cout<<"POUR(2,1)"<<endl;break;
            }
        }
    }
    void bfs(int x,int y){
        cup c;
        cup t[317];///目前瓶子里剩余的水量
        c.x=0,c.y=0;
        c.flag=0;
        c.pre=NULL;
        c.step=0;
        q.push(c);
        vis[x][y]=1;
        int count =-1;
        while(!q.empty()){
            count++;
            t[count]=q.front();
            q.pop();
            for(int i=1;i<=6;i++){
                switch(i)
                {
                    case 1:{
                        c.x=a;
                        c.y=t[count].y;
                        c.flag=1;
                        break;
                    }
                    case 2:{
                        c.x=t[count].x;
                        c.y=b;
                        c.flag=2;
                        break;
                    }
                    case 3:{
                        c.x=0;
                        c.y=t[count].y;
                        c.flag=3;
                        break;
                    }
                    case 4:{
                        c.x=t[count].x;
                        c.y=0;
                        c.flag=4;
                        break;
                    }
                    case 5:{
                        if(t[count].x>b-t[count].y){
                            c.x=t[count].x-(b-t[count].y);
                            c.y=b;
                        }else{
                            c.x=0;
                            c.y=t[count].y+t[count].x;
                               }
                               c.flag=5;
                        break;
                    }
                    case 6:{
                        if(t[count].y>a-t[count].x){
                            c.y=t[count].y-(a-t[count].x);
                            c.x=a;
                        }else{
                            c.y=0;
                            c.x=t[count].x+t[count].y;
                               }
                               c.flag=6;
                        break;
                    }    
                } 
                if(vis[c.x][c.y])continue;
                    vis[c.x][c.y]=1;
                    c.step=t[count].step+1;
                    c.pre=&t[count];
                    if(c.x==e||c.y==e){
                        ans=c.step;
                        while(c.pre){
                            r.push(c.flag);
                            c = *c.pre;
                        }
                        return;
                    }
                    q.push(c);
            }
        } 
        
    } 
    int main(){
        cin>>a>>b>>e;
        bfs(0,0);
        if(ans==0)
            cout<<"impossible"<<endl;
        else{
            cout<<ans<<endl;
            print();
        }
        return 0;
    }
  • 相关阅读:
    已设定选项readonly请加!强制执行
    Linux下NVIDIA显卡驱动安装方法
    C#使用MiniDump导出内存快照MiniDumper
    一些陈旧的注册表垃圾清理脚本:注册表冗余数据清理.reg
    脚本精灵一些脚本
    本地安装SonarQube Community8.1社区版进行代码质量管控
    spring redistemplate中使用setHashValueSerializer的设置hash值序列化方法
    spring-core-5.0.6.RELEASE-sources.jar中java源代码不全
    lombok插件/slf4j中字符串格式化
    light4j/light-4j一个轻量级的低延时、高吞吐量、内存占用量小的API平台
  • 原文地址:https://www.cnblogs.com/CuteAbacus/p/9492151.html
Copyright © 2011-2022 走看看