1.4.4 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.
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
{ ID:makeeca1 PROG: milk3 LANG: PASCAL } program milk3; var a,b,c,x,y,z,i:longint; can:array[0..20]of boolean; f:array[0..20,0..20]of boolean; function max(xx,yy:longint):longint; begin if xx>yy then exit(xx)else exit(yy);end; function min(xx,yy:longint):longint; begin if xx>yy then exit(yy)else exit(xx);end; procedure dfs(x,y,z:longint); begin if f[x,y]then exit; f[x,y]:=true; if (x=0) then can[z]:=true; if (x>0)and(y<b) then dfs(max(0,x+y-b),min(b,x+y),z); if (x>0)and(z<c) then dfs(max(0,x+z-c),y,min(c,x+z)); if (y>0)and(x<a) then dfs(min(a,y+x),max(0,y+x-a),z); if (y>0)and(z<c) then dfs(x,max(0,y+z-c),min(c,y+z)); if (z>0)and(x<a) then dfs(min(a,z+x),y,max(0,z+x-a)); if (z>0)and(y<b) then dfs(x,min(b,z+y),max(0,z+y-b)); end; begin assign(input,'milk3.in');reset(input); assign(output,'milk3.out');rewrite(output); readln(a,b,c); fillchar(can,sizeof(can),0); fillchar(f,sizeof(f),0); dfs(0,0,c); for i:=0 to c-1 do if can[i]then write(i,' '); writeln(c); close(input);close(output); end.