Submission #328216

# Submission time Handle Problem Language Result Execution time Memory
328216 2020-11-15T21:16:07 Z anishrajeev Mecho (IOI09_mecho) Java 11
50 / 100
1000 ms 65536 KB
import java.io.*;
import java.util.*;

public class mecho {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        //BufferedReader bf = new BufferedReader(new FileReader("tester.in"));
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
        StringTokenizer stk = new StringTokenizer(bf.readLine());
        int N = Integer.parseInt(stk.nextToken());
        int S = Integer.parseInt(stk.nextToken());
        String[][] grid = new String[N][N];
        boolean[][] visited = new boolean[N][N];
        LinkedList<pair> list = new LinkedList<>();
        int[][] bees = new int[N][N];
        int startx = 0, starty = 0, homex = 0, homey = 0;
        for(int i = 0; i < N; i++){
            String line = bf.readLine();
            String[] str = line.split("");
            Arrays.fill(bees[i], Integer.MAX_VALUE);
            for(int c = 0; c < N; c++){
                grid[i][c] = str[c];
                if(grid[i][c].equals("H")){
                    list.add(new pair(i, c, 0));
                    visited[i][c] = true;
                    bees[i][c] = 0;
                }
                if(grid[i][c].equals("M")){
                    startx = i;
                    starty = c;
                }
                if(grid[i][c].equals("D")){
                    homex = i;
                    homey = c;
                }
            }
        }
        int[] dx = new int[]{0, 1, 0, -1};
        int[] dy = new int[]{1, 0, -1, 0};
        while(!list.isEmpty()){
            pair p = list.pop();
            if(p.x == homex && p.y == homey)continue;
            if(grid[p.x][p.y].equals("T"))continue;
            bees[p.x][p.y] = Math.min(bees[p.x][p.y], p.t+S);
            for(int i = 0; i < 4; i++){
                int nx = p.x+dx[i];
                int ny = p.y+dy[i];
                if(N <= nx || nx < 0 || N <= ny || ny < 0 || visited[nx][ny])continue;
                list.add(new pair(nx, ny, bees[p.x][p.y]));
                visited[nx][ny] = true;

            }
        }
        bees[homex][homey] = Integer.MAX_VALUE;
        int start = 0, end = 10000000;
        while(start != end){
            int mid = (start+end+1)/2;
            if(verify(bees, grid, mid, startx, starty, homex, homey, N, S))start = mid;
            else end = mid-1;
        }
        if(!verify(bees, grid, 0, startx, starty, homex, homey, N, S))System.out.println(-1);
        else System.out.println(start);
    }
    public static boolean verify(int[][] bees, String[][] grid, int time, int startx, int starty, int homex, int homey, int N, int S){
        LinkedList<pair> q = new LinkedList<>();
        q.add(new pair(startx, starty, time*S));
        int[] dx = new int[]{0, 1, 0, -1};
        int[] dy = new int[]{1, 0, -1, 0};
        boolean[][] visited = new boolean[N][N];
        visited[startx][starty] = true;
        while(!q.isEmpty()){
            pair p = q.poll();
            if(p.t >= bees[p.x][p.y])continue;
            for(int i = 0; i < 4; i++){
                int nx = p.x+dx[i];
                int ny = p.y+dy[i];
                if(N <= nx || nx < 0 || N <= ny || ny < 0)continue;
                if(grid[nx][ny].equals("T")||grid[nx][ny].equals("H"))continue;
                if(bees[nx][ny] <= p.t+1 || visited[nx][ny])continue;
                q.add(new pair(nx, ny, p.t+1));
                visited[nx][ny] = true;
            }
        }
        return (visited[homex][homey]);
    }
    public static class pair implements Comparable<pair>{
        int x, y, t;
        public pair(int a, int b, int c){
            x = a;
            y = b;
            t = c;
        }

        @Override
        public int compareTo(pair o) {
            if(x==o.x&&y==o.y)return Integer.compare(t, o.t);
            if(x==o.x)return Integer.compare(y, o.y);
            return Integer.compare(x, o.x);
        }
        public boolean equals(pair o){
            if(x == o.x && y == o.y && t == o.t)return true;
            return false;
        }
    }
}
# Verdict Execution time Memory Grader output
1 Correct 72 ms 8684 KB Output is correct
2 Correct 77 ms 8556 KB Output is correct
3 Correct 72 ms 8684 KB Output is correct
4 Correct 74 ms 8768 KB Output is correct
5 Correct 72 ms 8684 KB Output is correct
6 Correct 74 ms 8684 KB Output is correct
7 Runtime error 772 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
8 Correct 77 ms 8936 KB Output is correct
9 Correct 78 ms 8684 KB Output is correct
10 Correct 81 ms 8668 KB Output is correct
11 Correct 78 ms 8684 KB Output is correct
12 Correct 113 ms 9580 KB Output is correct
13 Correct 108 ms 9452 KB Output is correct
14 Correct 178 ms 11820 KB Output is correct
15 Correct 82 ms 8940 KB Output is correct
16 Correct 78 ms 8812 KB Output is correct
17 Correct 89 ms 9196 KB Output is correct
18 Correct 90 ms 9068 KB Output is correct
19 Correct 92 ms 9068 KB Output is correct
20 Correct 96 ms 9308 KB Output is correct
21 Correct 101 ms 9324 KB Output is correct
22 Correct 102 ms 9452 KB Output is correct
23 Correct 109 ms 9552 KB Output is correct
24 Correct 113 ms 9452 KB Output is correct
25 Correct 124 ms 9600 KB Output is correct
26 Correct 148 ms 11800 KB Output is correct
27 Correct 128 ms 9836 KB Output is correct
28 Correct 187 ms 12488 KB Output is correct
29 Correct 135 ms 10092 KB Output is correct
30 Correct 197 ms 12488 KB Output is correct
31 Correct 169 ms 11860 KB Output is correct
32 Correct 162 ms 12192 KB Output is correct
33 Correct 573 ms 24652 KB Output is correct
34 Correct 680 ms 24528 KB Output is correct
35 Correct 733 ms 25924 KB Output is correct
36 Correct 499 ms 26988 KB Output is correct
37 Correct 583 ms 26996 KB Output is correct
38 Correct 786 ms 28080 KB Output is correct
39 Correct 630 ms 32940 KB Output is correct
40 Correct 756 ms 33004 KB Output is correct
41 Execution timed out 1096 ms 33660 KB Time limit exceeded
42 Correct 665 ms 36836 KB Output is correct
43 Correct 764 ms 37096 KB Output is correct
44 Execution timed out 1098 ms 37748 KB Time limit exceeded
45 Correct 680 ms 38904 KB Output is correct
46 Correct 782 ms 39308 KB Output is correct
47 Execution timed out 1093 ms 40248 KB Time limit exceeded
48 Correct 698 ms 43480 KB Output is correct
49 Correct 792 ms 43928 KB Output is correct
50 Execution timed out 1099 ms 44652 KB Time limit exceeded
51 Correct 767 ms 53164 KB Output is correct
52 Correct 866 ms 53912 KB Output is correct
53 Execution timed out 1072 ms 54912 KB Time limit exceeded
54 Correct 765 ms 55736 KB Output is correct
55 Correct 867 ms 56764 KB Output is correct
56 Execution timed out 1100 ms 57324 KB Time limit exceeded
57 Correct 797 ms 60960 KB Output is correct
58 Correct 910 ms 61248 KB Output is correct
59 Execution timed out 1099 ms 62060 KB Time limit exceeded
60 Runtime error 652 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
61 Runtime error 685 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
62 Runtime error 649 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
63 Runtime error 701 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
64 Runtime error 731 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
65 Runtime error 731 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
66 Runtime error 730 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
67 Runtime error 692 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
68 Runtime error 813 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
69 Runtime error 797 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
70 Runtime error 827 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
71 Runtime error 834 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
72 Runtime error 826 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
73 Runtime error 694 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
74 Runtime error 698 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
75 Runtime error 695 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
76 Runtime error 693 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
77 Runtime error 710 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
78 Runtime error 780 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
79 Runtime error 750 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
80 Runtime error 779 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
81 Runtime error 766 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
82 Runtime error 759 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
83 Runtime error 753 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
84 Runtime error 752 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
85 Runtime error 754 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
86 Runtime error 716 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
87 Runtime error 755 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
88 Runtime error 761 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
89 Runtime error 773 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
90 Runtime error 749 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
91 Runtime error 751 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)
92 Runtime error 764 ms 65536 KB Execution killed with signal 9 (could be triggered by violating memory limits)