博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1.4.2 Mother's Milk(dfs)
阅读量:4921 次
发布时间:2019-06-11

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

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 
2 #include
3 #include
4 #include
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,b23 if(c!=0){24 //c->a25 if(a+c>=x&&a!=x) dfs(x,b,c-x+a);26 else if(a+c
b28 if(b+c>=y&&b!=y) dfs(a,y,c-y+b);29 else if(b+c
a,c32 if(b!=0){33 //b->a34 if(b+a>=x&&a!=x) dfs(x,b+a-x,c);35 else if(b+a
c37 if(b+c>=z&&c!=z) dfs(a,b+c-z,z);38 else if(b+c
c,b41 if(a!=0){42 //a->c43 if(a+c>=z&&c!=z) dfs(a+c-z,b,z);44 else if(a+c
b46 if(b+a>=y&&b!=y) dfs(a+b-y,y,c);47 else if(a+b
>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<

 

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

你可能感兴趣的文章
米洛个人修炼术:情绪的四种常用处理方式,其实都是有问题的
查看>>
[翻译] Virtual method interception 虚方法拦截
查看>>
--- git-svn 使用环境和步骤
查看>>
flutter AS 打包
查看>>
Python webpy微信公众号开发之 回复图文消息
查看>>
ubuntu多版本cuda并存与切换【两个博客链接】
查看>>
html5新特性之DOCTYPE声明
查看>>
POJ 3299 Humidex 难度:0
查看>>
快速切题 poj3414 Pots
查看>>
Linux 常用命令
查看>>
五家共井(第1届第3题)
查看>>
c文件操作
查看>>
《Spring In Action》 读书笔记(2) -- bean装配 ...
查看>>
很好很強大..
查看>>
Oracle之子查询:Top-N问题
查看>>
PAT:1011. A+B和C (15) AC
查看>>
JS中的内置对象
查看>>
Android--在Android应用中愉快地写C/C++代码(转)
查看>>
IOSUIcontrol事件
查看>>
docker 部署spring.boot项目【一】(引用外部配置文件)
查看>>