zoukankan      html  css  js  c++  java
  • poj 3414 Pots(BFS)

    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)

    大致题意:给出了两个罐子的容量A,B, 以及一个目标水量C,对A、B可以有如下操作:
    FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap; //装满i罐子
    DROP(i)      empty the pot i to the drain; //清空i罐子
    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). //将i罐子倒到j罐子 有两种情况:1: i+j<=j 2: i+j>j

      1 #include <iostream>
      2 #include <queue>
      3 #include <cstdlib>
      4 #include <cstdio>
      5 #include <cstring>
      6 #include <algorithm>
      7 using namespace std;
      8 struct node
      9 {
     10     int a,b;   //当前水量
     11     char str[150][150];
     12     int step;  //记录步数
     13 };
     14 int A,B,C; //两个杯子的容量以及目标残余水量
     15 bool vis[150][150];
     16 void bfs()
     17 {
     18     memset(vis,false,sizeof(vis));
     19     node head,tail;
     20     queue<node>q;
     21     head.a=head.b=head.step=0;
     22     q.push(head);
     23     vis[0][0]=true;
     24     while(!q.empty())
     25     {
     26         head=q.front();
     27         q.pop();
     28         if(head.a==C || head.b==C) //如果达到目标水量
     29         {
     30             cout<<head.step<<endl; //输出步数
     31             for(int i=1; i<=head.step; i++) //以及操作步骤
     32                 cout<<head.str[i]<<endl;
     33             return ;
     34         }
     35         if(head.a==0) //如果a当前容量为空
     36         {
     37             tail=head;
     38             tail.a=A;
     39             tail.step++; //步数加一
     40             strcpy(tail.str[tail.step],"FILL(1)");  //将操作步骤存起来
     41             if(!vis[tail.a][tail.b])
     42             {
     43                 vis[tail.a][tail.b]=true;
     44                 q.push(tail);
     45             }
     46         }
     47         else if(head.a<=A)
     48         {
     49             tail=head;
     50             tail.a=0;
     51             tail.step++;
     52             strcpy(tail.str[tail.step],"DROP(1)");
     53             if(!vis[tail.a][tail.b])
     54             {
     55                 vis[tail.a][tail.b]=true;
     56                 q.push(tail);
     57             }
     58             if(head.b<B)
     59             {
     60                 tail=head;
     61                 if(tail.a+tail.b>B) //当二个罐子的数量相加大于瓶子的容量时
     62                 {
     63                     tail.a=tail.a+tail.b-B;
     64                     tail.b=B;
     65                 }
     66                 else //若不大于时
     67                 {
     68                     tail.b+=tail.a; //将a罐子的水倒到b罐子里
     69                     tail.a=0;       //这时a罐子为空
     70                 }
     71                 tail.step++; //步数加一
     72                 strcpy(tail.str[tail.step],"POUR(1,2)");
     73                 if(!vis[tail.a][tail.b])
     74                 {
     75                     vis[tail.a][tail.b]=true;
     76                     q.push(tail);
     77                 }
     78             }
     79         }
     80         if(head.b==0)
     81         {
     82             tail=head;
     83             tail.b=B;
     84             tail.step++;
     85             strcpy(tail.str[tail.step],"FILL(2)");
     86             if(!vis[tail.a][tail.b])
     87             {
     88                 vis[tail.a][tail.b]=true;
     89                 q.push(tail);
     90             }
     91         }
     92         else if(head.b<=B)
     93         {
     94             tail=head;
     95             tail.b=0;
     96             tail.step++;
     97             strcpy(tail.str[tail.step],"DROP(2)");
     98             if(!vis[tail.a][tail.b])
     99             {
    100                 vis[tail.a][tail.b]=true;
    101                 q.push(tail);
    102             }
    103             if(head.a<A)
    104             {
    105                 tail=head;
    106                 if(tail.a+tail.b>A)
    107                 {
    108                     tail.b=tail.a+tail.b-A;
    109                     tail.a=A;
    110                 }
    111                 else
    112                 {
    113                     tail.a+=tail.b;
    114                     tail.b=0;
    115                 }
    116                 tail.step++;
    117                 strcpy(tail.str[tail.step],"POUR(2,1)");
    118                 if(!vis[tail.a][tail.b])
    119                 {
    120                     vis[tail.a][tail.b]=true;
    121                     q.push(tail);
    122                 }
    123             }
    124         }
    125     }
    126     cout<<"impossible"<<endl;
    127     return ;
    128 }
    129 int main()
    130 {
    131     while(cin>>A>>B>>C)
    132     {
    133         bfs();
    134     }
    135     return 0;
    136 }
    View Code
  • 相关阅读:
    4星|《态度》:吴军博士写给读高中和大学的女儿们的信
    2星|宇见《洞察力:让营销从此直指人心》:营销讲师自创的理论,逻辑较差,洞察力不用提
    1星|《社群X平台》:没有实际工作经验的职业写手拼凑而成
    3星|《一课经济学》:70年前的经济学思想普及读物,现在看还不过时
    3.5星|《戴维斯王朝》:业绩媲美巴菲特的投资人家族的故事
    3.5星|《行为设计学:零成本改变》:明确的、可操作的、短期的、可以引起情感共鸣的目标,更有助于个人或组织做出改变
    1星|《讲法:从说教到赋能》:用伪科学概念说理,对建构主义的解释与理解比较牵强
    2星|《谁站在马云背后》:总裁律师的职责、素养、趋势。信息浓度较低,案例偏老旧肤浅
    2星|简七《巴比伦富翁新解》:浅显的理财原则,目测作者长于理财培训而不是理财实战
    使用 Visual Studio 部署 .NET Core 应用 ——.Net Core 部署到Ubuntu 16.04
  • 原文地址:https://www.cnblogs.com/cxbky/p/4899054.html
Copyright © 2011-2022 走看看