답안 #815866

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
815866 2023-08-09T01:44:33 Z powervic08 Mecho (IOI09_mecho) Java 11
73 / 100
700 ms 47752 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] != 2) {
                        q.add(new Node(newi, newj, p.dist + 1));
                    }
                }
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (dists[i][j] != Integer.MAX_VALUE) {
                    dists[i][j] *= S;
                }
            }
        }
        int l = -1;
        int r = n * n;
        while (l < r) {
            int mid = (l + r + 1) / 2;
            if (works(mid, dists, grid, starti, startj, endi, endj)) {
                l = mid;
            } else {
                r = mid - 1;
            }
        }
        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 54 ms 8364 KB Output is correct
2 Incorrect 47 ms 8412 KB Output isn't correct
3 Correct 51 ms 8356 KB Output is correct
4 Incorrect 50 ms 8300 KB Output isn't correct
5 Correct 54 ms 8164 KB Output is correct
6 Incorrect 59 ms 8212 KB Output isn't correct
7 Incorrect 606 ms 32948 KB Output isn't correct
8 Correct 52 ms 8244 KB Output is correct
9 Correct 50 ms 8612 KB Output is correct
10 Correct 51 ms 8456 KB Output is correct
11 Correct 60 ms 8344 KB Output is correct
12 Incorrect 61 ms 8616 KB Output isn't correct
13 Incorrect 76 ms 8928 KB Output isn't correct
14 Correct 110 ms 12444 KB Output is correct
15 Correct 51 ms 8408 KB Output is correct
16 Correct 52 ms 8348 KB Output is correct
17 Correct 65 ms 8296 KB Output is correct
18 Correct 61 ms 8440 KB Output is correct
19 Correct 58 ms 8436 KB Output is correct
20 Correct 53 ms 8484 KB Output is correct
21 Correct 55 ms 8488 KB Output is correct
22 Correct 62 ms 8740 KB Output is correct
23 Correct 62 ms 8844 KB Output is correct
24 Correct 58 ms 8644 KB Output is correct
25 Correct 73 ms 8536 KB Output is correct
26 Correct 70 ms 8548 KB Output is correct
27 Correct 66 ms 8600 KB Output is correct
28 Correct 64 ms 8960 KB Output is correct
29 Correct 60 ms 8920 KB Output is correct
30 Correct 65 ms 8804 KB Output is correct
31 Correct 72 ms 10256 KB Output is correct
32 Correct 83 ms 11012 KB Output is correct
33 Correct 148 ms 14876 KB Output is correct
34 Correct 221 ms 14732 KB Output is correct
35 Correct 284 ms 16204 KB Output is correct
36 Correct 152 ms 15008 KB Output is correct
37 Correct 213 ms 15184 KB Output is correct
38 Correct 306 ms 16424 KB Output is correct
39 Correct 174 ms 15544 KB Output is correct
40 Correct 211 ms 15792 KB Output is correct
41 Correct 316 ms 17640 KB Output is correct
42 Correct 172 ms 15928 KB Output is correct
43 Correct 226 ms 16128 KB Output is correct
44 Correct 379 ms 18408 KB Output is correct
45 Correct 161 ms 16436 KB Output is correct
46 Correct 223 ms 16284 KB Output is correct
47 Correct 414 ms 19084 KB Output is correct
48 Correct 163 ms 16944 KB Output is correct
49 Correct 228 ms 17060 KB Output is correct
50 Correct 414 ms 19492 KB Output is correct
51 Correct 184 ms 17348 KB Output is correct
52 Correct 228 ms 17492 KB Output is correct
53 Correct 472 ms 21144 KB Output is correct
54 Correct 170 ms 17820 KB Output is correct
55 Correct 250 ms 18688 KB Output is correct
56 Correct 516 ms 23276 KB Output is correct
57 Correct 178 ms 18844 KB Output is correct
58 Correct 231 ms 20916 KB Output is correct
59 Correct 532 ms 25372 KB Output is correct
60 Correct 172 ms 20856 KB Output is correct
61 Correct 242 ms 21448 KB Output is correct
62 Correct 564 ms 25576 KB Output is correct
63 Correct 391 ms 25664 KB Output is correct
64 Correct 589 ms 26876 KB Output is correct
65 Correct 558 ms 26396 KB Output is correct
66 Correct 452 ms 25944 KB Output is correct
67 Correct 413 ms 25828 KB Output is correct
68 Correct 302 ms 25296 KB Output is correct
69 Correct 309 ms 24136 KB Output is correct
70 Correct 299 ms 23388 KB Output is correct
71 Correct 301 ms 24812 KB Output is correct
72 Incorrect 330 ms 22052 KB Output isn't correct
73 Incorrect 439 ms 47588 KB Output isn't correct
74 Correct 581 ms 47664 KB Output is correct
75 Correct 591 ms 47512 KB Output is correct
76 Correct 537 ms 47752 KB Output is correct
77 Correct 531 ms 47548 KB Output is correct
78 Correct 574 ms 44528 KB Output is correct
79 Correct 489 ms 44540 KB Output is correct
80 Correct 525 ms 44664 KB Output is correct
81 Correct 577 ms 44500 KB Output is correct
82 Correct 559 ms 44468 KB Output is correct
83 Correct 600 ms 40428 KB Output is correct
84 Correct 571 ms 40464 KB Output is correct
85 Correct 596 ms 40560 KB Output is correct
86 Correct 568 ms 40480 KB Output is correct
87 Correct 585 ms 40592 KB Output is correct
88 Correct 621 ms 36872 KB Output is correct
89 Correct 606 ms 36668 KB Output is correct
90 Correct 623 ms 36812 KB Output is correct
91 Correct 629 ms 36828 KB Output is correct
92 Correct 700 ms 36816 KB Output is correct