Skip to analysis
This is a explanation of this problem from USACO's training website. I have converted it to markdown. Please do not just copy code; you will not learn anything; at least type it out and understand so you can do it yourself in the future!
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
CODE
Java
C++
Pascal
ANALYSIS
Russ Cox
We use a simple depth-first search to find all the possible states for the three buckets, pruning the search by not researching from states we’ve seen before.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#define MAX 20
typedef struct State State;
struct State {
int a[3];
};
int seen[MAX+1][MAX+1][MAX+1];
int canget[MAX+1];
State
state(int a, int b, int c)
{
State s;
s.a[0] = a;
s.a[1] = b;
s.a[2] = c;
return s;
}
int cap[3];
/* pour from bucket "from" to bucket "to" */
State
pour(State s, int from, int to)
{
int amt;
amt = s.a[from];
if(s.a[to]+amt > cap[to])
amt = cap[to] - s.a[to];
s.a[from] -= amt;
s.a[to] += amt;
return s;
}
void
search(State s)
{
int i, j;
if(seen[s.a[0]][s.a[1]][s.a[2]])
return;
seen[s.a[0]][s.a[1]][s.a[2]] = 1;
if(s.a[0] == 0) /* bucket A empty */
canget[s.a[2]] = 1;
for(i=0; i<3; i++)
for(j=0; j<3; j++)
search(pour(s, i, j));
}
void
main(void)
{
int i;
FILE *fin, *fout;
char *sep;
fin = fopen("milk3.in", "r");
fout = fopen("milk3.out", "w");
assert(fin != NULL && fout != NULL);
fscanf(fin, "%d %d %d", &cap[0], &cap[1], &cap[2]);
search(state(0, 0, cap[2]));
sep = "";
for(i=0; i<=cap[2]; i++) {
if(canget[i]) {
fprintf(fout, "%s%d", sep, i);
sep = " ";
}
}
fprintf(fout, "\n");
exit(0);
}
Ran Pang
Ran Pang from Canada sends this non-recursive DP solution:
#include<stdio.h>
int m[21][21][21];
int poss[21];
int A, B, C;
int main(void) {
int i,j,k;
int flag;
FILE* in=fopen("milk3.in","r");
fscanf(in, "%d %d %d",&A, &B, &C);
fclose(in);
for(i=0;i<21;i++)
for(j=0;j<21;j++)
for(k=0;k<21;k++)
m[i][j][k]=0;
for(i=0;i<21;i++)
poss[i]=0;
m[0][0][C]=1;
for(flag=1;flag;) {
flag=0;
for(i=0;i<=A;i++)
for(j=0;j<=B;j++)
for(k=0;k<=C;k++) {
if(m[i][j][k]) {
if(i==0) poss[k]=1;
if(i) {
if(j<B) {
if(B-j>=i) {
if( m[0][j+i][k]==0) {
m[0][j+i][k]=1;
flag=1;
}
} else {
if( m[i-(B-j)][B][k] == 0) {
m[i-(B-j)][B][k] =1;
flag=1;
}
}
}
if(k<C) {
if(C-k>=i) {
if( m[0][j][k+i]==0) {
m[0][j][k+i]=1;
flag=1;
}
}
else {
if( m[i-(C-k)][j][C] == 0) {
m[i-(C-k)][j][C] =1;
flag=1;
}
}
}
}
if(j) {
if(i<A) {
if(A-i>=j) {
if( m[i+j][0][k]==0) {
m[i+j][0][k]=1;
flag=1;
}
} else {
if( m[A][j-(A-i)][k] == 0) {
m[A][j-(A-i)][k] =1;
flag=1;
}
}
}
if(k<C) {
if(C-k>=j) {
if( m[i][0][k+j]==0) {
m[i][0][k+j]=1;
flag=1;
}
} else {
if( m[i][j-(C-k)][C] == 0) {
m[i][j-(C-k)][C] =1;
flag=1;
}
}
}
}
if(k) {
if(i<A) {
if(A-i>=k) {
if( m[i+k][j][0]==0) {
m[i+k][j][0]=1;
flag=1;
}
} else {
if( m[A][j][k-(A-i)] == 0) {
m[A][j][k-(A-i)] =1;
flag=1;
}
}
}
if(j<B) {
if(B-j>=k) {
if( m[i][j+k][0]==0) {
m[i][j+k][0]=1;
flag=1;
}
} else {
if( m[i][B][k-(B-j)] == 0) {
m[i][B][k-(B-j)] =1;
flag=1;
}
}
}
}
}
}
}
{
FILE* out=fopen("milk3.out", "w");
for(i=0;i<21;i++) {
if(poss[i]) {
fprintf(out,"%d",i);
i++;
break;
}
}
for(;i<21;i++) {
if(poss[i]) {
fprintf(out, " %d", i);
}
}
fprintf(out,"\n");
}
return 0;
}
Daniel Jasper
Daniel Jasper from Germany writes:
Both other solutions (recursive and non-recursive) use a 3D-array to store the states, so that the memory usage is O(N3). However a 2D Array and O(N2) would be enough since a state is uniquely defined by the amount of milk in bucket B and C. The amount of milk in bucket A is size-of-C minus amount-in-C minus amount-in-B. This solution works with it, and is a little bit shorter (though not more elegant):
#include <stdio.h>
int A, B, C;
int CB[21][21]; // All states
void readFile() {
FILE *f;
f = fopen("milk3.in", "r");
fscanf(f, "%d%d%d", &A, &B, &C);
fclose(f);
}
void writeFile() {
FILE *f; int i;
f = fopen("milk3.out", "w");
for(i = 0; i <= C; i++) {
if(CB[i][C - i] == 1) {
if((i != C-B) && (i != 0)) fprintf(f, " ");
fprintf(f, "%d", i);
}
}
fprintf(f, "\n");
fclose(f);
}
// do brute-force search, c/b: current state
void search(int c, int b) {
int a;
if(CB[c][b] == 1) return; // already searched
CB[c][b] = 1;
a = C-b-c; // calc amount in A
// do all moves:
// c->b
if(B < c+b) search(c - (B - b), B);
else search(0, c + b);
// b->c
if(C < c+b) search(C, b - (C - c));
else search(c + b, 0);
// c->a
if(A < c+a) search(c - (A - a), b);
else search(0, b);
// a->c
if(C < c+a) search(C, b);
else search(c + a, b);
// b->a
if(A < b+a) search(c, b - (A - a));
else search(c, 0);
// a->b
if(B < b+a) search(c, B);
else search(c, b + a);
}
int main () {
readFile();
search(C, 0);
writeFile();
return 0;
}
Back to top