[Java] Mother‘s Milk
作者:互联网
/* Use the slash-star style comments or the system won't see your
identification information */
/*
ID: lincans1
LANG: JAVA
TASK: milk3
*/
import java.io.*;
import java.util.*;
public class milk3 {
private int A;
private int B;
private int C;
private void DFS(int a, int b, int c, boolean[] flag, Set<String> set) {
if (set.contains(b + " " + c)) {
return;
}
if (a == 0) {
flag[c] = true;
}
set.add(b + " " + c);
if (a != 0) {
// a -> b
if (b != B) {
DFS(a - Math.min(B - b, a), b + Math.min(B - b, a), c, flag, set);
}
// a -> c
if (c != C) {
DFS(a - Math.min(C - c, a), b, c + Math.min(C - c, a), flag, set);
}
}
if (b != 0) {
if (a != A) {
// b -> a
DFS(a + Math.min(A - a, b) , b - Math.min(A - a, b), c, flag, set);
}
if (c != C) {
// b -> c
DFS(a, b - Math.min(C - c, b), c + Math.min(C - c, b), flag, set);
}
}
if (c != 0) {
if (a != A) {
// c -> a
DFS(a + Math.min(A - a, c), b, c - Math.min(A - a, c), flag, set);
}
if (b != B) {
// c -> b
DFS(a, b + Math.min(B - b, c), c - Math.min(B - b, c), flag, set);
}
}
}
public milk3() throws IOException {
// Use BufferedReader rather than RandomAccessFile; it's much faster
BufferedReader f = new BufferedReader(new FileReader("milk3.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("milk3.out")));
// Use StringTokenizer vs. readLine/split -- lots faster
StringTokenizer st = new StringTokenizer(f.readLine());
this.A = Integer.parseInt(st.nextToken());
this.B = Integer.parseInt(st.nextToken());
this.C = Integer.parseInt(st.nextToken());
boolean[] flag = new boolean[C + 1];
DFS(0, 0, C, flag, new HashSet<String>());
for (int i = 0; i < C; i++) {
if (flag[i]) {
out.print(i + " ");
}
}
out.println(C);
out.close();
f.close();
}
public static void main (String [] args) throws IOException {
new milk3();
}
}
标签:set,Java,min,DFS,flag,Mother,new,Milk,Math 来源: https://blog.csdn.net/weixin_41714373/article/details/112385421