博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Jugs(回溯法)
阅读量:6976 次
发布时间:2019-06-27

本文共 4022 字,大约阅读时间需要 13 分钟。

ZOJ Problem Set - 1005
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.

 

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 思路:用二元组(a,b)记录每一次操作后A和B的状态(gallon数),一个状态元组不能重复出现,不然会不断循环。 AC Code:
1 #include 
2 #include
3 #include
4 5 using namespace std; 6 7 const int SZ = 10000; 8 int op[SZ]; 9 //op is for "operation". 1:fill A,2:fill B,3:empty A,4:empty B,5:pour A B,6:pour B A,0:success 10 int ca, cb, n; 11 map
, bool> tag; 12 13 14 bool DFS(int a, int b, int k) //a和b分别是A和B的gallon数,k是op的下标 15 { 16 pair
p = make_pair(a, b); 17 if(tag[p]) return false; 18 tag[p] = true; 19 if(b == n) 20 { 21 op[k] = 0; 22 return true; 23 } 24 if(a < ca) //fill A 25 { 26 op[k] = 1; 27 if(DFS(ca, b, k + 1)) return true; 28 } 29 if(b < cb) //fill B 30 { 31 op[k] = 2; 32 if(DFS(a, cb, k + 1)) return true; 33 } 34 if(a) //empty A 35 { 36 op[k] = 3; 37 if(DFS(0, b, k + 1)) return true; 38 } 39 if(b) //empty B 40 { 41 op[k] = 4; 42 if(DFS(a, 0, k + 1)) return true; 43 } 44 if(a && b < cb) //pour A B 45 { 46 op[k] = 5; 47 int ra, rb; 48 if(a > cb - b) 49 { 50 rb = cb; 51 ra = a - (cb - b); 52 } 53 else 54 { 55 ra = 0; 56 rb = b + a; 57 } 58 if(DFS(ra, rb, k + 1)) return true; 59 } 60 if(b && a < ca) //pour B A 61 { 62 op[k] = 6; 63 int ra, rb; 64 if(b > ca - a) 65 { 66 ra = ca; 67 rb = b - (ca - a); 68 } 69 else 70 { 71 rb = 0; 72 ra = b + a; 73 } 74 if(DFS(ra, rb, k + 1)) return true; 75 } 76 return false; 77 } 78 79 int main() 80 { 81 while(scanf("%d %d %d", &ca, &cb, &n) != EOF) 82 { 83 tag.clear(); 84 bool flag = DFS(0, 0, 0); 85 for(int i = 0; op[i]; i++) 86 { 87 switch(op[i]) 88 { 89 case 1: 90 puts("fill A"); 91 break; 92 case 2: 93 puts("fill B"); 94 break; 95 case 3: 96 puts("empty A"); 97 break; 98 case 4: 99 puts("empty B");100 break;101 case 5:102 puts("pour A B");103 break;104 case 6:105 puts("pour B A");106 break;107 default:108 break;109 }110 }111 puts("success");112 }113 return 0;114 }

 

 
 

转载地址:http://brkpl.baihongyu.com/

你可能感兴趣的文章
用Swift实现一款天气预报APP(三)
查看>>
HttpApplication事件&ASP.NET页面周期
查看>>
春天。
查看>>
MapReduce对交易日志进行排序的Demo(MR的二次排序)
查看>>
Android -- Fragment注意事项
查看>>
Android APP测试的日志文件抓取
查看>>
DevDays2012 开发者日中文版资料下载
查看>>
ios开发学习-手势交互(Gesture)效果源码分享
查看>>
推荐15个国外使用 CSS3 制作的漂亮网站
查看>>
iOS:转载:UIControl的使用
查看>>
大叔也说并行和串行`性能提升N倍(N由操作系统位数和cpu核数决定)
查看>>
对ListenSocket 的研究(四)
查看>>
JQuery:JQuery 中的CSS()方法
查看>>
Linux内核跟踪之trace框架分析【转】
查看>>
内存分配器memblock【转】
查看>>
C# BackgroundWorker 详解
查看>>
IOS自定义表格UITableViewCell
查看>>
[Linux] ubuntu 格式化u盘
查看>>
一个COM示例程序
查看>>
通过改进算法来优化程序性能的真实案例(Ransac)
查看>>