zoukankan      html  css  js  c++  java
  • 广搜+输出路径 POJ 3414 Pots

    POJ 3414 Pots

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 13547   Accepted: 5718   Special Judge

    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)
      1 /*和之前的那道倒水题目十分相似,唯一的难点就是输出倒水的方式,我们可以记录每一个搜到的状态的前一状态在队列中的编号和,该状态与前一状态的关系,再递归输出结果即可*/
      2 #include<iostream>
      3 using namespace std;
      4 #include<cstdio>
      5 #include<cstring>
      6 #define N 10010
      7 int head=-1,tail=-1;
      8 int a,b,c;
      9 bool flag[101][101]={0};
     10 struct node{
     11     int a1,b1,pre,dis,relat;
     12 }que[N];
     13 int bfs()
     14 {
     15     while(head<tail)
     16     {
     17         ++head;
     18         if(que[head].a1==c||que[head].b1==c)
     19         {
     20             return head;
     21         }
     22         if(!flag[que[head].a1][0])
     23         {
     24             flag[que[head].a1][0]=true;
     25             node nex;
     26             nex.a1=que[head].a1;nex.b1=0;
     27             nex.dis=que[head].dis+1;
     28             nex.pre=head;
     29             nex.relat=4;
     30             ++tail;
     31             que[tail]=nex;
     32         }
     33         if(!flag[0][que[head].b1])
     34         {
     35             flag[0][que[head].b1]=true;
     36             node nex;
     37             nex.a1=0;nex.b1=que[head].b1;
     38             nex.dis=que[head].dis+1;
     39             nex.pre=head;
     40             nex.relat=3;
     41             ++tail;
     42             que[tail]=nex;
     43         }
     44         if(!flag[que[head].a1][b])
     45         {
     46             flag[que[head].a1][b]=true;
     47             node nex;
     48             nex.a1=que[head].a1;nex.b1=b;
     49             nex.dis=que[head].dis+1;
     50             nex.pre=head;
     51             nex.relat=2;
     52             ++tail;
     53             que[tail]=nex;
     54         }
     55         if(!flag[a][que[head].b1])
     56         {
     57             flag[a][que[head].b1]=true;
     58             node nex;
     59             nex.a1=a;nex.b1=que[head].b1;
     60             nex.dis=que[head].dis+1;
     61             nex.pre=head;
     62             nex.relat=1;
     63             ++tail;
     64             que[tail]=nex;
     65         }
     66         if(que[head].a1>=(b-que[head].b1)&&!flag[que[head].a1-(b-que[head].b1)][b])
     67         {
     68             flag[que[head].a1-(b-que[head].b1)][b]=true;
     69             node nex;
     70             nex.a1=que[head].a1-(b-que[head].b1);nex.b1=b;
     71             nex.dis=que[head].dis+1;
     72             nex.pre=head;
     73             nex.relat=5;
     74             ++tail;
     75             que[tail]=nex;
     76         }
     77         if(que[head].a1<(b-que[head].b1)&&!flag[0][que[head].a1+que[head].b1])
     78         {
     79             flag[0][que[head].a1+que[head].b1]=true;
     80             node nex;
     81             nex.a1=0;nex.b1=que[head].a1+que[head].b1;
     82             nex.dis=que[head].dis+1;
     83             nex.pre=head;
     84             nex.relat=5;
     85             ++tail;
     86             que[tail]=nex;
     87         }
     88         if(que[head].b1>=(a-que[head].a1)&&!flag[a][que[head].b1-(a-que[head].a1)])
     89         {
     90             flag[a][que[head].b1-(a-que[head].a1)]=true;
     91             node nex;
     92             nex.a1=a;nex.b1=que[head].b1-(a-que[head].a1);
     93             nex.dis=que[head].dis+1;
     94             nex.pre=head;
     95             nex.relat=6;
     96             ++tail;
     97             que[tail]=nex;
     98         }
     99         if(que[head].b1<(a-que[head].a1)&&!flag[que[head].a1+que[head].b1][0])
    100         {
    101             flag[que[head].a1+que[head].b1][0]=true;
    102             node nex;
    103             nex.a1=que[head].a1+que[head].b1;nex.b1=0;
    104             nex.dis=que[head].dis+1;
    105             nex.pre=head;
    106             nex.relat=6;
    107             ++tail;
    108             que[tail]=nex;
    109         }
    110     }
    111     return -1;
    112 }
    113 void out(int temp)
    114 {
    115     if(que[que[temp].pre].pre!=-1)
    116       out(que[temp].pre);
    117     if(que[temp].relat==1)
    118       printf("FILL(1)
    ");
    119     else if(que[temp].relat==2)
    120            printf("FILL(2)
    ");
    121          else if(que[temp].relat==3)
    122                 printf("DROP(1)
    ");
    123               else if(que[temp].relat==4)
    124                      printf("DROP(2)
    ");
    125                    else if(que[temp].relat==5)
    126                           printf("POUR(1,2)
    ");
    127                         else printf("POUR(2,1)
    ");
    128 }
    129 /*
    130 FILL(2)
    131 POUR(2,1)
    132 DROP(1)
    133 POUR(2,1)
    134 FILL(2)
    135 POUR(2,1)
    136 */
    137 int main()
    138 {
    139     scanf("%d%d%d",&a,&b,&c);
    140     ++tail;
    141     que[tail].a1=0;que[tail].b1=0;
    142     que[tail].dis=0;que[tail].pre=-1;
    143     flag[0][0]=true;
    144     int temp=bfs();
    145     if(temp==-1)
    146      printf("impossible
    ");
    147     else {
    148         printf("%d
    ",que[head].dis);
    149         out(temp);
    150     }
    151     return 0;
    152 }
  • 相关阅读:
    Spring Security简单的登陆验证授权
    汽车之家汽车品牌Logo信息抓取 DotnetSpider实战[三]
    汽车之家店铺商品详情数据抓取 DotnetSpider实战[二]
    如何解决 MySQL报错:ERROR 1045 (28000)
    linux三剑客grep|sed|awk实践
    VMware中Linux启动时***Host SMBus controller not enabled的解决方法
    selenium初探:WebDriverException解决方法探索(以Chrome浏览器|IE浏览器|Edge浏览器为例)
    Windows10 64位 Python2.7 Matplotlib安装
    关于 水平制表符 Horizontal Tab (TAB)
    leetcode每日解题思路 221 Maximal Square
  • 原文地址:https://www.cnblogs.com/c1299401227/p/5576176.html
Copyright © 2011-2022 走看看