|
| 1 | +package TIL; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +public class J2251 { |
| 7 | + static int A, B, C; |
| 8 | + static int[] maxCapacity; |
| 9 | + static boolean[][] visited; |
| 10 | + static boolean[] availableCapicity; |
| 11 | + |
| 12 | + public static void main(String[] args) throws IOException { |
| 13 | + BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in)); |
| 14 | + |
| 15 | + String[] input = buffer.readLine().split(" "); |
| 16 | + |
| 17 | + A = Integer.parseInt(input[0]); |
| 18 | + B = Integer.parseInt(input[1]); |
| 19 | + C = Integer.parseInt(input[2]); |
| 20 | + |
| 21 | + maxCapacity = new int[] {A, B, C}; |
| 22 | + visited = new boolean[A+1][B+1]; |
| 23 | + availableCapicity = new boolean[C + 1]; |
| 24 | + |
| 25 | + BFS(); |
| 26 | + for(int i = 0; i < availableCapicity.length; i++) { |
| 27 | + if(availableCapicity[i]) System.out.print(i + " "); |
| 28 | + } |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + public static void BFS() { |
| 33 | + Queue<int[]> queue = new LinkedList<int[]>(); |
| 34 | + |
| 35 | + queue.add(new int[] {0, 0, C}); |
| 36 | + while(!queue.isEmpty()) { |
| 37 | + int[] status = queue.poll(); |
| 38 | + visited[status[0]][status[1]] = true; |
| 39 | + |
| 40 | + if(status[0] == 0) availableCapicity[status[2]] = true; |
| 41 | + |
| 42 | + for(int i = 0; i < 3; i++) { |
| 43 | + for(int j = 0; j < 3; j++) { |
| 44 | + if(i == j) continue; |
| 45 | + int[] newStatus = watering(status, i, j); |
| 46 | + if(newStatus != null && !visited[newStatus[0]][newStatus[1]]) { |
| 47 | + queue.add(newStatus); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + public static int[] watering(int[] status, int sender, int receiver) { |
| 56 | + int residue = maxCapacity[receiver] - status[receiver]; |
| 57 | + |
| 58 | + if(residue == 0 || status[sender] == 0 ) return null; |
| 59 | + |
| 60 | + int[] newStatus = status.clone(); |
| 61 | + if(residue < newStatus[sender]) { |
| 62 | + newStatus[sender] -= residue; |
| 63 | + newStatus[receiver] += residue; |
| 64 | + } else { |
| 65 | + newStatus[receiver] += newStatus[sender]; |
| 66 | + newStatus[sender] = 0; |
| 67 | + } |
| 68 | + |
| 69 | + return newStatus; |
| 70 | + |
| 71 | + } |
| 72 | +} |
0 commit comments