描述
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<<" "; } }