Submission #847654

# Submission time Handle Problem Language Result Execution time Memory
847654 2023-09-10T06:07:06 Z Oz121 Mecho (IOI09_mecho) Java 11
100 / 100
461 ms 40328 KB
import java.io.*;
import java.util.*;
public class mecho {
    public static char[][] arr; public static int[][] dis;
    public static int num; public static int s;
    public static int sx; public static int sy; public static int ex; public static int ey;
    public static ArrayDeque<Integer> Hqueue;
    public static int[] dx = {0,0,-1,1};
    public static int[] dy = {-1,1,0,0};
    public static void main(String[] args) throws IOException {
        BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer l1 = new StringTokenizer(scan.readLine());

        num = Integer.parseInt(l1.nextToken()); s = Integer.parseInt(l1.nextToken());
        arr = new char[num][num]; dis = new int[num][num]; Hqueue = new ArrayDeque<>();

        for (int i = 0;i<num;i++)
            Arrays.fill(dis[i],Integer.MAX_VALUE);

        sx = -1; sy = -1; ex = -1; ey = -1;
        for (int i = 0;i<num;i++) {
            StringTokenizer st = new StringTokenizer(scan.readLine()); String s = st.nextToken();
            for (int j = 0;j<num;j++) {
                arr[i][j] = s.charAt(j);

                if (arr[i][j]=='M') {
                    sx = i; sy = j;
                } else if (arr[i][j]=='D') {
                    ex = i; ey = j;
                } else if (arr[i][j]=='H') {
                    Hqueue.add(i*num+j);
                    dis[i][j] = 0;
                }
            }
        }

        bfsH();

        //printMat(dis);

        int low = 0; int high = num*num;
        while (low<high) {
            int mid = (low+high+1)/2;

            if (bfsB(mid)) {
                low = mid;
            } else {
                high = mid-1;
            }

        }

        if (bfsB(low)) {
            System.out.println(low);
        } else {
            System.out.println(-1);
        }
    }

    public static boolean bfsB (int eat) { //bfs for the bears
        ArrayDeque<Pair> bear = new ArrayDeque<>();
        bear.add(new Pair(sx*num+sy,eat*s));

        boolean[][] visited = new boolean[num][num];
        if (eat>=dis[sx][sy])
            return false;
        visited[sx][sy] = true;

        while (!bear.isEmpty()) {
            Pair curr = bear.poll(); int node = curr.node; int time = curr.time;
            int x = node/num; int y = node%num;
            //int minute = time/s; int sec = time%s;

            for (int i = 0;i<4;i++) {
                int nx = x+dx[i]; int ny = y+dy[i];
                if (nx<0||ny<0||nx>=num||ny>=num||visited[nx][ny]) continue; //||dis[nx][ny]!=Integer.MAX_VALUE
                if (arr[nx][ny]=='T'||arr[nx][ny]=='H') continue;

                if ((time+1)/s<dis[nx][ny]) {
                    visited[nx][ny] = true;

                    bear.add(new Pair(nx*num+ny, time+1));
                }
            }
        }

        return visited[ex][ey];
    }

    public static void bfsH () { //bfs for the bees
        while (!Hqueue.isEmpty()) {
            int curr = Hqueue.poll();
            int x = curr/num; int y = curr%num;

            for (int i = 0;i<4;i++) {
                int nx = x+dx[i]; int ny = y+dy[i];
                if (nx<0||ny<0||nx>=num||ny>=num||dis[nx][ny]!=Integer.MAX_VALUE) continue;
                if (arr[nx][ny]=='T'||arr[nx][ny]=='D') continue;

                dis[nx][ny] = dis[x][y]+1;
                Hqueue.add(nx*num+ny);
            }
        }
    }

