|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.Set; |
| 4 | +import java.util.StringTokenizer; |
| 5 | +import java.util.TreeSet; |
| 6 | + |
| 7 | +public class BJ_2251_물통 { |
| 8 | + |
| 9 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + private static final StringBuilder sb = new StringBuilder(); |
| 12 | + private static StringTokenizer st; |
| 13 | + |
| 14 | + private static int[] capacity; |
| 15 | + private static Set<Integer> candidate; |
| 16 | + private static boolean[][] visited; |
| 17 | + |
| 18 | + public static void main(String[] args) throws IOException { |
| 19 | + init(); |
| 20 | + sol(); |
| 21 | + } |
| 22 | + |
| 23 | + private static void init() throws IOException { |
| 24 | + st = new StringTokenizer(br.readLine()); |
| 25 | + |
| 26 | + capacity = new int[3]; |
| 27 | + for (int i = 0; i < 3; i++) { |
| 28 | + capacity[i] = Integer.parseInt(st.nextToken()); |
| 29 | + } |
| 30 | + visited = new boolean[capacity[0] + 1][capacity[1] + 1]; |
| 31 | + candidate = new TreeSet<>(); |
| 32 | + } |
| 33 | + |
| 34 | + private static void sol() throws IOException { |
| 35 | + dfs(0, 0, capacity[2]); |
| 36 | + |
| 37 | + for (int c : candidate) { |
| 38 | + bw.write(c + " "); |
| 39 | + } |
| 40 | + bw.flush(); |
| 41 | + bw.close(); |
| 42 | + br.close(); |
| 43 | + } |
| 44 | + |
| 45 | + private static void dfs(int a, int b, int c) throws IOException { |
| 46 | + if (visited[a][b]) return; |
| 47 | + |
| 48 | + // a가 0일 때 c에 담긴 물의 양 |
| 49 | + if (a == 0) candidate.add(c); |
| 50 | + |
| 51 | + visited[a][b] = true; |
| 52 | + |
| 53 | + // a -> b |
| 54 | + if (a + b <= capacity[1]) { |
| 55 | + dfs(0, a + b, c); |
| 56 | + } else { |
| 57 | + dfs(a + b - capacity[1], capacity[1], c); |
| 58 | + } |
| 59 | + |
| 60 | + // b -> a |
| 61 | + if (a + b <= capacity[0]) { |
| 62 | + dfs(a + b, 0, c); |
| 63 | + } else { |
| 64 | + dfs(capacity[0], a + b - capacity[0], c); |
| 65 | + } |
| 66 | + |
| 67 | + // b -> c |
| 68 | + if (b + c <= capacity[2]) { |
| 69 | + dfs(a, 0, b + c); |
| 70 | + } else { |
| 71 | + dfs(a, b + c - capacity[2], capacity[2]); |
| 72 | + } |
| 73 | + |
| 74 | + // c -> a |
| 75 | + if (a + c <= capacity[0]) { |
| 76 | + dfs(a + c, b, 0); |
| 77 | + } else { |
| 78 | + dfs(capacity[0], b, a + c - capacity[0]); |
| 79 | + } |
| 80 | + |
| 81 | + // c -> b |
| 82 | + if (b + c <= capacity[1]) { |
| 83 | + dfs(a, b + c, 0); |
| 84 | + } else { |
| 85 | + dfs(a, capacity[1], b + c - capacity[1]); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | +} |
| 90 | +``` |
0 commit comments