zoukankan      html  css  js  c++  java
  • pots

    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)
     
    #include<algorithm>
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<stdlib.h>
    using namespace std;
    int A, B, C;
    char a[10][10] ={ "FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
    
    #define maxn 510
    #define INF 0x3f3f3f3f
    struct point
    {
        int x,y;
    };
    struct node
    {
        int fromx,fromy,step,op;
    }v[maxn][maxn];
    void dfs(int x,int y)
    {
        if(x==0&&y==0)
            return ;
        dfs(v[x][y].fromx,v[x][y].fromy);
    
        printf("%s
    ",a[v[x][y].op]);
    }
    void bfs()
    {
        queue<point>Q;
        point s,q;
        s.x = s.y = 0;
        v[0][0].step = 1;
    
        Q.push(s);
    
        while(Q.size())
        {
            s = Q.front();
            Q.pop();
    
            if(s.x==C||s.y==C)
            {
                printf("%d
    ",v[s.x][s.y].step-1);
                dfs(s.x,s.y);
    
                return ;
            }
            for(int i=0;i<6;i++)
            {
                q = s;
    
                if(i == 0)
                    q.x = A;
                else if(i == 1)
                    q.y = B;
                else if(i == 2)
                    q.x = 0;
                else if(i == 3)
                    q.y = 0;
                else if(i == 4)
                {
                    if(q.x+q.y <= B)
                        q.y += q.x, q.x = 0;//把A里面的水全倒B里面
                    else
                        q.x -= (B-q.y), q.y = B;//把B倒满
                }
                else
                {
                    if(q.x+q.y <= A)
                        q.x += q.y, q.y = 0;
                    else
                        q.y -= (A-q.x), q.x = A;
                }
    
                if(v[q.x][q.y].step == 0)
                {
                    v[q.x][q.y].step = v[s.x][s.y].step+1;
                    v[q.x][q.y].fromx = s.x;
                    v[q.x][q.y].fromy = s.y;
                    v[q.x][q.y].op = i;
    
                    Q.push(q);
                }
            }
        }
        printf("impossible
    ");
    }
    int main()
    {
        while(scanf("%d%d%d", &A,&B,&C)!=EOF)
        {
            memset(v,0,sizeof(v));
            bfs();
        }
        return 0;
    }
  • 相关阅读:
    Windows Azure 网站开发Stacks支持
    AzureDev 社区活动获奖者公布
    Android 改变窗口标题栏的布局
    cocos2d-x游戏开发系列教程-超级玛丽01-前言
    cocos2dx进阶学习之CCObject
    基于visual Studio2013解决算法导论之055拓扑排序
    查看某文件夹内文件大小
    vmstat命令
    uname 命令
    iostat命令
  • 原文地址:https://www.cnblogs.com/biu-biu-biu-/p/5761450.html
Copyright © 2011-2022 走看看