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

    Pots
    Time Limit: 1000MS   MemoryLimit: 65536K
            Special Judge

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

    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)

    Source

    Northeastern Europe 2002, Western Subregion
     
    倒水问题,输出步数和方案
    #include<cstdio>
    #include<queue>
    #include<cstdlib>
    #include<cstring>
    using namespace std;
    int a,b,z,xu;
    bool v[1000000];
    int solution[1000000],cnt,pre[1000000];
    struct node
    {
        int x,y,s,id;
    }cur,nxt;
    queue<node>p;
    void output(int w)
    {
        if(!w) return;
        else output(pre[w]);
        switch(solution[w])
        {
            case 1:printf("FILL(1)
    ");break;
            case 2:printf("FILL(2)
    ");break;
            case 3:printf("DROP(1)
    ");break;
            case 4:printf("DROP(2)
    ");break;
            case 5:printf("POUR(2,1)
    ");break;
            case 6:printf("POUR(1,2)
    ");
        }
    }
    void pan(int i,int j,int w)
    {
        if(i==z||j==z)
        {
            printf("%d
    ",nxt.s);
            output(w);
            exit(0);
        }
    }
    int main()
    {
        while(scanf("%d%d%d",&a,&b,&z)==3)
        {
            if(a==z)
            {
                printf("1
    ");
                printf("FILL(1)
    ");
                return 0;
            }
            else if(b==z) 
            {
                printf("1
    ");
                printf("FILL(2)
    ");
                return 0;
            }
            while(!p.empty()) p.pop(); cnt=0;
            memset(v,0,sizeof(v));
            memset(solution,0,sizeof(solution));
            cur.x=a; cur.y=0; cur.s=1; cur.id=++cnt; solution[cnt]=1;
            p.push(cur);
            cur.x=0; cur.y=b; cur.s=1; cur.id=++cnt; solution[cnt]=2;
            p.push(cur);
            while(!p.empty())
            {
                cur=p.front();
                if(!cur.x)    
                {
                    nxt.x=a; nxt.y=cur.y; nxt.s=cur.s+1;
                    nxt.id=++cnt; pre[cnt]=cur.id; solution[cnt]=1;
                    pan(nxt.x,nxt.y,cnt);
                    if(!v[nxt.x*1000+nxt.y])        
                    {
                        v[nxt.x*1000+nxt.y]=true;
                        p.push(nxt);
                    }
                }
                if(!cur.y)        
                {
                    nxt.x=cur.x; nxt.y=b; nxt.s=cur.s+1;
                    nxt.id=++cnt; pre[cnt]=cur.id; solution[cnt]=2;
                    pan(nxt.x,nxt.y,cnt);
                    if(!v[nxt.x*1000+nxt.y])
                    {
                        v[nxt.x*1000+nxt.y]=true;
                        p.push(nxt);
                    }
                }
                if(cur.x)
                {
                    nxt.x=0; nxt.y=cur.y; nxt.s=cur.s+1;
                    nxt.id=++cnt; pre[cnt]=cur.id; solution[cnt]=3;
                    if(!v[nxt.x*1000+nxt.y])
                    {
                        v[nxt.x*1000+nxt.y]=true;
                        p.push(nxt);
                    }
                }
                if(cur.y)
                {
                    nxt.x=cur.x; nxt.y=0; nxt.s=cur.s+1;
                    nxt.id=++cnt; pre[cnt]=cur.id; solution[cnt]=4;
                    if(!v[nxt.x*1000+nxt.y])
                    {
                        v[nxt.x*1000+nxt.y]=true;
                        p.push(nxt);
                    }
                }
                if(cur.x||cur.y)
                {
                    xu=a-cur.x;
                    if(cur.y>xu)
                    {
                        nxt.x=a; nxt.y=cur.y-xu;
                    }
                    else 
                    {
                        nxt.x=cur.x+cur.y; nxt.y=0;
                    }
                    nxt.s=cur.s+1;
                    nxt.id=++cnt; pre[cnt]=cur.id; solution[cnt]=5;
                    pan(nxt.x,nxt.y,cnt);
                    if(!v[nxt.x*1000+nxt.y])
                    {
                        v[nxt.x*1000+nxt.y]=true;
                        p.push(nxt);
                    }
                    xu=b-cur.y;
                    if(cur.x>xu)
                    {
                        nxt.y=b;
                        nxt.x=cur.x-xu;
                    }
                    else 
                    {
                        nxt.y=cur.x+cur.y;
                        nxt.x=0;
                    }
                    nxt.s=cur.s+1;
                    nxt.id=++cnt; pre[cnt]=cur.id; solution[cnt]=6;
                    pan(nxt.x,nxt.y,cnt);
                    if(!v[nxt.x*1000+nxt.y])
                    {
                        v[nxt.x*1000+nxt.y]=true;
                        p.push(nxt);
                    }
                }
                p.pop();
            }
            printf("impossible
    ");
        }
    }
  • 相关阅读:
    Flash/Flex学习笔记(44):万有引力与粒子系统
    Flash/Flex学习笔记(43):动量守恒与能量守恒
    Flash/Flex学习笔记(36):自己动手实现一个滑块控件(JimmySilder)
    Flash/Flex学习笔记(40):弹性运动续弹簧
    Flash/Flex学习笔记(38):缓动动画
    Flash/Flex学习笔记(37):不用系统组件(纯AS3)的视频播放器只有8.82K
    Flash/Flex学习笔记(42):坐标旋转
    LAMP经典入门教程
    配置SQLServer2005数据库镜像实践操作记录
    PHP 免费视频教程
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6826896.html
Copyright © 2011-2022 走看看