ZOJ 1005 Jugs (演绎推理)
2017-12-13 22:39
369 查看
Jugs
Time Limit: 2 Seconds
Memory Limit: 65536 KB Special Judge
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.
Time Limit: 2 Seconds
Memory Limit: 65536 KB Special Judge
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
/* 这是一个单向决策游戏。 每一个状态只有唯一一个有效方向。 判断是否成环即可。 */ #include<iostream> #include<cstdio> using namespace std; int n,m,aim; int P[1000];int step=0; int main() { int a,b,c;c=0; while(scanf("%d%d%d",&n,&m,&aim)!=EOF) { a=0;b=0;step=0; while(b!=m) { a=n;P[++step]=1; if(b+a>m) { P[++step]=5;a-=(m-b);b=m; P[++step]=4;b=0;if(a==aim){P[++step]=5;break;} } if(a==aim){if(b!=0){P[++step]=4;}P[++step]=5;break;} if(a!=0){P[++step]=5;b+=a;a=0;} } if(a!=aim) { a=0;b=0;step=0; while(a!=m) { b=m;P[++step]=3; if(b+a>n) { P[++step]=6;b-=(n-a);a=n;if(b==aim)break; P[++step]=2;a=0; } if(b==aim)break; if(b!=0){P[++step]=6;a+=b;b=0;} } } for(a=1;a<=step;a++) { if(P[a]==1)printf("fill A\n"); else if(P[a]==2)printf("empty A\n"); else if(P[a]==3)printf("fill B\n"); else if(P[a]==4)printf("empty B\n"); else if(P[a]==5)printf("pour A B\n"); else printf("pour B A\n"); } printf("success\n"); } }
相关文章推荐
- zoj 1005 Jugs
- ZOJ Problem Set - 1005 Jugs
- ZOJ 1005 Jugs
- zoj 1005Jugs
- ZOJ 1005 Jugs 杯子倒水问题
- ZOJ 1005 Jugs (倒水问题)
- [ZOJ 1005] Jugs (dfs倒水问题)
- ZOJ Problem Set - 1005 Jugs
- zoj 1005 Jugs
- zoj&nbsp;1005&nbsp;Jugs
- 浙大ZOJ 1005 Jugs问题解决
- ZOJ 1005 Jugs
- zoj 1005 Jugs
- zoj1005 Jugs
- ZOJ1005 Jugs
- bfs ZOJ 1005 Jugs
- ZOJ-1005-Jugs【BFS+记录路径】
- 模拟 zoj 1005 Jugs
- zoj 1005 Jugs
- ZOJ1005 Jugs