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

    总时间限制: 
    1000ms
     
    内存限制: 
    65536kB
    描述

    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.

    输入

    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).

    输出

    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’.

    样例输入
    3 5 4
    样例输出
    6
    FILL(2)
    POUR(2,1)
    DROP(1)
    POUR(2,1)
    FILL(2)
    POUR(2,1)

    总共6种操作,为达到目标状态BFS一下即可。由于需要还原最短路径,在BFS的过程中记录一下达到每个状态前的状态,及进行操作的编号。倒序输出即可。

      1 #include <iostream>
      2 #include <string>
      3 #include <algorithm>
      4 #include <cstring>
      5 #include <cstdio>
      6 #include <cmath>
      7 #include <queue>
      8 #include <set>
      9 #include <map>
     10 #include <list>
     11 #include <vector>
     12 #include <stack>
     13 #define mp make_pair
     14 //#define P make_pair
     15 #define MIN(a,b) (a>b?b:a)
     16 //#define MAX(a,b) (a>b?a:b)
     17 typedef long long ll;
     18 typedef unsigned long long ull;
     19 const int MAX=1e5+5;
     20 const int INF=1e8+5;
     21 using namespace std;
     22 //const int MOD=1e9+7;
     23 typedef pair<ll,int> pii;
     24 const double eps=0.00000001;
     25 
     26 int A,B,C;
     27 bool vi[105][105];
     28 map<pii,pii>pre;
     29 map<pii,int>id;
     30 stack<int>st;
     31 pii bfs()
     32 {
     33     int a,b,a2,b2,num;
     34     vi[0][0]=true;
     35     queue<pii>que;
     36     que.push(mp(0,0));
     37     while(!que.empty())
     38     {
     39         pii tem=que.front(),tem2;
     40         que.pop();
     41         a=tem.first,b=tem.second;
     42         if(a==C||b==C)
     43             return tem;
     44         //fill 1
     45         if(!vi[A][b])
     46         {
     47             tem2=mp(A,b);
     48             pre[tem2]=tem;
     49             id[tem2]=1;
     50             que.push(tem2);
     51             vi[A][b]=true;
     52         }
     53         if(!vi[a][B])
     54         {
     55             tem2=mp(a,B);
     56             id[tem2]=2;
     57             pre[tem2]=tem;
     58             que.push(tem2);
     59             vi[a][B]=true;
     60         }
     61         if(!vi[0][b])
     62         {
     63             tem2=mp(0,b);
     64             id[tem2]=3;
     65             pre[tem2]=tem;
     66             que.push(tem2);
     67             vi[0][b]=true;
     68         }
     69         if(!vi[a][0])
     70         {
     71             tem2=mp(a,0);
     72             id[tem2]=4;
     73             pre[tem2]=tem;
     74             que.push(tem2);
     75             vi[a][0]=true;
     76         }
     77         num=B-b;
     78         b2=b+min(num,a);
     79         a2=a-min(num,a);
     80         if(!vi[a2][b2])
     81         {
     82             tem2=mp(a2,b2);
     83             id[tem2]=5;
     84             pre[tem2]=tem;
     85             que.push(tem2);
     86             vi[a2][b2]=true;
     87         }
     88         num=A-a;
     89         a2=a+min(num,b);
     90         b2=b-min(num,b);
     91         if(!vi[a2][b2])
     92         {
     93             tem2=mp(a2,b2);
     94             id[tem2]=6;
     95             pre[tem2]=tem;
     96             que.push(tem2);
     97             vi[a2][b2]=true;
     98         }
     99     }
    100     return mp(-1,-1);
    101 }
    102 int main()
    103 {
    104     scanf("%d%d%d",&A,&B,&C);
    105     vi[0][0]=true;
    106     pii an=bfs();
    107     if(an.first==-1)
    108         return 0*printf("impossible
    ");
    109     while(!(an.first==0&&an.second==0))
    110     {
    111         st.push(id[an]);
    112         an=pre[an];
    113     }
    114     printf("%d
    ",st.size());
    115     while(!st.empty())
    116     {
    117         int oh=st.top();
    118         st.pop();
    119         if(oh<=2)
    120             printf("FILL(%d)
    ",oh);
    121         else if (oh<=4)
    122             printf("DROP(%d)
    ",oh-2);
    123         else if (oh==5)
    124             printf("POUR(1,2)
    ");
    125         else
    126             printf("POUR(2,1)
    ");
    127     }
    128 }
  • 相关阅读:
    Java追加文件内容的三种方法
    3种方法关闭Java线程
    linux系统登陆过程
    swift正点
    swift简介(东拼西凑,看看就的了)
    变量设置和查看
    进程调度优先级
    获取进程对应的UID登陆用户
    进程会计
    system调用
  • 原文地址:https://www.cnblogs.com/quintessence/p/7211394.html
Copyright © 2011-2022 走看看