[USACO]Mother's Milk【JAVA】

Mother's Milk

[筆者CSDN傳送陣]:http://blog.csdn.net/u013247524/article/details/24390101

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

  1.   
  2. import java.io.*;  
  3. import java.util.StringTokenizer;  
  4. import java.util.Collection;  
  5. import java.util.TreeSet;  
  6.   
  7. public class milk3 {  
  8.     public static int A;  
  9.     public static int B;  
  10.     public static int C;  
  11.     public static boolean[][][] found;  
  12.     public static Collection set;  
  13.       
  14.     public static void main(String[] args) throws IOException {  
  15.         // Use BufferedReader rather than RandomAccessFile; it's much faster  
  16.         BufferedReader f = new BufferedReader(new FileReader("milk3.in"));  
  17.         // input file name goes above  
  18.         PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(  
  19.                 "milk3.out")));  
  20.           
  21.         StringTokenizer st = new StringTokenizer(f.readLine());  
  22.           
  23.         A = Integer.parseInt(st.nextToken());  
  24.         B = Integer.parseInt(st.nextToken());  
  25.         C = Integer.parseInt(st.nextToken());  
  26.         found = new boolean[A+1][B+1][C+1];  
  27.         set = new TreeSet();  
  28.           
  29.         DFS(0,0,C);  
  30.           
  31.         int flag = 0;  
  32.         for(int i: set){  
  33.             if(!(flag == 0)){  
  34.                 out.print(" ");  
  35.             }else{  
  36.                 flag++;  
  37.             }  
  38.             out.print(i);  
  39.         }  
  40.         out.println();  
  41.           
  42.         out.close(); // close the output file  
  43.         System.exit(0); // don't omit this!  
  44.     }  
  45.       
  46.     public static void DFS(int a, int b, int c){  
  47.         if(!found[a][b][c]){  
  48.             found[a][b][c] = true;  
  49.             if(a == 0){  
  50.                 set.add(c);  
  51.             }  
  52.             if(a<=B-b)DFS(0,a+b, c);     //A=>B  
  53.             else DFS(a-(B-b), B, c);  
  54.             if(a<=C-c)DFS(0, b, a+c);        //A=>C  
  55.             else DFS(a-(C-c), b, C);  
  56.               
  57.             if(b<=A-a)DFS(a+b,0, c);     //B=>A  
  58.             else DFS(A, b-(A-a), c);  
  59.             if(b<=C-c)DFS(a,0, c+b);     //B=>C  
  60.             else DFS(a, b-(C-c), C);  
  61.               
  62.             if(c<=A-a)DFS(a+c,b, 0);     //C=>A  
  63.             else DFS(A, b, c-(A-a));  
  64.             if(c<=B-b)DFS(a,b+c, 0);     //C=>B  
  65.             else DFS(a, B, c-(B-b));  
  66.               
  67.               
  68.         }  
  69.     }  
  70. }  



AC結果:

USER: Singles Xin [xl1993a1]
TASK: milk3
LANG: JAVA

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.101 secs, 32588 KB]
   Test 2: TEST OK [0.122 secs, 30540 KB]
   Test 3: TEST OK [0.115 secs, 32588 KB]
   Test 4: TEST OK [0.108 secs, 30540 KB]
   Test 5: TEST OK [0.108 secs, 30540 KB]
   Test 6: TEST OK [0.115 secs, 30540 KB]
   Test 7: TEST OK [0.122 secs, 30540 KB]
   Test 8: TEST OK [0.115 secs, 31564 KB]
   Test 9: TEST OK [0.101 secs, 30540 KB]
   Test 10: TEST OK [0.115 secs, 32588 KB]

All tests OK.

Your program ('milk3') produced all correct answers! This is your submission #4 for this problem. Congratulations!

Here are the test data inputs:

------- test 1 ----
2 5 10
------- test 2 ----
20 20 20
------- test 3 ----
5 11 15
------- test 4 ----
2 12 20
------- test 5 ----
19 4 11
------- test 6 ----
5 11 13
------- test 7 ----
3 20 20
------- test 8 ----
7 16 20
------- test 9 ----
20 10 9
------- test 10 ----
7 12 18
Keep up the good work!
Thanks for your submission!

給的答案爲c的,就略過了
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章