zoukankan      html  css  js  c++  java
  • POJ 3414, Pots

    Time Limit: 1000MS  Memory Limit: 65536K
    Total Submissions: 2779  Accepted: 1167  Special Judge


    Description

    You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
    FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
    DROP(i)      empty the pot i to the drain;
    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 A, B, 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


    // POJ3414.cpp : Defines the entry point for the console application.
    //

    #include 
    <iostream>
    #include 
    <queue>
    #include 
    <stack>
    #include 
    <string>
    using namespace std;

    int main(int argc, char* argv[])
    {
        
    int A,B,C;
        cin 
    >> A >> B >> C;

        
    int status[101][101][4];
        memset(status, 
    -1sizeof(status));

        queue
    <pair<char,char>> q;
        q.push(make_pair(
    0,0));
        status[
    0][0][0= 0;
        
    int x,y;
        
    bool find = false;
        
    while (!q.empty())
        {
            pair
    <char,char> cur = q.front();
            q.pop();

            
    if (cur.first == C || cur.second == C)
            {
                x 
    = cur.first;
                y 
    = cur.second;
                find 
    = true;
                
    break;
            }

            
    //fill pot 1
            if (status[A][cur.second][0]==-1)
            {
                q.push(make_pair(A,cur.second));
                status[A][cur.second][
    0= status[cur.first][cur.second][0+ 1;
                status[A][cur.second][
    1= cur.first;
                status[A][cur.second][
    2= cur.second;
                status[A][cur.second][
    3= 1;
            }
            
    //drop pot 1
            if (status[0][cur.second][0]==-1)
            {
                q.push(make_pair(
    0,cur.second));
                status[
    0][cur.second][0= status[cur.first][cur.second][0+ 1;
                status[
    0][cur.second][1= cur.first;
                status[
    0][cur.second][2= cur.second;
                status[
    0][cur.second][3= 2;
            }
            
    //Pour pot 1 --> pot 2
            int total = cur.first + cur.second;
            
    int a, b;
            
    if (total <= B)
            {
                a 
    = 0;
                b 
    = total;
            }
            
    else
            {
                a 
    = total - B;
                b 
    = B;
            }
            
    if (status[a][b][0]==-1)
            {
                q.push(make_pair(a, b));
                status[a][b][
    0= status[cur.first][cur.second][0+ 1;
                status[a][b][
    1= cur.first;
                status[a][b][
    2= cur.second;
                status[a][b][
    3= 3;
            }

            
    //fill pot 2
            if (status[cur.first][B][0]==-1)
            {
                q.push(make_pair(cur.first,B));
                status[cur.first][B][
    0= status[cur.first][cur.second][0+ 1;
                status[cur.first][B][
    1= cur.first;
                status[cur.first][B][
    2= cur.second;
                status[cur.first][B][
    3= 4;
            }
            
    //drop pot 2
            if (status[cur.first][0][0]==-1)
            {
                q.push(make_pair(cur.first,
    0));
                status[cur.first][
    0][0= status[cur.first][cur.second][0+ 1;
                status[cur.first][
    0][1= cur.first;
                status[cur.first][
    0][2= cur.second;
                status[cur.first][
    0][3= 5;
            }
            
    //Pour pot 2 --> pot 1
            total = cur.first + cur.second;
            
    if (total <= A)
            {
                a 
    = total;
                b 
    = 0;
            }
            
    else
            {
                a 
    = A;
                b 
    = total - A;
            }
            
    if (status[a][b][0]==-1)
            {
                q.push(make_pair(a, b));
                status[a][b][
    0= status[cur.first][cur.second][0+ 1;
                status[a][b][
    1= cur.first;
                status[a][b][
    2= cur.second;
                status[a][b][
    3= 6;
            }
        };

        
    if (find == true)
        {
            
    int cnt = status[x][y][0];
            stack
    <string> st;
            
    for (int i = 0; i < cnt; ++i)
            {
                
    switch(status[x][y][3])
                {
                    
    case 1: st.push("FILL(1)\n");break;
                    
    case 2: st.push("DROP(1)\n");break;
                    
    case 3: st.push("POUR(1,2)\n");break;
                    
    case 4: st.push("FILL(2)\n");break;
                    
    case 5: st.push("DROP(2)\n");break;
                    
    case 6: st.push("POUR(2,1)\n");break;
                }
                
    int x1 = status[x][y][1];
                
    int y1 = status[x][y][2];
                x 
    = x1;
                y 
    = y1;
            }

            cout 
    << cnt << endl;
            
    while(!st.empty())
            {
                cout 
    << st.top();
                st.pop();
            }
        }
        
    else
            cout 
    << "impossible\n";
        
    return 0;
    }

  • 相关阅读:
    解决动态加载数据滑动失效
    js 判断手机横竖屏的实现方法(不依赖任何其他库)
    element-ui upload 图片格式验证 ,文件验证
    swiper 绑定点击事件 点击失效处理
    vue项目
    [Qt]自定义表头实现过滤功能
    字符串截取问题
    mysql超时机制
    nginx原理
    高可用集群(主从模式)
  • 原文地址:https://www.cnblogs.com/asuran/p/1580733.html
Copyright © 2011-2022 走看看