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 ≤ 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。

    代码如下:
      1 # include<iostream>
      2 # include<cstdio>
      3 # include<string>
      4 # include<queue>
      5 # include<vector>
      6 # include<cstring>
      7 # include<algorithm>
      8 using namespace std;
      9 struct node
     10 {
     11     int a,b,t;
     12     vector<string>op;
     13     bool operator < (const node &a) const {
     14         return t>a.t;
     15     }
     16     node & operator = (const node &p) {
     17         a=p.a,b=p.b,t=p.t;
     18         op.clear();
     19         for(int i=0;i<p.op.size();++i)
     20             op.push_back(p.op[i]);
     21         return *this;
     22     }
     23 };
     24 int vis[105][105];
     25 void bfs(int A,int B,int C)
     26 {
     27     priority_queue<node>q;
     28     memset(vis,0,sizeof(vis));
     29     node sta;
     30     sta.a=sta.b=sta.t=0;
     31     sta.op.clear();
     32     vis[0][0]=1;
     33     q.push(sta);
     34     while(!q.empty())
     35     {
     36         node u=q.top();
     37         q.pop();
     38         if(u.a==C||u.b==C){
     39             printf("%d
    ",u.t);
     40             for(int i=0;i<u.op.size();++i)
     41                 cout<<u.op[i]<<endl;
     42             return ;
     43         }
     44         if(u.a<A){
     45             node now=u;
     46             now.a=A,now.b=u.b;
     47             if(!vis[now.a][now.b]){
     48                 vis[now.a][now.b];
     49                 now.t=u.t+1;
     50                 now.op.push_back("FILL(1)");
     51                 q.push(now);
     52             }
     53         }
     54         if(u.b<B){
     55             node now=u;
     56             now.a=u.a,now.b=B;
     57             if(!vis[now.a][now.b]){
     58                 vis[now.a][now.b];
     59                 now.t=u.t+1;
     60                 now.op.push_back("FILL(2)");
     61                 q.push(now);
     62             }
     63         }
     64         if(u.a>0){
     65             node now=u;
     66             now.a=0,now.b=u.b;
     67             if(!vis[now.a][now.b]){
     68                 vis[now.a][now.b];
     69                 now.t=u.t+1;
     70                 now.op.push_back("DROP(1)");
     71                 q.push(now);
     72             }
     73         }
     74         if(u.b>0){
     75             node now=u;
     76             now.a=u.a,now.b=0;
     77             if(!vis[now.a][now.b]){
     78                 vis[now.a][now.b];
     79                 now.t=u.t+1;
     80                 now.op.push_back("DROP(2)");
     81                 q.push(now);
     82             }
     83         }
     84         if(u.a<A&&u.b>0){
     85             node now=u;
     86             now.a=min(A,u.a+u.b);
     87             now.b=max(0,u.b-A+u.a);
     88             if(!vis[now.a][now.b]){
     89                 vis[now.a][now.b]=vis[now.b][now.a]=1;
     90                 now.t=u.t+1;
     91                 now.op.push_back("POUR(2,1)");
     92                 q.push(now);
     93             }
     94         }
     95         if(u.a>0&&u.b<B){
     96             node now=u;
     97             now.a=max(0,u.a-B+u.b);
     98             now.b=min(B,u.b+u.a);
     99             if(!vis[now.a][now.b]){
    100                 vis[now.a][now.b]=vis[now.b][now.a]=1;
    101                 now.t=u.t+1;
    102                 now.op.push_back("POUR(1,2)");
    103                 q.push(now);
    104             }
    105         }
    106     }
    107     printf("impossible
    ");
    108 }
    109 int main()
    110 {
    111     int A,B,C;
    112     scanf("%d%d%d",&A,&B,&C);
    113     bfs(A,B,C);
    114     return 0;
    115 }
  • 相关阅读:
    28. css样式中px转rem
    27.用webpack自搭react和vue框架
    26.webpack 入门
    25.redux回顾,redux中的action函数异步
    24.redux
    23.react-router 路由
    22.2、react生命周期与react脚手架(二)
    22.1 、react生命周期(一)
    21.react 组件通信
    const关键字的作用
  • 原文地址:https://www.cnblogs.com/20143605--pcx/p/4734479.html
Copyright © 2011-2022 走看看