zoukankan      html  css  js  c++  java
  • POJ3414 Pots —— BFS + 模拟

    题目链接:http://poj.org/problem?id=3414


    Pots
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 18550   Accepted: 7843   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



    题解:

    类似的题目,也是有关几个容器倒来倒去,然后求一个目标量:http://blog.csdn.net/dolfamingo/article/details/77804030

    此题不过是多了个路径输出,队列把STL换成数组就可以了,不细述。




    代码如下:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cmath>
      5 #include <algorithm>
      6 #include <vector>
      7 #include <queue>
      8 #include <stack>
      9 #include <map>
     10 #include <string>
     11 #include <set>
     12 #define ms(a,b) memset((a),(b),sizeof((a)))
     13 using namespace std;
     14 typedef long long LL;
     15 const int INF = 2e9;
     16 const LL LNF = 9e18;
     17 const int MOD = 1e9+7;
     18 const int MAXN = 100+10;
     19 
     20 struct node
     21 {
     22     //con[i]为容器i当前的量;op存操作的信息;pre为上一步所在队列的下标(为了输出路径)
     23     int con[2], op[3], step, pre;
     24 };
     25 int vis[MAXN][MAXN], vol[3];    //vol[0]、vol[1]为两个容器的容量, vol[3]为目标量
     26 
     27 node que[100010];   //由于要输出路径,就要用数组来作队列了
     28 int front, rear;
     29 int bfs()
     30 {
     31     ms(vis,0);
     32     front = rear = 0;
     33 
     34     node now, tmp;
     35     now.con[0] = now.con[1] = 0;
     36     now.step = 0;
     37     vis[0][0] = 1;
     38     que[rear++] = now;
     39 
     40     while(front!=rear)
     41     {
     42         now = que[front++];
     43         if(now.con[0]==vol[2] || now.con[1]==vol[2])
     44             return front-1;
     45 
     46         for(int i = 0; i<2; i++)
     47         {
     48             tmp = now;      //操作1:FILL
     49             tmp.con[i] = vol[i];
     50             if(!vis[tmp.con[0]][tmp.con[1]])
     51             {
     52                 vis[tmp.con[0]][tmp.con[1]] = 1;
     53                 tmp.op[0] = 1; tmp.op[1] = i;
     54                 tmp.step = now.step+1;
     55                 tmp.pre = front-1;
     56                 que[rear++] = tmp;
     57             }
     58 
     59             tmp = now;      //操作2:DROP
     60             tmp.con[i] = 0;
     61             if(!vis[tmp.con[0]][tmp.con[1]])
     62             {
     63                 vis[tmp.con[0]][tmp.con[1]] = 1;
     64                 tmp.op[0] = 2; tmp.op[1] = i;
     65                 tmp.step = now.step+1;
     66                 tmp.pre = front-1;
     67                 que[rear++] = tmp;
     68             }
     69 
     70             tmp = now;      //操作三POUR
     71             int j = !i; //另外一个容器
     72             int pour = min(tmp.con[i], vol[j]-tmp.con[j]);
     73             tmp.con[j] += pour;
     74             tmp.con[i] -= pour;
     75             if(!vis[tmp.con[0]][tmp.con[1]])
     76             {
     77                 vis[tmp.con[0]][tmp.con[1]] = 1;
     78                 tmp.op[0] = 3; tmp.op[1] = i; tmp.op[2] = j;
     79                 tmp.step = now.step+1;
     80                 tmp.pre = front-1;
     81                 que[rear++] = tmp;
     82             }
     83         }
     84     }
     85     return -1;
     86 }
     87 
     88 void Print(int i)   //输出路径
     89 {
     90     if(que[i].pre!=0)
     91         Print(que[i].pre);
     92 
     93     if(que[i].op[0]==1)
     94         printf("FILL(%d)
    ", que[i].op[1]+1);
     95     else if(que[i].op[0]==2)
     96         printf("DROP(%d)
    ", que[i].op[1]+1);
     97     else
     98         printf("POUR(%d,%d)
    ", que[i].op[1]+1, que[i].op[2]+1);
     99 }
    100 
    101 int main()
    102 {
    103     while(scanf("%d%d%d",&vol[0], &vol[1], &vol[2])!=EOF)
    104     {
    105         int i = bfs();
    106         if(i==-1)
    107             puts("impossible");
    108         else
    109         {
    110             printf("%d
    ",que[i].step);
    111             Print(i);
    112         }
    113     }
    114 }
    View Code


  • 相关阅读:
    valgrind试用笔记
    《c++ primer》3.5 array 小结
    《c++ primer》chap8 The IO library 小结
    《c++ primer》3.4 迭代器(iterator)
    《c++ primer》3.1 声明命名空间 小结
    c++ 流格式控制符
    《c++ primer》3.3 vector 类型小结
    《c++ primer》3.2 string 小结
    Ubuntu 管理相关小知识(不定期更新)
    shell 脚本 生成文件,文件名为日期时间
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7538592.html
Copyright © 2011-2022 走看看