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

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 8266   Accepted: 3507   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)

    题意:给定三个数A B C,前两个数代表容量为A和B的容器,有三种操作,FILL,DROP,POUR;求最少经过几次这样的操作可以得到容量为C的水;

    思路:因为每个容器都有FILL,DROP,POUR三种操作,将两个容器的状态用队列来维护,两个容器共有六种操作的可能;
    另一个关键是打印路径,可以用一个结构体数组保存前驱,通过栈再将路径打印出来;
      1 #include<stdio.h>
      2 #include<string.h>
      3 #include<algorithm>
      4 #include<queue>
      5 #include<stack>
      6 //各种状态
      7 #define FILL_A 1;
      8 #define FILL_B 2;
      9 #define DROP_A 3;
     10 #define DROP_B 4;
     11 #define POURA_B 5;
     12 #define POURB_A 6;
     13 using namespace std;
     14 
     15 struct node
     16 {
     17     int x,y;
     18     int step;
     19 };
     20 queue<struct node>que;
     21 
     22 struct Path
     23 {
     24     int x,y;
     25     int way;
     26 }path[110][110];//保存前驱;
     27 
     28 int vis[110][110];
     29 int a,b,c;
     30 
     31 //打印路径
     32 void Print_Path(int x, int y)
     33 {
     34     stack<struct Path>st;
     35     while(!st.empty())
     36         st.pop();
     37         
     38     while(!(x == 0 && y == 0))
     39     {
     40         st.push(path[x][y]);
     41         int sx = path[x][y].x;
     42         int sy = path[x][y].y;
     43         x = sx;
     44         y = sy;
     45     }
     46 
     47     while(!st.empty())
     48     {
     49         switch(st.top().way)
     50         {
     51             case 1:printf("FILL(1)
    ");break;
     52             case 2:printf("FILL(2)
    ");break;
     53             case 3:printf("DROP(1)
    ");break;
     54             case 4:printf("DROP(2)
    ");break;
     55             case 5:printf("POUR(1,2)
    ");break;
     56             case 6:printf("POUR(2,1)
    ");break;
     57         }
     58         st.pop();
     59     }
     60 }
     61 
     62 void bfs()
     63 {
     64     while(!que.empty())
     65         que.pop();
     66     que.push((struct node){0,0,0});//初始状态进队列
     67     vis[0][0] = 1;
     68     while(!que.empty())
     69     {
     70         struct node u = que.front();
     71         que.pop();
     72         if(u.x == c || u.y == c)
     73         {
     74             printf("%d
    ",u.step);
     75             Print_Path(u.x,u.y);
     76             return;
     77         }
     78         //FILL_A
     79         if(u.x < a && !vis[a][u.y])
     80         {
     81             vis[a][u.y] = 1;
     82             que.push((struct node){a,u.y,u.step+1});
     83             path[a][u.y] = (struct Path){u.x,u.y,1};
     84         }
     85         //FILL_B
     86         if(u.y < b && !vis[u.x][b])
     87         {
     88             vis[u.x][b] = 1;
     89             que.push((struct node){u.x,b,u.step+1});
     90             path[u.x][b] = (struct Path){u.x,u.y,2};
     91         }
     92         //DROP_A
     93         if(u.x >0 && !vis[0][u.y])
     94         {
     95             vis[0][u.y] = 1;
     96             que.push((struct node){0,u.y,u.step+1});
     97             path[0][u.y] = (struct Path){u.x,u.y,3};
     98         }
     99         //DROP_B
    100         if(u.y > 0 && !vis[u.x][0])
    101         {
    102             vis[u.x][0] = 1;
    103             que.push((struct node){u.x,0,u.step+1});
    104             path[u.x][0] = (struct Path){u.x,u.y,4};
    105         }
    106         //POURA_B
    107         if(u.x < a && u.y > 0)
    108         {
    109             int tmp = min(a-u.x,u.y);
    110             if(!vis[u.x+tmp][u.y-tmp])
    111             {
    112                 vis[u.x+tmp][u.y-tmp] = 1;
    113                 que.push((struct node){u.x+tmp,u.y-tmp,u.step+1});
    114                 path[u.x+tmp][u.y-tmp] = (struct Path){u.x,u.y,6};
    115             }
    116         }
    117         //POURB_A
    118         if(u.x > 0 && u.y < b)
    119         {
    120             int tmp = min(u.x,b-u.y);
    121             if(!vis[u.x-tmp][u.y+tmp])
    122             {
    123                 vis[u.x-tmp][u.y+tmp] = 1;
    124                 que.push((struct node){u.x-tmp,u.y+tmp,u.step+1});
    125                 path[u.x-tmp][u.y+tmp] = (struct Path){u.x,u.y,5};
    126             }
    127         }
    128     }
    129     printf("impossible
    ");
    130 }
    131 int main()
    132 {
    133     scanf("%d %d %d",&a,&b,&c);
    134     memset(vis,0,sizeof(vis));
    135     bfs();
    136     return 0;
    137 }
    View Code
  • 相关阅读:
    Windows 2003/2008更改远程桌面端口脚本
    如何修改远程桌面连接3389端口
    关于百度地图(离线)使用过程报“Cannot read property 'jb' of undefined ”错误的解决办法
    IIS 错误:处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”
    IIS 错误:由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。
    IIS7错误:不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。锁定是默认设置的(overrideModeDefault="Deny")......
    Jqgrid pager 关于“local” dataType 动态加载数据分页的研究(没好用的研究结果)
    JQGrid导出Excel文件
    Oracle以15分钟为界,统计一天内各时间段的数据笔数
    ORA-01438: 值大于为此列指定的允许精度
  • 原文地址:https://www.cnblogs.com/LK1994/p/3270989.html
Copyright © 2011-2022 走看看