Submission #815894

# Submission time Handle Problem Language Result Execution time Memory
815894 2023-08-09T02:06:52 Z powervic08 Mecho (IOI09_mecho) Java 11
95 / 100
564 ms 47916 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<>();
        if ((long) mid * S >= dists[starti][startj])
            return false;
        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 && !visited[newi][newj]) {
                        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;
        }
    }

}
# Verdict Execution time Memory Grader output
1 Correct 48 ms 8388 KB Output is correct
2 Correct 50 ms 8360 KB Output is correct
3 Correct 47 ms 8344 KB Output is correct
4 Correct 50 ms 8560 KB Output is correct
5 Correct 49 ms 8420 KB Output is correct
6 Correct 47 ms 8372 KB Output is correct
7 Correct 509 ms 33172 KB Output is correct
8 Correct 49 ms 8524 KB Output is correct
9 Correct 51 ms 8432 KB Output is correct
10 Correct 50 ms 8352 KB Output is correct
11 Correct 50 ms 8280 KB Output is correct
12 Incorrect 56 ms 8704 KB Output isn't correct
13 Correct 70 ms 8916 KB Output is correct
14 Correct 104 ms 11720 KB Output is correct
15 Correct 52 ms 8508 KB Output is correct
16 Correct 50 ms 8308 KB Output is correct
17 Correct 49 ms 8756 KB Output is correct
18 Correct 51 ms 8344 KB Output is correct
19 Correct 50 ms 8464 KB Output is correct
20 Correct 54 ms 8476 KB Output is correct
21 Correct 51 ms 8572 KB Output is correct
22 Correct 53 ms 8532 KB Output is correct
23 Correct 53 ms 8732 KB Output is correct
24 Correct 55 ms 8800 KB Output is correct
25 Correct 60 ms 8612 KB Output is correct
26 Correct 59 ms 8876 KB Output is correct
27 Correct 58 ms 8928 KB Output is correct
28 Correct 60 ms 8932 KB Output is correct
29 Correct 61 ms 8864 KB Output is correct
30 Correct 64 ms 8724 KB Output is correct
31 Correct 70 ms 10168 KB Output is correct
32 Correct 64 ms 9084 KB Output is correct
33 Correct 144 ms 14864 KB Output is correct
34 Correct 190 ms 15008 KB Output is correct
35 Correct 258 ms 16140 KB Output is correct
36 Correct 142 ms 14884 KB Output is correct
37 Correct 204 ms 15448 KB Output is correct
38 Correct 266 ms 16724 KB Output is correct
39 Correct 161 ms 15640 KB Output is correct
40 Correct 208 ms 16040 KB Output is correct
41 Correct 288 ms 17860 KB Output is correct
42 Correct 151 ms 16184 KB Output is correct
43 Correct 223 ms 16300 KB Output is correct
44 Correct 300 ms 18444 KB Output is correct
45 Correct 178 ms 16448 KB Output is correct
46 Correct 207 ms 16740 KB Output is correct
47 Correct 391 ms 19184 KB Output is correct
48 Correct 178 ms 16704 KB Output is correct
49 Correct 237 ms 17216 KB Output is correct
50 Correct 382 ms 19896 KB Output is correct
51 Correct 200 ms 17500 KB Output is correct
52 Correct 265 ms 17508 KB Output is correct
53 Correct 414 ms 20964 KB Output is correct
54 Correct 192 ms 17992 KB Output is correct
55 Correct 234 ms 18136 KB Output is correct
56 Correct 441 ms 23568 KB Output is correct
57 Correct 191 ms 18760 KB Output is correct
58 Correct 257 ms 20708 KB Output is correct
59 Correct 458 ms 25512 KB Output is correct
60 Correct 177 ms 20584 KB Output is correct
61 Correct 223 ms 20952 KB Output is correct
62 Correct 481 ms 25880 KB Output is correct
63 Correct 361 ms 25492 KB Output is correct
64 Correct 523 ms 26292 KB Output is correct
65 Correct 471 ms 26096 KB Output is correct
66 Correct 415 ms 26052 KB Output is correct
67 Correct 395 ms 25880 KB Output is correct
68 Correct 284 ms 23644 KB Output is correct
69 Correct 288 ms 23868 KB Output is correct
70 Correct 294 ms 22072 KB Output is correct
71 Correct 292 ms 22572 KB Output is correct
72 Correct 239 ms 21568 KB Output is correct
73 Correct 381 ms 47736 KB Output is correct
74 Correct 453 ms 47852 KB Output is correct
75 Correct 527 ms 47856 KB Output is correct
76 Correct 461 ms 47916 KB Output is correct
77 Correct 503 ms 47880 KB Output is correct
78 Correct 505 ms 44572 KB Output is correct
79 Correct 462 ms 44520 KB Output is correct
80 Correct 459 ms 44620 KB Output is correct
81 Correct 534 ms 44664 KB Output is correct
82 Correct 476 ms 44636 KB Output is correct
83 Correct 564 ms 40488 KB Output is correct
84 Correct 470 ms 40516 KB Output is correct
85 Correct 537 ms 40572 KB Output is correct
86 Correct 496 ms 40564 KB Output is correct
87 Correct 519 ms 40680 KB Output is correct
88 Correct 509 ms 36680 KB Output is correct
89 Correct 528 ms 36604 KB Output is correct
90 Correct 546 ms 36688 KB Output is correct
91 Correct 523 ms 36676 KB Output is correct
92 Correct 547 ms 36536 KB Output is correct