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 ito 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)
code:
View Code

#include<stdio.h>#include<string.h>#define clr(x)memset(x,0,sizeof(x))int v[1001][1001];int va;int vb;struct node{int x,y,xu,step;}q[100000];int pre[100000];//1 full 1   2 full 2   3 drop 1   4  drop 2   5 from a to b  6 from b to avoid print(int x){if(pre[x]!=0)        print(pre[x]);//printf("%d\n",q[x].xu);    if(q[x].xu==1)    printf("FILL(1)\n");else if(q[x].xu==2)    printf("FILL(2)\n");else if(q[x].xu==3)    printf("DROP(1)\n");else if(q[x].xu==4)    printf("DROP(2)\n");else if(q[x].xu==5)    printf("POUR(1,2)\n");else printf("POUR(2,1)\n");}int main(){int a,b,c,i,j,front,rear,tmp,v1,v2,res,ans;while(scanf("%d%d%d",&a,&b,&c)!=EOF)    {        va=vb=0;        front=rear=0;        q[rear].xu=0; q[rear].step=0;  q[rear].x=0; q[rear++].y=0;        res=-1;        v[0][0]=1;

        memset(pre,0,sizeof(pre));while(front<=rear)        {           va=q[front].x; vb=q[front].y;//printf("%d %d\n",va,vb);           if(va==c||vb==c) { res=q[front].step;ans=front; break;}if(!v[a][vb]) { v[a][vb]=1; q[rear].x=a;  q[rear].y=vb; q[rear].xu=1; pre[rear]=front;   q[rear++].step=q[front].step+1; }if(!v[va][b]) { v[va][b]=1; q[rear].x=va; q[rear].y=b;  q[rear].xu=2; pre[rear]=front;   q[rear++].step=q[front].step+1; }if(!v[0][vb]) { v[0][vb]=1; q[rear].x=0;  q[rear].y=vb; q[rear].xu=3; pre[rear]=front;   q[rear++].step=q[front].step+1; }if(!v[va][0]) { v[va][0]=1; q[rear].x=va; q[rear].y=0;  q[rear].xu=4; pre[rear]=front;   q[rear++].step=q[front].step+1; }if((va+vb)/b<1){if(!v[0][va+vb]){ v[0][va+vb]=1; q[rear].x=0; q[rear].y=va+vb; q[rear].xu=5; pre[rear]=front; q[rear++].step=q[front].step+1; }           }else if(!v[va-(b-vb)][b]) { v[va-(b-vb)][b]=1; q[rear].x=va-(b-vb); q[rear].y=b; q[rear].xu=5; pre[rear]=front; q[rear++].step=q[front].step+1; }if((va+vb)/a<1){if(!v[va+vb][0]){ v[va+vb][0]=1; q[rear].x=va+vb; q[rear].y=0; q[rear].xu= 6; pre[rear]=front; q[rear++].step=q[front].step+1; }           }else if(!v[a][vb-(a-va)]) { v[a][vb-(a-va)]=1; q[rear].x=a; q[rear].y=vb-(a-va); q[rear].xu=6; pre[rear]=front; q[rear++].step=q[front].step+1; }           front++;        }if(res==-1)printf("impossible\n");else {                printf("%d\n",res);                print(ans);        }    }return 0;}

转载于:https://www.cnblogs.com/dream-wind/archive/2012/03/22/2412181.html

POJ 3414 Pots【广搜】相关推荐

  1. POJ 3414 Pots(深搜并打印路径)

    POJ 3414 Pots(深搜并打印路径) You are given two pots, having the volume of A and B liters respectively. The ...

  2. POJ 3414 Pots【BFS】+ Python

    原题链接: 3414 -- Pots 参考资料:POJ 3414 - Pots | 眈眈探求 POJ 3414 Pots[BFS][图搜] - it610.com 一 特别注意: 1. 每一种操作对应 ...

  3. POJ 3414 Pots(罐子)

    POJ 3414 Pots(罐子) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 You are given two po ...

  4. POJ 3414 Pots

    题目链接 Description You are given two pots, having the volume of A and B liters respectively. The follo ...

  5. poj 1426 栈 广搜

    题目意思很简单 : 就是找一个十进制的数,全由 1 0 组成,是输入那个数字的整数倍 答案有很多种,所以是special judge 需要用到 : 同余取模定理 广搜 因为是广搜我们就可以用栈 当然也 ...

  6. poj 3414 Pots(广搜BFS+路径输出)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=3414 此题和poj ...

  7. poj 3414 Pots BFS

    参考文章:思路公式 代码思路 原来的代码有点瑕疵,这是最新的AC代码 #pragma warning(disable:4996) #include<iostream> #include&l ...

  8. H - Pots POJ - 3414(两个锅互相倒水)

    H - Pots POJ - 3414 题意: (两个锅互相倒水)希望能通过题目中的几种操作使一个锅中的水量为目标水量 bfs 搜索每一步的每种操作,直到所有操作用完,则impossible,否则输出 ...

  9. Pots POJ - 3414(bfs)

    You are given two pots, having the volume of A and B liters respectively. The following operations c ...

最新文章

  1. 计算机应用基础上机操作,计算机应用基础上机操作试题
  2. session和cookie的区别和联系---转载
  3. 正则表达式中空格的危害
  4. postgresql日常操作命令
  5. 帆软正则表达式定义规则
  6. CodeForces - 1311D Three Integers(暴力)
  7. java pnpoly算法_C语言实现的PNPoly算法代码例子
  8. 高效测试必学 | 用pytest生成测试报告
  9. 基于Verilog实现2ASK调制
  10. 中国能源互联网行业十四五前景规划与发展战略格局分析报告2022-2028年版
  11. python 计算变量的IV值
  12. python前戏之量
  13. docker相关的文件配置
  14. No Spring WebApplicationInitializer types detected on classpath
  15. Docker 深入篇之 Build 原理
  16. 这半年,蔡崇信、张勇、彭蕾、井贤栋是怎么扛住马云“绩效”考核的?
  17. Unity录屏功能插件NatCorder使用简记
  18. shardingjdbc 实现读写分离
  19. 如何选购计算机硬件,DIY攒机经验之谈:十年老司机教你组装电脑如何选购硬件...
  20. PHP验证码不能显示的问题

热门文章

  1. vim学习、各类插件配置与安装
  2. Centos7安装mariadb galera cluster数据库集群 详解
  3. 深入浅出JMS(一)——JMS简单介绍
  4. 恶意软件、Rootkit和僵尸网络
  5. 轻松自动化---selenium-webdriver(python) (六)
  6. Firefox 增强版 仅仅5.7 MB
  7. backtrace java_在c file中打出backtrace到某个文件中
  8. PSP 2.0降级至1.5详细教程(转)
  9. 解决weblogic Managed Server启动非常慢的情况
  10. 简化PHP开发的10个工具