답안 #581668

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
581668 2022-06-23T02:36:40 Z DylanSmith Mecho (IOI09_mecho) Java 11
84 / 100
707 ms 30312 KB
import java.util.*;
import java.io.*;
public class mecho {
    public static void main(String[] args) throws IOException {
        Reader in = new Reader();
        PrintWriter out = new PrintWriter(System.out);
        
        int N = in.nextInt(), K = in.nextInt();
        char[][] mat = new char[N][N];
        for (int i = 0; i < N; i++) {
            mat[i] = in.next().toCharArray();
        }
        Queue<int[]> q = new LinkedList<>();
        int[][] bee = new int[N][N];
        int[] start = new int[2], end = new int[2];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (mat[i][j] == 'H') {
                    bee[i][j] = 0;
                    q.offer(new int[]{i, j});
                } else {
                    bee[i][j] = -1;
                }
                if (mat[i][j] == 'M') {
                    start = new int[]{i, j};
                }
                if (mat[i][j] == 'D') {
                    end = new int[]{i, j};
                }
            }
        }
        int[] x = new int[]{-1, 1, 0, 0}, y = new int[]{0, 0, -1, 1};
        while (!q.isEmpty()) {
            int[] current = q.poll();
            int r = current[0], c = current[1];
            for (int k = 0; k < 4; k++) {
                int r2 = r + x[k], c2 = c + y[k];
                if (r2 >= 0 && r2 < N && c2 >= 0 && c2 < N) {
                    if (mat[r2][c2] != 'D' && mat[r2][c2] != 'T' && bee[r2][c2] == -1) {
                        bee[r2][c2] = bee[r][c] + 1;
                        q.offer(new int[]{r2, c2});
                    }
                }
            }
        }
        out.println(search(mat, bee, start, end, K, 0, N * N));
        
        out.close();
    }
    public static int search(char[][] mat, int[][] bee, int[] start, int[] end, int K, int l, int r) {
        if (l > r) {
            return -1;
        }
        int mid = (l + r) / 2;
        if (check(mat, bee, start, end, K, mid)) {
            int m = search(mat, bee, start, end, K, mid + 1, r);
            if (m == -1) {
                return mid;
            }
            return m;
        }
        return search(mat, bee, start, end, K, l, mid - 1);
    }
    public static boolean check(char[][] mat, int[][] bee, int[] start, int[] end, int K, int eat) {
        int N = mat.length;
        int[][] dist = new int[N][N];
        for (int i = 0; i < N; i++) {
            Arrays.fill(dist[i], -1);
        }
        dist[start[0]][start[1]] = 0;
        Queue<int[]> q = new LinkedList<>();
        q.offer(start);
        int[] x = new int[]{-1, 1, 0, 0}, y = new int[]{0, 0, -1, 1};
        while (!q.isEmpty()) {
            int[] current = q.poll();
            int r = current[0], c = current[1];
            for (int k = 0; k < 4; k++) {
                int r2 = r + x[k], c2 = c + y[k];
                if (r2 >= 0 && r2 < N && c2 >= 0 && c2 < N) {
                    if (mat[r2][c2] != 'T' && mat[r2][c2] != 'H' && dist[r2][c2] == -1) {
                        if (mat[r2][c2] == 'D' || (dist[r][c] + 1) / K + eat < bee[r2][c2]) {
                            dist[r2][c2] = dist[r][c] + 1;
                            q.offer(new int[]{r2, c2});
                        }
                    }
                }
            }
        }
        return dist[end[0]][end[1]] >= 0;
    }
    static class Reader {
        BufferedInputStream in;
        public Reader() {
            in = new BufferedInputStream(System.in);
        }
        public String nextLine() throws IOException {
            int c;
            StringBuilder sb = new StringBuilder("");
            while ((c = in.read()) != '\n')
                sb.append((char)(c));
            return sb.toString();
        }
        public String next() throws IOException {
            int c;
            StringBuilder sb = new StringBuilder("");
            while ((c = in.read()) != ' ' && c != '\n')
                sb.append((char)(c));
            return sb.toString();
        }
        public int nextInt() throws IOException {
            return (int)nextLong();
        }
        public long nextLong() throws IOException {
            int c;
            long res = 0;
            boolean start = false, negative = false;
            while ((c = in.read()) != ' ' && c != '\n' || !start)
                if (c >= '0' && c <= '9' || c == '-') {
                    start = true;
                    if (c == '-')
                        negative = true;
                    else
                        res = res * 10 + c - '0';
                }
            return res * (negative ? -1 : 1);
        }
    }
    public static void sort(int[] arr) {
        List<Integer> list = new ArrayList<>();
        for (int i : arr) {
            list.add(i);
        }
        Collections.sort(list);
        for (int i = 0; i < arr.length; i++) {
            arr[i] = list.get(i);
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 8168 KB Output is correct
2 Correct 60 ms 8392 KB Output is correct
3 Correct 66 ms 8084 KB Output is correct
4 Correct 60 ms 8312 KB Output is correct
5 Correct 60 ms 8320 KB Output is correct
6 Correct 61 ms 8376 KB Output is correct
7 Correct 501 ms 29404 KB Output is correct
8 Correct 60 ms 8608 KB Output is correct
9 Correct 61 ms 8324 KB Output is correct
10 Correct 61 ms 8380 KB Output is correct
11 Correct 64 ms 8188 KB Output is correct
12 Incorrect 71 ms 8780 KB Output isn't correct
13 Incorrect 68 ms 8580 KB Output isn't correct
14 Correct 136 ms 11668 KB Output is correct
15 Correct 60 ms 8296 KB Output is correct
16 Correct 62 ms 8492 KB Output is correct
17 Correct 60 ms 8364 KB Output is correct
18 Correct 60 ms 8472 KB Output is correct
19 Correct 62 ms 8372 KB Output is correct
20 Correct 63 ms 8504 KB Output is correct
21 Correct 67 ms 8536 KB Output is correct
22 Correct 69 ms 8552 KB Output is correct
23 Correct 71 ms 8472 KB Output is correct
24 Correct 71 ms 8536 KB Output is correct
25 Correct 72 ms 8636 KB Output is correct
26 Correct 97 ms 11176 KB Output is correct
27 Correct 77 ms 8944 KB Output is correct
28 Correct 101 ms 11536 KB Output is correct
29 Correct 77 ms 8704 KB Output is correct
30 Correct 102 ms 11600 KB Output is correct
31 Correct 109 ms 11820 KB Output is correct
32 Correct 102 ms 11736 KB Output is correct
33 Correct 255 ms 14700 KB Output is correct
34 Correct 244 ms 15236 KB Output is correct
35 Correct 350 ms 18876 KB Output is correct
36 Correct 263 ms 15920 KB Output is correct
37 Correct 231 ms 16228 KB Output is correct
38 Correct 324 ms 19116 KB Output is correct
39 Correct 250 ms 16588 KB Output is correct
40 Correct 245 ms 17096 KB Output is correct
41 Correct 369 ms 19224 KB Output is correct
42 Correct 266 ms 19232 KB Output is correct
43 Correct 258 ms 19076 KB Output is correct
44 Correct 394 ms 19476 KB Output is correct
45 Correct 266 ms 19936 KB Output is correct
46 Correct 266 ms 20008 KB Output is correct
47 Correct 384 ms 20468 KB Output is correct
48 Correct 276 ms 20760 KB Output is correct
49 Correct 273 ms 22084 KB Output is correct
50 Correct 387 ms 22164 KB Output is correct
51 Correct 277 ms 23136 KB Output is correct
52 Correct 269 ms 23536 KB Output is correct
53 Correct 484 ms 22972 KB Output is correct
54 Correct 286 ms 24272 KB Output is correct
55 Correct 280 ms 24272 KB Output is correct
56 Correct 512 ms 24660 KB Output is correct
57 Correct 297 ms 27112 KB Output is correct
58 Correct 283 ms 27880 KB Output is correct
59 Correct 515 ms 28048 KB Output is correct
60 Correct 320 ms 28188 KB Output is correct
61 Correct 300 ms 27780 KB Output is correct
62 Correct 539 ms 28484 KB Output is correct
63 Correct 523 ms 29628 KB Output is correct
64 Correct 707 ms 28396 KB Output is correct
65 Correct 638 ms 28368 KB Output is correct
66 Correct 602 ms 28628 KB Output is correct
67 Correct 535 ms 28508 KB Output is correct
68 Correct 400 ms 29768 KB Output is correct
69 Correct 410 ms 29820 KB Output is correct
70 Correct 389 ms 29024 KB Output is correct
71 Correct 402 ms 29688 KB Output is correct
72 Incorrect 325 ms 28352 KB Output isn't correct
73 Incorrect 306 ms 28028 KB Output isn't correct
74 Correct 399 ms 29952 KB Output is correct
75 Correct 467 ms 28696 KB Output is correct
76 Correct 457 ms 29876 KB Output is correct
77 Correct 388 ms 28396 KB Output is correct
78 Correct 414 ms 28512 KB Output is correct
79 Correct 374 ms 28176 KB Output is correct
80 Correct 443 ms 28388 KB Output is correct
81 Correct 450 ms 28408 KB Output is correct
82 Correct 435 ms 28456 KB Output is correct
83 Correct 432 ms 28696 KB Output is correct
84 Correct 468 ms 28384 KB Output is correct
85 Correct 447 ms 28408 KB Output is correct
86 Correct 447 ms 28432 KB Output is correct
87 Correct 476 ms 28496 KB Output is correct
88 Correct 396 ms 30312 KB Output is correct
89 Correct 488 ms 28328 KB Output is correct
90 Correct 477 ms 28544 KB Output is correct
91 Correct 474 ms 28420 KB Output is correct
92 Correct 500 ms 28432 KB Output is correct