|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static class State { |
| 7 | + int a, b, c, cnt; |
| 8 | + State(int a, int b, int c, int cnt) { |
| 9 | + this.a = a; |
| 10 | + this.b = b; |
| 11 | + this.c = c; |
| 12 | + this.cnt = cnt; |
| 13 | + } |
| 14 | + } |
| 15 | + |
| 16 | + static int[] dmg = {9, 3, 1}; |
| 17 | + static boolean[][][] visited = new boolean[61][61][61]; |
| 18 | + |
| 19 | + public static void main(String[] args) throws IOException { |
| 20 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 21 | + int n = Integer.parseInt(br.readLine()); |
| 22 | + |
| 23 | + int[] scv = new int[3]; |
| 24 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 25 | + for (int i = 0; i < n; i++) { |
| 26 | + scv[i] = Integer.parseInt(st.nextToken()); |
| 27 | + } |
| 28 | + |
| 29 | + System.out.println(bfs(scv[0], scv[1], scv[2])); |
| 30 | + } |
| 31 | + |
| 32 | + static int bfs(int a, int b, int c) { |
| 33 | + Queue<State> q = new LinkedList<>(); |
| 34 | + q.add(new State(a, b, c, 0)); |
| 35 | + visited[a][b][c] = true; |
| 36 | + |
| 37 | + while (!q.isEmpty()) { |
| 38 | + State cur = q.poll(); |
| 39 | + int x = cur.a; |
| 40 | + int y = cur.b; |
| 41 | + int z = cur.c; |
| 42 | + |
| 43 | + if (x == 0 && y == 0 && z == 0) { |
| 44 | + return cur.cnt; |
| 45 | + } |
| 46 | + |
| 47 | + int[][] orders = { |
| 48 | + {0,1,2}, {0,2,1}, |
| 49 | + {1,0,2}, {1,2,0}, |
| 50 | + {2,0,1}, {2,1,0} |
| 51 | + }; |
| 52 | + |
| 53 | + for (int[] ord : orders) { |
| 54 | + int nx = Math.max(0, x - dmg[ord[0]]); |
| 55 | + int ny = Math.max(0, y - dmg[ord[1]]); |
| 56 | + int nz = Math.max(0, z - dmg[ord[2]]); |
| 57 | + |
| 58 | + if (!visited[nx][ny][nz]) { |
| 59 | + visited[nx][ny][nz] = true; |
| 60 | + q.add(new State(nx, ny, nz, cur.cnt + 1)); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + return -1; |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
0 commit comments