[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 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: 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的,就略过了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值