[USACO]Mother's Milk【JAVA】

来源:互联网 发布:广电网络宽带怎么缴费 编辑:程序博客网 时间:2024/05/20 07:54
Mother'sMilk

[笔者CSDN传送阵]:http://blog.csdn.net/u013247524/article/details/24390101

Farmer John has three milking buckets of capacity A, B, and Cliters. Each of the numbers A, B, and C is an integer from 1through 20, inclusive. Initially, buckets A and B are empty whilebucket C is full of milk. Sometimes, FJ pours milk from one bucketto another until the second bucket is filled or the first bucket isempty. Once begun, a pour must be completed, of course. Beingthrifty, no milk may be tossed out.

Write a program to help FJ determine what amounts of milk he canleave in bucket C when he begins with three buckets as above, poursmilk among the buckets for a while, and then notes that bucket A isempty.

PROGRAMNAME: milk3

INPUTFORMAT

A single line with the three integers A, B, and C.

SAMPLEINPUT (file milk3.in)

8 9 10

OUTPUTFORMAT

A single line with a sorted list of all the possible amounts ofmilk that can be in bucket C when bucket A is empty.

SAMPLEOUTPUT (file milk3.out)

1 2 8 9 10

SAMPLEINPUT (file milk3.in)

2 5 10

SAMPLEOUTPUT (file milk3.out)

5 6 7 8 9 10
[java] view plaincopy
  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 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.         Integer.parseInt(st.nextToken());  
  24.         Integer.parseInt(st.nextToken());  
  25.         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(0b, a+c);        //A=>C  
  55.             else DFS(a-(C-c), b, C);  
  56.               
  57.             if(b<=A-a)DFS(a+b,0c);     //B=>A  
  58.             else DFS(A, b-(A-a), c);  
  59.             if(b<=C-c)DFS(a,0c+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: milk3LANG: JAVACompiling...Compile: OKExecuting...   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 yoursubmission #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的,就略过了
0 0
原创粉丝点击