1.4.2 Mother's Milk(dfs)
2016-01-26 14:21
323 查看
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
代码:
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
代码:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; int x,y,z; int cnt=0; int v[200100]; int ans[30]; void dfs(int a, int b ,int c){ if(v[a*10000+b*1000+c]==1) { return; } v[a*10000+b*1000+c]=1; if(a==0){ cnt++; ans[c]=1; } // c--->a,b if(c!=0){ //c->a if(a+c>=x&&a!=x) dfs(x,b,c-x+a); else if(a+c<x) dfs(a+c,b,0); //c->b if(b+c>=y&&b!=y) dfs(a,y,c-y+b); else if(b+c<y) dfs(a,b+c,0); } // b--->a,c if(b!=0){ //b->a if(b+a>=x&&a!=x) dfs(x,b+a-x,c); else if(b+a<x) dfs(b+a,0,c); //b->c if(b+c>=z&&c!=z) dfs(a,b+c-z,z); else if(b+c<z) dfs(a,0,b+c); } // a--->c,b if(a!=0){ //a->c if(a+c>=z&&c!=z) dfs(a+c-z,b,z); else if(a+c<z) dfs(0,b,a+c); //a->b if(b+a>=y&&b!=y) dfs(a+b-y,y,c); else if(a+b<y) dfs(0,a+b,c); } } int main() { freopen("milk3.in","r",stdin); freopen("milk3.out","w",stdout); cin>>x>>y>>z; memset(v,0,sizeof(v)); memset(ans,0,sizeof(ans)); cnt=0; dfs(0,0,z); int k=0; for(int i=0;i<=20;i++){ if(ans[i]){ if(k) cout<<" "; k++; cout<<i; } } cout<<endl; return 0; }
相关文章推荐
- mysql的grant命令
- php 分页
- SVN常用命令
- YTU 2837: 编程题B-狐狸算卦
- 实体操作类,直接操作数据库无映射,缓存同步更新,单列模式
- iOS完美实现输入框随键盘无缝滑动的效果_KLCPopKeyBoard
- logback(三)
- sourcegrid统计报表画法以及EXCEL导出内容代码完全版
- php 错误和异常处理
- VS2012+win7x64+OpenCV2.4.2安装
- unable to write jarlist cache file
- SharePoint 2013 快速提高性能的临时方案--配置分布式缓存
- 1016. 部分A+B (15)
- Orange the Apple
- 计算机视觉(ComputerVision, CV)相关领域的网站链接
- 微信JSSDK分享功能详解
- logback(二)
- Activity启动模式详解(二)--->singleTask
- Linux常用命令ps,kill
- shell 编程