답안 #815878

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
815878 2023-08-09T01:56:39 Z powervic08 Mecho (IOI09_mecho) Java 11
84 / 100
641 ms 47892 KB
import java.util.*;
import java.io.*;

public class mecho {

    static int[] dx = {-1, 1, 0, 0};
    static int[] dy = {0, 0, -1, 1};
    static int S;

    public static void main(String[] args) throws IOException {
        BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);
        StringTokenizer st = new StringTokenizer(f.readLine());
        int n = Integer.parseInt(st.nextToken());
        S = Integer.parseInt(st.nextToken());
        int[][] grid = new int[n][n];
        int starti = 0, startj = 0, endi = 0, endj = 0;
        for (int i = 0; i < n; i++) {
            String s = f.readLine();
            for (int j = 0; j < n; j++) {
                if (s.charAt(j) == 'H') grid[i][j] = 1;
                else if (s.charAt(j) == 'T') grid[i][j] = 2;
                else if (s.charAt(j) == 'M') {
                    starti = i;
                    startj = j;
                } else if (s.charAt(j) == 'D') {
                    endi = i;
                    endj = j;
                }
            }
        }
        out.println(maximumMinutes(grid, starti, startj, endi, endj));
        out.close();
        f.close();
    }

    public static int maximumMinutes(int[][] grid, int starti, int startj, int endi, int endj) {
        int n = grid.length;
        int m = grid[0].length;
        Queue<Node> q = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == 1) {
                    q.add(new Node(i, j, 0));
                }
            }
        }
        int[][] dists = new int[n][m];
        for (int i = 0; i < n; i++) {
            Arrays.fill(dists[i], Integer.MAX_VALUE);
        }
        while (!q.isEmpty()) {
            Node p = q.poll();
            if (p.dist >= dists[p.i][p.j]) {
                continue;
            }
            dists[p.i][p.j] = (int) p.dist;
            for (int i = 0; i < 4; i++) {
                int newi = dx[i] + p.i;
                int newj = dy[i] + p.j;
                if (inRange(newi, newj, grid)) {
                    if (grid[newi][newj] == 0) {
                        q.add(new Node(newi, newj, p.dist + S));
                    }
                }
            }
        }
        dists[endi][endj] = Integer.MAX_VALUE;
        int l = -1;
        int r = 2 * n * n;
        while (r - l > 1) {
            int mid = (l + r) / 2;
            if (works(mid, dists, grid, starti, startj, endi, endj)) {
                l = mid;
            } else {
                r = mid;
            }
        }
        return l;
    }

    public static boolean works(int mid, int[][] dists, int[][] grid, int starti, int startj, int endi, int endj) {
        Queue<Node> q = new LinkedList<>();
        q.add(new Node(starti, startj, (long) mid * S));
        boolean[][] visited = new boolean[grid.length][grid[0].length];
        while (!q.isEmpty()) {
            Node p = q.poll();
            if (p.i == endi && p.j == endj) {
                return true;
            }
            if (visited[p.i][p.j]) {
                continue;
            }
            visited[p.i][p.j] = true;
            for (int i = 0; i < 4; i++) {
                int newi = dx[i] + p.i;
                int newj = dy[i] + p.j;
                if (inRange(newi, newj, grid)) {
                    if (grid[newi][newj] == 0 && dists[newi][newj] > p.dist + 1 || newi == endi && newj == endj && dists[newi][newj] == p.dist + 1) {
                        q.add(new Node(newi, newj, p.dist + 1));
                    }
                }
            }
        }
        return false;
    }

    public static boolean inRange(int i, int j, int[][] grid) {
        return i >= 0 && j >= 0 && i < grid.length && j < grid[0].length;
    }

    static class Node {
        int i, j;
        long dist;

        public Node(int a, int b, long c) {
            i = a;
            j = b;
            dist = c;
        }
    }

}
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 8520 KB Output is correct
2 Correct 49 ms 8312 KB Output is correct
3 Correct 47 ms 8068 KB Output is correct
4 Correct 56 ms 8232 KB Output is correct
5 Correct 49 ms 8380 KB Output is correct
6 Correct 51 ms 8464 KB Output is correct
7 Correct 592 ms 33340 KB Output is correct
8 Correct 48 ms 8236 KB Output is correct
9 Correct 51 ms 8576 KB Output is correct
10 Correct 53 ms 8520 KB Output is correct
11 Correct 49 ms 8388 KB Output is correct
12 Incorrect 58 ms 8652 KB Output isn't correct
13 Incorrect 62 ms 9212 KB Output isn't correct
14 Correct 102 ms 12380 KB Output is correct
15 Correct 50 ms 8352 KB Output is correct
16 Correct 50 ms 8508 KB Output is correct
17 Correct 51 ms 8460 KB Output is correct
18 Correct 50 ms 8252 KB Output is correct
19 Correct 50 ms 8444 KB Output is correct
20 Correct 51 ms 8532 KB Output is correct
21 Correct 52 ms 8588 KB Output is correct
22 Correct 54 ms 8760 KB Output is correct
23 Correct 54 ms 8524 KB Output is correct
24 Correct 58 ms 8612 KB Output is correct
25 Correct 58 ms 8656 KB Output is correct
26 Correct 63 ms 8680 KB Output is correct
27 Correct 62 ms 8984 KB Output is correct
28 Correct 62 ms 9120 KB Output is correct
29 Correct 61 ms 8728 KB Output is correct
30 Correct 65 ms 9172 KB Output is correct
31 Correct 70 ms 10312 KB Output is correct
32 Correct 83 ms 11136 KB Output is correct
33 Correct 146 ms 14888 KB Output is correct
34 Correct 200 ms 14676 KB Output is correct
35 Correct 267 ms 16040 KB Output is correct
36 Correct 150 ms 15036 KB Output is correct
37 Correct 209 ms 15108 KB Output is correct
38 Correct 271 ms 16764 KB Output is correct
39 Correct 149 ms 15612 KB Output is correct
40 Correct 205 ms 15724 KB Output is correct
41 Correct 304 ms 17644 KB Output is correct
42 Correct 150 ms 15900 KB Output is correct
43 Correct 210 ms 16192 KB Output is correct
44 Correct 340 ms 18288 KB Output is correct
45 Correct 152 ms 16292 KB Output is correct
46 Correct 210 ms 16572 KB Output is correct
47 Correct 373 ms 18956 KB Output is correct
48 Correct 169 ms 17032 KB Output is correct
49 Correct 222 ms 17180 KB Output is correct
50 Correct 398 ms 19500 KB Output is correct
51 Correct 171 ms 17412 KB Output is correct
52 Correct 216 ms 17508 KB Output is correct
53 Correct 478 ms 21144 KB Output is correct
54 Correct 161 ms 18136 KB Output is correct
55 Correct 223 ms 18292 KB Output is correct
56 Correct 508 ms 22868 KB Output is correct
57 Correct 160 ms 18840 KB Output is correct
58 Correct 224 ms 20724 KB Output is correct
59 Correct 509 ms 25088 KB Output is correct
60 Correct 192 ms 20480 KB Output is correct
61 Correct 226 ms 21952 KB Output is correct
62 Correct 589 ms 25668 KB Output is correct
63 Correct 381 ms 25620 KB Output is correct
64 Correct 586 ms 26792 KB Output is correct
65 Correct 490 ms 26088 KB Output is correct
66 Correct 434 ms 25972 KB Output is correct
67 Correct 410 ms 26288 KB Output is correct
68 Correct 290 ms 24932 KB Output is correct
69 Correct 298 ms 24532 KB Output is correct
70 Correct 268 ms 23456 KB Output is correct
71 Correct 278 ms 24324 KB Output is correct
72 Incorrect 266 ms 21900 KB Output isn't correct
73 Incorrect 421 ms 47620 KB Output isn't correct
74 Correct 502 ms 47892 KB Output is correct
75 Correct 542 ms 47852 KB Output is correct
76 Correct 554 ms 47820 KB Output is correct
77 Correct 527 ms 47844 KB Output is correct
78 Correct 583 ms 44680 KB Output is correct
79 Correct 476 ms 44492 KB Output is correct
80 Correct 515 ms 44732 KB Output is correct
81 Correct 541 ms 44560 KB Output is correct
82 Correct 521 ms 44572 KB Output is correct
83 Correct 602 ms 40544 KB Output is correct
84 Correct 549 ms 40636 KB Output is correct
85 Correct 587 ms 40520 KB Output is correct
86 Correct 551 ms 40444 KB Output is correct
87 Correct 592 ms 40740 KB Output is correct
88 Correct 617 ms 36712 KB Output is correct
89 Correct 593 ms 36752 KB Output is correct
90 Correct 614 ms 36532 KB Output is correct
91 Correct 584 ms 36752 KB Output is correct
92 Correct 641 ms 36608 KB Output is correct