    public static void printMat (int[][] arr) {
        for (int i = 0;i<arr.length;i++) {
            for (int j = 0;j<arr[0].length;j++) {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
    }

    public static class Pair {
        int node; int time;

        Pair (int node, int time) {
            this.node = node; this.time = time;
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 48 ms 22876 KB Output is correct
2 Correct 49 ms 22492 KB Output is correct
3 Correct 48 ms 24432 KB Output is correct
4 Correct 50 ms 26784 KB Output is correct
5 Correct 48 ms 22412 KB Output is correct
6 Correct 49 ms 22684 KB Output is correct
7 Correct 343 ms 36768 KB Output is correct
8 Correct 48 ms 22676 KB Output is correct
9 Correct 48 ms 22188 KB Output is correct
10 Correct 49 ms 22340 KB Output is correct
11 Correct 50 ms 22700 KB Output is correct
12 Correct 56 ms 22136 KB Output is correct
13 Correct 55 ms 21948 KB Output is correct
14 Correct 79 ms 24876 KB Output is correct
15 Correct 49 ms 22384 KB Output is correct
16 Correct 49 ms 22572 KB Output is correct
17 Correct 50 ms 24396 KB Output is correct
18 Correct 50 ms 22388 KB Output is correct
19 Correct 50 ms 22632 KB Output is correct
20 Correct 51 ms 22656 KB Output is correct
21 Correct 55 ms 24520 KB Output is correct
22 Correct 55 ms 26796 KB Output is correct
23 Correct 59 ms 22376 KB Output is correct
24 Correct 54 ms 24332 KB Output is correct
25 Correct 59 ms 22552 KB Output is correct
26 Correct 68 ms 28420 KB Output is correct
27 Correct 58 ms 22400 KB Output is correct
28 Correct 68 ms 26460 KB Output is correct
29 Correct 60 ms 22444 KB Output is correct
30 Correct 70 ms 27896 KB Output is correct
31 Correct 71 ms 24116 KB Output is correct
32 Correct 70 ms 23612 KB Output is correct
33 Correct 178 ms 27084 KB Output is correct
34 Correct 191 ms 29304 KB Output is correct
35 Correct 212 ms 27596 KB Output is correct
36 Correct 178 ms 31720 KB Output is correct
37 Correct 196 ms 29148 KB Output is correct
38 Correct 216 ms 29660 KB Output is correct
39 Correct 183 ms 29032 KB Output is correct
40 Correct 198 ms 29568 KB Output is correct
41 Correct 237 ms 31632 KB Output is correct
42 Correct 185 ms 33136 KB Output is correct
43 Correct 208 ms 31436 KB Output is correct
44 Correct 255 ms 31232 KB Output is correct
45 Correct 189 ms 29640 KB Output is correct
46 Correct 203 ms 29220 KB Output is correct
47 Correct 261 ms 33084 KB Output is correct
48 Correct 198 ms 29716 KB Output is correct
49 Correct 205 ms 29624 KB Output is correct
50 Correct 282 ms 31836 KB Output is correct
51 Correct 188 ms 29328 KB Output is correct
52 Correct 206 ms 31336 KB Output is correct
53 Correct 309 ms 32092 KB Output is correct
54 Correct 192 ms 35324 KB Output is correct
55 Correct 218 ms 31356 KB Output is correct
56 Correct 327 ms 38016 KB Output is correct
57 Correct 190 ms 31748 KB Output is correct
58 Correct 213 ms 33544 KB Output is correct
59 Correct 336 ms 35008 KB Output is correct
60 Correct 197 ms 33700 KB Output is correct
61 Correct 225 ms 33280 KB Output is correct
62 Correct 355 ms 38412 KB Output is correct
63 Correct 369 ms 36536 KB Output is correct
64 Correct 461 ms 36580 KB Output is correct
65 Correct 411 ms 36324 KB Output is correct
66 Correct 387 ms 35752 KB Output is correct
67 Correct 368 ms 35972 KB Output is correct
68 Correct 271 ms 33408 KB Output is correct
69 Correct 265 ms 33176 KB Output is correct
70 Correct 262 ms 34844 KB Output is correct
71 Correct 253 ms 32760 KB Output is correct
72 Correct 206 ms 31468 KB Output is correct
73 Correct 241 ms 37860 KB Output is correct
74 Correct 306 ms 35680 KB Output is correct
75 Correct 326 ms 40328 KB Output is correct
76 Correct 305 ms 36348 KB Output is correct
77 Correct 326 ms 36140 KB Output is correct
78 Correct 307 ms 36520 KB Output is correct
79 Correct 286 ms 34900 KB Output is correct
80 Correct 278 ms 35704 KB Output is correct
81 Correct 314 ms 36672 KB Output is correct
82 Correct 307 ms 35840 KB Output is correct
83 Correct 328 ms 36412 KB Output is correct
84 Correct 322 ms 36312 KB Output is correct
85 Correct 309 ms 37904 KB Output is correct
86 Correct 325 ms 36048 KB Output is correct
87 Correct 323 ms 38036 KB Output is correct
88 Correct 317 ms 36908 KB Output is correct
89 Correct 343 ms 36624 KB Output is correct
90 Correct 345 ms 38608 KB Output is correct
91 Correct 323 ms 36848 KB Output is correct
92 Correct 328 ms 36352 KB Output is correct