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

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

    Source

    Northeastern Europe 2002, Western Subregion
     
     
     1 #include<iostream>
     2 #include<string>
     3 #include<cstdio>
     4 #include<cstring>
     5 
     6 using namespace std;
     7 
     8 const int maxn=110;
     9 
    10 string op[7]={"","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(2,1)","POUR(1,2)"};
    11 
    12 int l,r;
    13 int a,b,c;
    14 int vis[maxn][maxn],step[maxn*maxn];
    15 
    16 struct node{
    17     int x,y;
    18     int opr;
    19     int pre;
    20 }info[maxn*maxn];
    21 
    22 void Solve(int x,int y,int opr){
    23     if(vis[x][y])
    24         return ;
    25     vis[x][y]=1;
    26     info[r].x=x;
    27     info[r].y=y;
    28     info[r].opr=opr;
    29     info[r].pre=l;
    30     r++;
    31 }
    32 
    33 void Print(){
    34     int ans=0;
    35     while(l!=0){
    36         step[ans++]=info[l].opr;
    37         l=info[l].pre;
    38     }
    39     printf("%d\n",ans);
    40     for(int i=ans-1;i>=0;i--)
    41         cout<<op[step[i]]<<endl;
    42 }
    43 
    44 void BFS(){
    45     info[0].x=0;
    46     info[0].y=0;
    47     vis[0][0]=1;
    48     l=0;
    49     r=1;
    50     int tx,ty;
    51     while(l!=r){
    52         if(info[l].x==c || info[l].y==c){
    53             Print();
    54             return ;
    55         }
    56 
    57         tx=a;
    58         ty=info[l].y;
    59         Solve(tx,ty,1);
    60 
    61         tx=info[l].x;
    62         ty=b;
    63         Solve(tx,ty,2);
    64 
    65         tx=0;
    66         ty=info[l].y;
    67         Solve(tx,ty,3);
    68 
    69         tx=info[l].x;
    70         ty=0;
    71         Solve(tx,ty,4);
    72 
    73         tx=info[l].x+min(a-info[l].x,info[l].y);
    74         ty=info[l].y-min(a-info[l].x,info[l].y);
    75         Solve(tx,ty,5);
    76 
    77         tx=info[l].x-min(b-info[l].y,info[l].x);
    78         ty=info[l].y+min(b-info[l].y,info[l].x);
    79         Solve(tx,ty,6);
    80 
    81         l++;
    82     }
    83     if(l>=r)
    84         printf("impossible\n");
    85 }
    86 
    87 int main(){
    88 
    89     //freopen("input.txt","r",stdin);
    90 
    91     while(~scanf("%d%d%d",&a,&b,&c)){
    92         memset(vis,0,sizeof(vis));
    93         BFS();
    94     }
    95     return 0;
    96 }
  • 相关阅读:
    学习流程
    Linux计划任务 定时任务 Crond 配置详解 crond计划任务调试 sh x 详解 JAVA脚本环境变量定义
    asp.net 将Excel中某个工作簿的数据导入到DataTable方法
    在浏览器输入url,发生了什么?BSC结构图(百度搜索关键字发生了什么?)
    在C#中使用属性控件添加属性窗口
    转载:面向对象在数据库应用程序中的应用(dotNet)
    中国软件:10个人20年坎坷路
    客户端回调
    鼠标放在图片连接上面,预览图片
    一个合格的程序员该做的事情
  • 原文地址:https://www.cnblogs.com/jackge/p/3019976.html
Copyright © 2011-2022 走看看