|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static int afull, bfull, cfull; |
| 7 | + static boolean[][][] visited; |
| 8 | + static boolean[] possible; |
| 9 | + |
| 10 | + static class State { |
| 11 | + int a, b, c; |
| 12 | + State(int a, int b, int c) { |
| 13 | + this.a = a; |
| 14 | + this.b = b; |
| 15 | + this.c = c; |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + public static void main(String[] args) throws IOException { |
| 20 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 21 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 22 | + afull = Integer.parseInt(st.nextToken()); |
| 23 | + bfull = Integer.parseInt(st.nextToken()); |
| 24 | + cfull = Integer.parseInt(st.nextToken()); |
| 25 | + |
| 26 | + visited = new boolean[afull + 1][bfull + 1][cfull + 1]; |
| 27 | + possible = new boolean[cfull + 1]; |
| 28 | + |
| 29 | + bfs(); |
| 30 | + |
| 31 | + StringBuilder sb = new StringBuilder(); |
| 32 | + for(int i=0;i<possible.length;i++){ |
| 33 | + if(possible[i]) |
| 34 | + sb.append(i+" "); |
| 35 | + } |
| 36 | + System.out.println(sb.toString().trim()); |
| 37 | + } |
| 38 | + |
| 39 | + static void bfs() { |
| 40 | + Queue<State> q = new LinkedList<>(); |
| 41 | + |
| 42 | + q.offer(new State(0, 0, cfull)); |
| 43 | + visited[0][0][cfull] = true; |
| 44 | + |
| 45 | + while (!q.isEmpty()) { |
| 46 | + State cur = q.poll(); |
| 47 | + int a = cur.a, b = cur.b, c = cur.c; |
| 48 | + |
| 49 | + if (a == 0) { |
| 50 | + possible[c]=true; |
| 51 | + } |
| 52 | + |
| 53 | + move(a, b, c, 0, 1, q); |
| 54 | + move(a, b, c, 0, 2, q); |
| 55 | + move(a, b, c, 1, 0, q); |
| 56 | + move(a, b, c, 1, 2, q); |
| 57 | + move(a, b, c, 2, 0, q); |
| 58 | + move(a, b, c, 2, 1, q); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + static void move(int a, int b, int c, int from, int to, Queue<State> q) { |
| 63 | + int[] cur = new int[]{a, b, c}; |
| 64 | + int[] cap = new int[]{afull, bfull, cfull}; |
| 65 | + |
| 66 | + if (cur[from] == 0) return; |
| 67 | + |
| 68 | + int movewater = Math.min(cur[from], cap[to] - cur[to]); |
| 69 | + cur[from] -= movewater; |
| 70 | + cur[to] += movewater; |
| 71 | + |
| 72 | + int na = cur[0], nb = cur[1], nc = cur[2]; |
| 73 | + if (!visited[na][nb][nc]) { |
| 74 | + visited[na][nb][nc] = true; |
| 75 | + q.offer(new State(na, nb, nc)); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
0 commit comments