• POJ 3414 Pots(罐子)


    POJ 3414 Pots(罐子)

    Time Limit: 1000MS    Memory Limit: 65536K

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

    给定两个灌子,容量分别为A与B升。可执行下列操作:
    1.FILL(i)        将罐子 i (1 ≤ i ≤ 2) 装满;
    2.DROP(i)      将罐子 i 倒空;
    3.POUR(i,j)    将罐子 i 倒向罐子 j; 此操作后罐子 j 可能被装满 (罐子 i 中可能还剩一些水), 或者罐子 i 为空 (所有东西都被倒入罐子 j).
    CN

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

    输入的第一行也是唯一一行,有三个数A,B和C。所有整数都在1到100间,且C≤max(A,B)。
    CN

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

    输出的第一行必须包含操作序列长度K。
    随后K行必须每行描述一个操作。如果有多种最短序列,输出任意一种。
    如果无法得出结果,输出的第一行也是唯一一行则为‘impossible’。
    CN

    Sample Input - 输入样例

    3 5 4
    

     

    Sample Output - 输出样例

    6
    FILL(2)
    POUR(2,1)
    DROP(1)
    POUR(2,1)
    FILL(2)
    POUR(2,1)
    

     

    题解

      因为需要找最短的操作,不能直接上DFS了。
      典型的BFS套路。注意步骤的回溯即可。
      (作为懒人直接丢vector……)

     

    代码 C++

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <queue>
     4 struct OPR{
     5     int a, b, len;
     6     std::vector<int> op;
     7     void popOP(int sum){
     8         while (sum--) op.pop_back();
     9     }
    10     void pushOP(int i, int j, int k){
    11         op.push_back(i); op.push_back(j);
    12         if (i == 3) op.push_back(k);
    13     }
    14 }opt;
    15 
    16 int len[100105];
    17 std::queue<OPR> q;
    18 void qPush(OPR now){
    19     if (now.len < len[now.a * 1000 + now.b]){
    20         len[now.a * 1000 + now.b] = now.len;
    21         q.push(now);
    22     }
    23 }
    24 
    25 int main(){
    26     int aMX, bMX, c, tmpA, tmpB, i;
    27     memset(len, 0x7F, sizeof len); opt.len = len[100104];
    28     scanf("%d%d%d", &aMX, &bMX, &c);
    29     OPR now = { 0, 0, 0 }; q.push(now);
    30     while (!q.empty()){
    31         now = q.front(); q.pop();
    32         if (now.a == c || now.b == c){
    33             if (now.len < opt.len) opt = now;
    34             continue;
    35         }
    36         ++now.len;
    37         tmpA = now.a; tmpB = now.b;
    38 
    39         if (tmpA != aMX){
    40             now.a = aMX;
    41             now.pushOP(1, 1, -1); qPush(now);
    42             now.a = tmpA; now.popOP(2);
    43         }
    44         if (now.b != bMX){
    45             now.b = bMX;
    46             now.pushOP(1, 2, -1); qPush(now);
    47             now.b = tmpB; now.popOP(2);
    48         }
    49         if (tmpA){
    50             now.a = 0;
    51             now.pushOP(2, 1, -1); qPush(now);
    52             now.a = tmpA; now.popOP(2);
    53 
    54             now.b += now.a;
    55             if (now.b > bMX) now.a = now.b - bMX, now.b = bMX;
    56             else now.a = 0;
    57             now.pushOP(3, 1, 2); qPush(now);
    58             now.a = tmpA; now.b = tmpB; now.popOP(3);
    59         }
    60         if (now.b){
    61             now.b = 0;
    62             now.pushOP(2, 2, -1); qPush(now);
    63             now.b = tmpB; now.popOP(2);
    64 
    65             now.a += now.b;
    66             if (now.a > aMX) now.b = now.a - aMX, now.a = aMX;
    67             else now.b = 0;
    68             now.pushOP(3, 2, 1); qPush(now);
    69             now.a = tmpA; now.b = tmpB; now.popOP(3);
    70         }
    71     }
    72 
    73     if (opt.len == len[100104]) puts("impossible");
    74     else{
    75         printf("%d
    ", opt.len);
    76         for (i = 0; i < opt.op.size();){
    77             switch (opt.op[i]){
    78             case 1: printf("FILL(%d)
    ", opt.op[i + 1]); i += 2; break;
    79             case 2: printf("DROP(%d)
    ", opt.op[i + 1]); i += 2; break;
    80             default:printf("POUR(%d,%d)
    ", opt.op[i + 1], opt.op[i + 2]); i += 3; break;
    81             }
    82         }
    83     }
    84     return 0;
    85 }
  • 相关阅读:
    Probability theory
    python Memo
    numpy 札记
    linux LVM 逻辑卷
    关于TF-IDF的一些见解
    Python之list、dict、np等常用数值运算
    Python之matplotlib绘图
    Python之打开网页
    Python之获取地址经纬度
    两台电脑直接共享文件
  • 原文地址:https://www.cnblogs.com/Simon-X/p/6338094.html
Copyright © 2020-2023  润新知