Mother's Milk
描述
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.
輸入
A single line with the three integers A, B, and C.
輸出
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.
樣例輸入
8 9 10
2 5 10
樣例輸出
1 2 8 9 10
5 6 7 8 9 10
題目大意:有A,B,C三個杯子,A,B為空杯子,C杯子開始裝滿了牛奶。 一個杯子裏的牛奶可以往另外兩個杯子裏到(直到杯內牛奶倒完或者另一個杯子牛奶裝滿)
求A杯子空的時候C杯子牛奶可能的情況。 解題思路:深搜
#include <bits/stdc++.h> using namespace std; int a[65][65][65],b[65]; int n,m,k; void dfs(int A,int B,int C) { if(a[A][B][C]) return; a[A][B][C]=1; if(A==0) b[C]=1; if(A>0) { int mm=min(m-B,A); dfs(A-mm,B+mm,C); mm=min(k-C,A); dfs(A-mm,B,C+mm); } if(B>0) { int mm=min(B,n-A); dfs(A+mm,B-mm,C); mm=min(B,k-C); dfs(A,B-mm,C+mm); } if(C>0) { int mm=min(C,n-A); dfs(A+mm,B,C-mm); mm=min(C,m-B); dfs(A,B+mm,C-mm); } } int main() { while(cin>>n>>m>>k) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); dfs(0,0,k); int f=1; for(int i=0;i<=60;i++) { if(b[i]&&f) { cout<<i; f=0; } else if(b[i]) cout<<" "<<i; } cout<<"\n"; } }
Mother's Milk