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
  • 相关阅读:
    rxjava rxandroid使用遇到的坑
    使用ARouter遇到的坑
    java(Android)跨Module调用对应类方法需求解决方案
    前端工程师技能之photoshop巧用系列第四篇——图片格式
    前端工程师技能之photoshop巧用系列第三篇——切图篇
    前端工程师技能之photoshop巧用系列第二篇——测量篇
    前端工程师技能之photoshop巧用系列第一篇——准备篇
    js中用tagname和id获取元素的3种方法
    MVC显示Base64图片
    在ASP.NET MVC应用程序中随机获取一个字符串
  • 原文地址:https://www.cnblogs.com/cxbky/p/4899054.html
Copyright © 2011-2022 走看看