1.4.2 Mother's Milk(dfs)

本文介绍了一种解决农民John的三个容量不同的牛奶桶问题的方法,通过递归深度优先搜索算法,找出当其中一个桶为空时,另一个特定桶中可能存在的牛奶量的所有组合。

Farmer John has three milking buckets of capacity A, B, and C liters. Each of the numbers A, B, and C is an integer from 1 through 20, inclusive. Initially, buckets A and B are empty while bucket C is full of milk. Sometimes, FJ pours milk from one bucket to another until the second bucket is filled or the first bucket is empty. Once begun, a pour must be completed, of course. Being thrifty, no milk may be tossed out.

Write a program to help FJ determine what amounts of milk he can leave in bucket C when he begins with three buckets as above, pours milk among the buckets for a while, and then notes that bucket A is empty.

PROGRAM NAME: milk3

INPUT FORMAT

A single line with the three integers A, B, and C.

SAMPLE INPUT (file milk3.in)

8 9 10

OUTPUT FORMAT

A single line with a sorted list of all the possible amounts of milk that can be in bucket C when bucket A is empty.

SAMPLE OUTPUT (file milk3.out)

1 2 8 9 10

SAMPLE INPUT (file milk3.in)

2 5 10

SAMPLE OUTPUT (file milk3.out)

5 6 7 8 9 10

 

代码:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <algorithm>
 5 using namespace std;
 6 int x,y,z;
 7 int cnt=0;
 8 int v[200100];
 9 int ans[30];
10   
11 void dfs(int a, int b ,int c){
12       
13     if(v[a*10000+b*1000+c]==1)
14     {
15         return;
16     }
17     v[a*10000+b*1000+c]=1;
18     if(a==0){
19         cnt++;
20         ans[c]=1;
21     }
22     // c--->a,b
23     if(c!=0){
24         //c->a
25         if(a+c>=x&&a!=x) dfs(x,b,c-x+a);
26         else if(a+c<x) dfs(a+c,b,0);
27         //c->b
28         if(b+c>=y&&b!=y) dfs(a,y,c-y+b);
29         else if(b+c<y) dfs(a,b+c,0);         
30     }
31     // b--->a,c
32     if(b!=0){
33         //b->a
34         if(b+a>=x&&a!=x) dfs(x,b+a-x,c);
35         else if(b+a<x) dfs(b+a,0,c);
36         //b->c
37         if(b+c>=z&&c!=z) dfs(a,b+c-z,z);
38         else if(b+c<z) dfs(a,0,b+c);
39     }
40     // a--->c,b
41     if(a!=0){
42         //a->c
43         if(a+c>=z&&c!=z) dfs(a+c-z,b,z);
44         else if(a+c<z) dfs(0,b,a+c);
45         //a->b
46         if(b+a>=y&&b!=y) dfs(a+b-y,y,c);
47         else if(a+b<y) dfs(0,a+b,c);
48     }
49 }
50   
51   
52 int main() {
53     freopen("milk3.in","r",stdin);
54     freopen("milk3.out","w",stdout);
55     cin>>x>>y>>z;
56     memset(v,0,sizeof(v));
57     memset(ans,0,sizeof(ans));
58     cnt=0;
59     dfs(0,0,z);
60     int k=0;
61     for(int i=0;i<=20;i++){
62         if(ans[i]){
63             if(k) cout<<" ";
64             k++;
65             cout<<i;
66         }     
67     }
68     cout<<endl;
69     return 0;
70 }

 

转载于:https://www.cnblogs.com/shenyw/p/5160265.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值