|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class boj2251 { |
| 6 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + static StringTokenizer st; |
| 8 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 9 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 10 | + |
| 11 | + static int A, B, C; |
| 12 | + static Set<Integer> answer; |
| 13 | + static boolean[][][] visited; |
| 14 | + static int[] capacity; |
| 15 | + public static void main(String[] args) throws Exception { |
| 16 | + nextLine(); |
| 17 | + A = nextInt(); |
| 18 | + B = nextInt(); |
| 19 | + C = nextInt(); |
| 20 | + answer = new TreeSet<>(); |
| 21 | + capacity = new int[]{A, B, C}; |
| 22 | + visited = new boolean[A + 1][B + 1][C + 1]; |
| 23 | + |
| 24 | + dfs(0, 0, C); |
| 25 | + |
| 26 | + for (int a : answer) System.out.print(a + " "); |
| 27 | + } |
| 28 | + static void dfs(int a, int b, int c) { |
| 29 | + if (visited[a][b][c]) return; |
| 30 | + |
| 31 | + visited[a][b][c] = true; |
| 32 | + |
| 33 | + if (a == 0) answer.add(c); |
| 34 | + |
| 35 | + for (int i = 0; i < 3; i++) { |
| 36 | + for (int j = 0; j < 3; j++) { |
| 37 | + if (i != j) { |
| 38 | + int[] next = pour(new int[]{a, b, c}, i, j); |
| 39 | + dfs(next[0], next[1], next[2]); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + static int[] pour(int[] state, int from, int to) { |
| 45 | + int[] next = state.clone(); |
| 46 | + int amount = Math.min(state[from], capacity[to] - state[to]); |
| 47 | + next[from] -= amount; |
| 48 | + next[to] += amount; |
| 49 | + return next; |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
0 commit comments