zoukankan      html  css  js  c++  java
  • POJ_3414 Pots 【复杂BFS】

    一、题面

    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.

    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)

    二、分析

     对于这题,难点不在BFS的思路,难点在于BFS每一次父节点生成孩子结点的时候,情况比较复杂。对于记录路径,仍然需要使用孩子节点标记一个前缀指向父节点,然后用递归的方式实现即可。自己在写代码的时候非常不注意,在生成孩子节点时,对于标记访问的数组,本来应该用=,但我直接复制的判断条件里的==,导致一直RE。谨记!

    三、AC代码

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <queue>
      5 using namespace std;
      6 const int MAXN = 100;
      7 bool visit[MAXN+3][MAXN+3];
      8 int A, B, C;
      9 
     10 struct Point
     11 {
     12     int first, second, cnt;
     13     int prev, id;   //父节点和操作对象
     14     char op; //操作
     15 };
     16 
     17 Point P[MAXN*MAXN + 4];
     18 int Cnt, Ans;
     19 
     20 void Output(int t)
     21 {
     22     if(P[t].prev != -1)
     23     {
     24         Ans++;
     25         Output(P[t].prev);
     26     }
     27     if(Ans != -1)
     28     {
     29         printf("%d
    ", Ans);
     30         Ans = -1;
     31     }
     32     if(P[t].op=='F')
     33     {
     34         printf("FILL(%d)
    ", P[t].id);
     35     }
     36     else if(P[t].op == 'P')
     37     {
     38         printf("POUR(%d,%d)
    ", P[t].id, P[t].id==1?2:1);
     39     }
     40     else if(P[t].op == 'D')
     41     {
     42         printf("DROP(%d)
    ", P[t].id);
     43     }
     44 }
     45 
     46 void BFS()
     47 {
     48     Point t;
     49     int cur;
     50     t.first = 0, t.second = 0;
     51     visit[0][0] = 1;
     52     t.op = '0', t.prev = -1, t.id = -1;
     53     t.cnt = 0;
     54     P[0] = t;
     55     Cnt = 1;
     56     cur = 0;
     57 
     58     while(true)
     59     {
     60         if(cur >= Cnt)
     61         {
     62             printf("impossible
    ");
     63             return;
     64         }
     65         Point pt = P[cur++];
     66 
     67         if(pt.first == C || pt.second == C)
     68         {
     69             Ans = 0;
     70             Output(pt.cnt);
     71             break;
     72         }
     73 
     74         t.prev = pt.cnt;
     75 
     76         if(pt.first < A)
     77         {
     78             t.first = A;
     79             t.second = pt.second;
     80             if(visit[t.first][t.second] == 0)
     81             {
     82                 //visit[t.first][t.second] == 1; 刚开始RE的原因
     83                 visit[t.first][t.second] = 1;
     84                 t.op = 'F';
     85                 t.id = 1;
     86                 t.cnt = Cnt;
     87                 P[Cnt++] = t;
     88 
     89             }
     90         }
     91 
     92         if(pt.second < B)
     93         {
     94             t.first = pt.first;
     95             t.second = B;
     96             if(visit[t.first][t.second] == 0)
     97             {
     98                 visit[t.first][t.second] = 1;
     99                 t.op = 'F';
    100                 t.id = 2;
    101                 t.cnt = Cnt;
    102                 P[Cnt++] = t;
    103             }
    104         }
    105 
    106         if(pt.first < A && pt.second > 0 )
    107         {
    108             t.first = pt.first + pt.second;
    109             t.second = t.first - A;
    110             if(t.second < 0)
    111                 t.second = 0;
    112             else
    113                 t.first = A;
    114             if(visit[t.first][t.second] == 0)
    115             {
    116                 visit[t.first][t.second] = 1;
    117                 t.op = 'P';
    118                 t.id = 2;
    119                 t.cnt = Cnt;
    120                 P[Cnt++] = t;
    121             }
    122         }
    123 
    124         if(pt.second < B && pt.first > 0)
    125         {
    126             t.second = pt.second + pt.first;
    127             t.first = t.second - B;
    128             if(t.first < 0)
    129                 t.first = 0;
    130             else
    131                 t.second = B;
    132             if(visit[t.first][t.second] == 0)
    133             {
    134                 visit[t.first][t.second] = 1;
    135                 t.op = 'P';
    136                 t.id = 1;
    137                 t.cnt = Cnt;
    138                 P[Cnt++] = t;
    139             }
    140         }
    141 
    142         if(pt.first > 0)
    143         {
    144             t.first = 0;
    145             t.second = pt.second;
    146             if(visit[t.first][t.second] == 0)
    147             {
    148                 visit[t.first][t.second] = 1;
    149                 t.op = 'D';
    150                 t.id = 1;
    151                 t.cnt = Cnt;
    152                 P[Cnt++] = t;
    153             }
    154         }
    155 
    156         if(pt.second > 0)
    157         {
    158             t.first = pt.first;
    159             t.second = 0;
    160             if(visit[t.first][t.second] == 0)
    161             {
    162                 visit[t.first][t.second] = 1;
    163                 t.op = 'D';
    164                 t.id = 2;
    165                 t.cnt = Cnt;
    166                 P[Cnt++] = t;
    167             }
    168         }
    169     }
    170 }
    171 
    172 int main()
    173 {
    174     while(scanf("%d %d %d", &A, &B, &C)!=EOF)
    175     {
    176         memset(visit, 0, sizeof(visit));
    177         BFS();
    178     }
    179     return 0;
    180 }
    View Code
  • 相关阅读:
    Ganglia Install
    [ZZ]perl一揽子特殊变量
    点滴积累【C#】---C#实现上传word将路径保存到数据库,文件保存到服务器。并且按照名称读取服务器的word
    点滴积累【C#】---C#实现上传照片到物理路径,并且将地址保存到数据库,
    点滴积累【C#】---C#实现上传word以流形式保存到数据库和读取数据库中的word文件。
    点滴积累【C#】---将Excel数据导入到数据库
    点滴积累【C#】---TreeView读取数据库
    DIV内英文或者数字不换行的问题 解决办法
    Rabbitmq中rabbitmqctl的常用命令
    Uploadify参数详解
  • 原文地址:https://www.cnblogs.com/dybala21/p/10023498.html
Copyright © 2011-2022 走看看