题目


In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.

You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.

A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are

fill A
fill B
empty A
empty B
pour A B
pour B A
success

where "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished.

You may assume that the input you are given does have a solution.

Input

Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.

Output

Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line "success". Output lines start in column 1 and there should be no empty lines nor any trailing spaces.

Sample Input

3 5 4
5 7 3

Sample Output

fill B
pour B A
empty A
pour B A
fill B
pour B A
success
fill A
pour A B
fill A
pour A B
empty B
pour A B
success

解析


从翻译中得知题目要求通过题目所给操作使得B中的水量为限定值,首先需要考虑当B的容量等于C(B为1时B等于C),此时只需倒满B;其次时A的容量为1或者A等于C,这是只需要把A填满并且倒入B直到B中容量合格为止;如果前面两个条件都不满足则选取大于限定值的最小容量的罐子,将其填满并将其倒入另一个罐子,如果那个罐子空了则将其倒满,如果另一个罐子满了就将其倒空,否则继续将水倒入另一个罐子中直到B达到限定值。


代码


#include <iostream>using namespace std;
int ca,cb,a,b,c,t;
void FillA()
{ca=a;cout<<"fill A"<<endl;
}
void FillB()
{cb=b;cout<<"fill B"<<endl;
}
void pourAB ()
{t=ca-(b-cb);if(t<=0){cb+=ca;ca=0;}else{ca=t;cb=b;}cout<<"pour A B"<<endl;
}
void pourBA()
{t=cb-(a-ca);if(t<=0){ca+=cb;cb=0;}else{cb=t;ca=a;}cout<<"pour B A"<<endl;
}
void EmptyA ()
{ca=0;cout<<"empty A"<<endl;
}
void EmptyB ()
{cb=0;cout<<"empty B"<<endl;
}
int main()
{while(cin>>a>>b>>c){ca=0,cb=0;if (b==c){FillB();}else if (a==c||a==1){while(cb<c){FillA();pourAB();}}else if (a<c){FillB();pourBA();while(cb!=c){if(ca==a)EmptyA();if (cb==0)FillB();elsepourBA();}}else if (a>c){FillA();pourAB();while(cb!=c){if(cb==b)EmptyB();if (ca==0)FillA();elsepourAB();}}cout<<"success"<<endl;}return 0;
}

ZOJ Problem 1005 jugs相关推荐

  1. c语言大小写字母互换1005,1005 Jugs,1005jugs

    1005 Jugs,1005jugs 辗转相减,新手入门题.两个容量的灌水题,无所谓最优解. 1 #include 2 3 intmain(){4 intA,B,T,sA,sB;5 while(sca ...

  2. 浙大ZOJ 1005 Jugs问题解决

    今天提交的时候有点小郁闷,第一次提交把编译器选错了(选了GCC),太大意了! 一.工程代码及算法设计注释 ----------------------------------------------- ...

  3. zoj 1005 Jugs BFS

    感想:这是我的第一道oj题,思路我想了很久,感觉建模能力还是不够强啊,理清楚了就好,把各个操作看成一条路,BFS就好 http://acm.zju.edu.cn/onlinejudge/showPro ...

  4. zoj 1005 jugs

    题目内容见zoj1005 由于A,B互素且A的容量小于B,那么可以将B装满并且倒入A中,如果A被装满则将A中的内容全部清空,一直进行下去直到某一刻B中容量恰好等于目标的容量.这种方法能得到正确的结果, ...

  5. ZOJ Problem Set - 1331 Perfect Cubes

    这个题啊,一看就是暴力,暴力,暴力!!! 但是,暴力也是要做到暴力的恰到好处:看题 For hundreds of years Fermat's Last Theorem, which stated ...

  6. ZOJ Problem Set - 1048 Financial Management

    我承认这是一道水的不能再水的题,今天一下就做到了,还是无耻的帖上来吧 #include <stdio.h>int main() {double sum=0;for(int i=1;i< ...

  7. Argus(ZOJ Problem Set - 2212)(优先队列)

    Argus   时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte 总提交: 3            测试通过: 2 描述 A data stream ...

  8. ZOJ Problem Set - 3329 One Person Game

    题目大意:有三个骰子,分别有k1,k2,k3个面. 每次掷骰子,如果三个面分别为a,b,c则分数置0,否则加上三个骰子的分数之和. 当分数大于n时结束.求游戏的期望步数.初始分数为0分析 设 E[i] ...

  9. ZOJ Problem Set - 1730 Crazy Tea Party

    #include<cstdio> int main(){int T,n;scanf("%d",&T);while(T--){scanf("%d&quo ...

最新文章

  1. w7旗舰版计算机替换,win7系统电脑替换全部系统图标的操作方法
  2. Git本地分支版本过低导致的push错误 error: failed to push some refs to ... 及后续amend
  3. python用户登录_python用户登录系统
  4. svchost.exe占用CPU 100%,也可能是这样的原因
  5. 标准的SQL的解析顺序
  6. OpenDayLight Helium实验一 OpenDaylight的C/S模式实验
  7. C语言表达式作业,表达式和语句
  8. 人脸表情识别/人脸检测/ML/DL/图像处理博主
  9. C#中@的用法总结(转)
  10. struts2笔记06-ServletXxxAware接口
  11. Linux内核源码总体介绍—1
  12. Excel函数实战技巧精粹(二)常用函数之VLOOKUP全解
  13. NewWebPick 11下載
  14. Python学习笔记(11)-Python进阶11-函数
  15. PHPstudy实战安装帝国CMS
  16. fullPage.js使用
  17. Python的Code对象
  18. 计算机科学计算器CE符号,计算器的ce和c是什么意思???
  19. STM8S编译错误unable to allocate space for sections/blocks with a total
  20. Mysql部门培训-入门篇

热门文章

  1. python中and和or的惰性求值特点_Python 惰性求值
  2. php cms下载地址,下载phpcms详细步骤
  3. python数据分析实战 fabio nelli百度云_Python数据分析实战 内利(Fabio Nelli),杜春晓 9787115432209...
  4. python时间模块详解(time模块)
  5. 转载:云计算必将极大影响未来--云泉
  6. 秋招经验总结(私企,外企,国企)
  7. 【机器人学、机器人控视觉与控制】四足机器人MATLAB仿真
  8. python 气泡图 聚类_R可视化 | 气泡图
  9. 虚拟贝司拓展音源-Toontrack Acoustic EBX
  10. 安装spacy遇到的问题