|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +
|
| 5 | +public class Main { |
| 6 | + private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + private static final int[] dx = {9, 9, 3, 3, 1, 1}; |
| 9 | + private static final int[] dy = {3, 1, 9, 1, 3, 9}; |
| 10 | + private static final int[] dz = {1, 3, 1, 9, 9, 3}; |
| 11 | + private static Set<String> visited; |
| 12 | + private static int[] scv; |
| 13 | + private static int N; |
| 14 | + public static void main(String[] args) throws IOException { |
| 15 | + init(); |
| 16 | + int answer = BFS(); |
| 17 | +
|
| 18 | + bw.write(answer + "\n"); |
| 19 | + bw.flush(); |
| 20 | + bw.close(); |
| 21 | + br.close(); |
| 22 | + } |
| 23 | +
|
| 24 | + private static void init() throws IOException { |
| 25 | + N = Integer.parseInt(br.readLine()); |
| 26 | + scv = new int[3]; |
| 27 | + visited = new HashSet<>(); |
| 28 | +
|
| 29 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 30 | + for (int i = 0; i < 3; i++) { |
| 31 | + if (N == i) break; |
| 32 | + scv[i] = Integer.parseInt(st.nextToken()); |
| 33 | + } |
| 34 | + } |
| 35 | +
|
| 36 | + private static int BFS() { |
| 37 | + Queue<int[]> q = new ArrayDeque<>(); |
| 38 | + int result = 0; |
| 39 | + visited.add(scv[0]+","+scv[1]+","+scv[2]); |
| 40 | + q.add(new int[]{scv[0], scv[1], scv[2], 0}); |
| 41 | +
|
| 42 | + while (!q.isEmpty()) { |
| 43 | + int[] current = q.poll(); |
| 44 | +
|
| 45 | + if (current[0] == 0 && current[1] == 0 && current[2] == 0) { |
| 46 | + result = current[3]; |
| 47 | + break; |
| 48 | + } |
| 49 | +
|
| 50 | + for (int i = 0; i < 6; i++) { |
| 51 | + int x = current[0] - dx[i] < 0 ? 0 : current[0] - dx[i]; |
| 52 | + int y = current[1] - dy[i] < 0 ? 0 : current[1] - dy[i]; |
| 53 | + int z = current[2] - dz[i] < 0 ? 0 : current[2] - dz[i]; |
| 54 | +
|
| 55 | + if (visited.contains(x+","+y+","+z)) continue; |
| 56 | + visited.add(x+","+y+","+z); |
| 57 | + q.add(new int[]{x, y, z, current[3]+1}); |
| 58 | + } |
| 59 | + } |
| 60 | +
|
| 61 | + return result; |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
0 commit comments