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
解题思路:果断DFS啊,总共六种状态,C->A,C->B,B->A,A->B,A->C,B->C。每次要么被灌的桶装满或者原桶空了。要求按升序输出,由于桶容量最大才20,所以C桶的剩余量用一个20的数组标记一下就可以了。
偶滴代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 ID:spcjv51 3 PROG:milk3 4 LANG:C 5 */ 6 #include<stdio.h> 7 int f[25][25][25]; 8 int path[25]; 9 int a,b,c,total; 10 void init() 11 { 12 scanf("%d%d%d",&a,&b,&c); 13 memset(f,0,sizeof(f)); 14 memset(path,0,sizeof(path)); 15 total=0; 16 } 17 void search(int x,int y,int z) 18 { 19 if (f[x][y][z]) return; 20 f[x][y][z]=1; 21 if(x==0) path[z]=1; 22 if(x+z<=a) search(x+z,y,0); 23 else search(a,y,z-a+x); 24 if(y+z<=b) search(x,y+z,0); 25 else search(x,b,z+y-b); 26 if(x+y<=a) search(x+y,0,z); 27 else search(a,y+x-a,z); 28 if(x+y<=b) search(0,x+y,z); 29 else search(x+y-b,b,z); 30 if(x+z<=c) search(0,y,x+z); 31 else search(x+z-c,y,c); 32 if(y+z<=c) search(x,0,y+z); 33 else search(x,y+z-c,c); 34 } 35 void output() 36 { 37 int i,flag; 38 flag=0; 39 for(i=0; i<=20; i++) 40 if(path[i]&&flag==0) 41 { 42 printf("%d",i); 43 flag=1; 44 } 45 else if (path[i]&&flag!=0) printf(" %d",i); 46 printf("\n"); 47 } 48 int main(void) 49 { 50 51 freopen("milk3.in","r",stdin); 52 freopen("milk3.out","w",stdout); 53 init(); 54 search(0,0,c); 55 output(); 56 return 0; 57 }