답안 #581670

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
581670 2022-06-23T02:39:53 Z DylanSmith Mecho (IOI09_mecho) Java 11
84 / 100
757 ms 30972 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 (bee[r2][c2] == -1 || (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 59 ms 8400 KB Output is correct
2 Correct 60 ms 8176 KB Output is correct
3 Correct 60 ms 8120 KB Output is correct
4 Correct 71 ms 8308 KB Output is correct
5 Correct 74 ms 8420 KB Output is correct
6 Correct 60 ms 8420 KB Output is correct
7 Correct 501 ms 29452 KB Output is correct
8 Correct 75 ms 8204 KB Output is correct
9 Correct 69 ms 8400 KB Output is correct
10 Correct 62 ms 8472 KB Output is correct
11 Correct 61 ms 8248 KB Output is correct
12 Incorrect 72 ms 8448 KB Output isn't correct
13 Incorrect 74 ms 8444 KB Output isn't correct
14 Correct 147 ms 11788 KB Output is correct
15 Correct 61 ms 8172 KB Output is correct
16 Correct 65 ms 8192 KB Output is correct
17 Correct 71 ms 8316 KB Output is correct
18 Correct 70 ms 8212 KB Output is correct
19 Correct 68 ms 8212 KB Output is correct
20 Correct 65 ms 8244 KB Output is correct
21 Correct 66 ms 8528 KB Output is correct
22 Correct 66 ms 8708 KB Output is correct
23 Correct 72 ms 8648 KB Output is correct
24 Correct 74 ms 8440 KB Output is correct
25 Correct 75 ms 8704 KB Output is correct
26 Correct 126 ms 11152 KB Output is correct
27 Correct 78 ms 8792 KB Output is correct
28 Correct 99 ms 11664 KB Output is correct
29 Correct 86 ms 8628 KB Output is correct
30 Correct 108 ms 11372 KB Output is correct
31 Correct 125 ms 11692 KB Output is correct
32 Correct 104 ms 11704 KB Output is correct
33 Correct 288 ms 14980 KB Output is correct
34 Correct 255 ms 15056 KB Output is correct
35 Correct 381 ms 18648 KB Output is correct
36 Correct 276 ms 16108 KB Output is correct
37 Correct 251 ms 16468 KB Output is correct
38 Correct 368 ms 19020 KB Output is correct
39 Correct 258 ms 17368 KB Output is correct
40 Correct 249 ms 17048 KB Output is correct
41 Correct 428 ms 19168 KB Output is correct
42 Correct 267 ms 19212 KB Output is correct
43 Correct 268 ms 18736 KB Output is correct
44 Correct 425 ms 19344 KB Output is correct
45 Correct 327 ms 19804 KB Output is correct
46 Correct 279 ms 20032 KB Output is correct
47 Correct 434 ms 20416 KB Output is correct
48 Correct 294 ms 20552 KB Output is correct
49 Correct 301 ms 21720 KB Output is correct
50 Correct 394 ms 22160 KB Output is correct
51 Correct 305 ms 23064 KB Output is correct
52 Correct 322 ms 23424 KB Output is correct
53 Correct 516 ms 22924 KB Output is correct
54 Correct 310 ms 24152 KB Output is correct
55 Correct 326 ms 24092 KB Output is correct
56 Correct 538 ms 24540 KB Output is correct
57 Correct 323 ms 25328 KB Output is correct
58 Correct 321 ms 27844 KB Output is correct
59 Correct 555 ms 28048 KB Output is correct
60 Correct 332 ms 28196 KB Output is correct
61 Correct 331 ms 27788 KB Output is correct
62 Correct 558 ms 28184 KB Output is correct
63 Correct 540 ms 29640 KB Output is correct
64 Correct 757 ms 28280 KB Output is correct
65 Correct 646 ms 28272 KB Output is correct
66 Correct 610 ms 28464 KB Output is correct
67 Correct 567 ms 28336 KB Output is correct
68 Correct 441 ms 29732 KB Output is correct
69 Correct 417 ms 29732 KB Output is correct
70 Correct 418 ms 29440 KB Output is correct
71 Correct 444 ms 29752 KB Output is correct
72 Incorrect 327 ms 28548 KB Output isn't correct
73 Incorrect 346 ms 27728 KB Output isn't correct
74 Correct 429 ms 29252 KB Output is correct
75 Correct 506 ms 28588 KB Output is correct
76 Correct 485 ms 29836 KB Output is correct
77 Correct 461 ms 28336 KB Output is correct
78 Correct 444 ms 28328 KB Output is correct
79 Correct 421 ms 28120 KB Output is correct
80 Correct 472 ms 28364 KB Output is correct
81 Correct 452 ms 28408 KB Output is correct
82 Correct 444 ms 28316 KB Output is correct
83 Correct 469 ms 28780 KB Output is correct
84 Correct 479 ms 28340 KB Output is correct
85 Correct 457 ms 28500 KB Output is correct
86 Correct 468 ms 28640 KB Output is correct
87 Correct 472 ms 28392 KB Output is correct
88 Correct 450 ms 29992 KB Output is correct
89 Correct 508 ms 28364 KB Output is correct
90 Correct 502 ms 30900 KB Output is correct
91 Correct 500 ms 30680 KB Output is correct
92 Correct 500 ms 30972 KB Output is correct