浙江大学zoj 1005 jugs解法
来源:互联网 发布:数据割接方案 编辑:程序博客网 时间:2024/04/28 20:31
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 45 7 3
Sample Output
fill Bpour B Aempty Apour B Afill Bpour B Asuccessfill Apour A Bfill Apour A Bempty Bpour A Bsuccess
这个题目可以简化成一个表达式 x*ca-y*cb=n
我的解法是往a里面倒水, 倒入 b 。判断b是不是满了。
如果满了,就倒出一个cb的水,判断此时b==cn。
如果没满,则继续倒水到b中,知道满足跳出条件。
#include<iostream>
using namespace std;
void trip(int ca,int cb,int n)
{
int a=0,b=0;
if(cb==1)
{
cout<<"fill B/nsuccess"<<endl;
return ;
}
while(b!=n)
{
cout<<"fill A"<<endl;
cout<<"pour A B"<<endl;
b+=ca;
if(b>cb)
{
cout<<"empty B"<<endl;
b-=cb;
cout<<"pour A B"<<endl;
}
}
cout<<"success"<<endl;
}
int main()
{
int a,b,n;
while(cin>>a>>b>>n)
{
trip(a,b,n);
}
return 0;
}
- 浙江大学zoj 1005 jugs解法
- ZOJ-1005-Jugs
- ZOJ 1005 Jugs
- ZOJ 1005 Jugs
- zoj 1005 Jugs
- ZOJ 1005 Jugs (DFS)
- zoj 1005 Jugs
- zoj 1005 Jugs
- ZOJ 1005 Jugs 【BFS】
- ZOJ 1005 Jugs
- zoj 1005 jugs
- ZOJ 1005 Jugs
- zoj 1005Jugs
- zoj 1005 Jugs (模拟)
- 模拟 zoj 1005 Jugs
- zoj 1005 Jugs
- zoj 1005 Jugs
- zoj 1005 Jugs
- 转基因食品危害
- JAVA中的运算符
- Recommended Tags for Documentation Comments (C# Programming Guide)
- Eclipse中编译Android工程时出现的问题解决方法。
- struts2 中请求的处理流程
- 浙江大学zoj 1005 jugs解法
- 【C/C++】C++必须知道的基础问题
- 乌龙闯情关13 07-9-13闯闯
- ASP.NET配置文件Web.config 详细解释
- fatal error C1010: unexpected end of file while looking for precompiled header directive。
- java 编写函数 交换两个基本变量的值
- 原来自己知道的那么少~~~~~~~
- ECL电平特点及其应用
- 与人相处很重要的30个原则