一. 原题
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
二. 分析
三. 代码
USER: Qi Shen [maxkibb3] TASK: milk3 LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 4188 KB] Test 2: TEST OK [0.000 secs, 4188 KB] Test 3: TEST OK [0.000 secs, 4188 KB] Test 4: TEST OK [0.000 secs, 4188 KB] Test 5: TEST OK [0.000 secs, 4188 KB] Test 6: TEST OK [0.000 secs, 4188 KB] Test 7: TEST OK [0.000 secs, 4188 KB] Test 8: TEST OK [0.000 secs, 4188 KB] Test 9: TEST OK [0.000 secs, 4188 KB] Test 10: TEST OK [0.000 secs, 4188 KB] All tests OK.
YOUR PROGRAM ('milk3') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.
/*
ID:maxkibb3
LANG:C++
PROG:milk3
*/
#include<cstdio>
int v[3];
int a[3];
bool vis[21][21][21];
int min(int a, int b) {
return (a < b)?a:b;
}
void dfs(int x[3]) {
if(vis[x[0]][x[1]][x[2]]) return;
vis[x[0]][x[1]][x[2]] = true;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(i == j) continue;
if(x[i] != 0 && x[j] != v[j]) {
int amt = min(x[i], v[j] - x[j]);
x[i] -= amt; x[j] += amt;
dfs(x);
x[i] += amt; x[j] -= amt;
}
}
}
}
int main() {
freopen("milk3.in", "r", stdin);
freopen("milk3.out", "w", stdout);
scanf("%d%d%d", &v[0], &v[1], &v[2]);
a[0] = a[1] = 0; a[2] = v[2];
dfs(a);
for(int i = 0; i < v[2]; i++) {
for(int j = 0; j <= v[2]; j++) {
if(vis[0][j][i]) {
printf("%d ", i);
break;
}
}
}
printf("%d\n", v[2]);
return 0;
}