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];
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));
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};
boolean[][] visited = new boolean[N][N];
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);
if(visited[p.x][p.y])continue;
visited[p.x][p.y] = true;
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;
list.add(new pair(nx, ny, bees[p.x][p.y]));
}
}
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;
}
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];
while(!q.isEmpty()){
pair p = q.poll();
if(p.t >= bees[p.x][p.y])continue;
if(visited[p.x][p.y])continue;
visited[p.x][p.y] = true;
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(bees[nx][ny] <= p.t)continue;
if(grid[nx][ny].equals("T")||grid[nx][ny].equals("H"))continue;
if(bees[nx][ny] <= p.t+1)continue;
q.add(new pair(nx, ny, p.t+1));
}
}
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;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
8660 KB |
Output is correct |
2 |
Correct |
74 ms |
8684 KB |
Output is correct |
3 |
Correct |
74 ms |
8628 KB |
Output is correct |
4 |
Correct |
73 ms |
8684 KB |
Output is correct |
5 |
Correct |
73 ms |
8704 KB |
Output is correct |
6 |
Correct |
77 ms |
8812 KB |
Output is correct |
7 |
Runtime error |
758 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Incorrect |
78 ms |
8684 KB |
Output isn't correct |
9 |
Correct |
83 ms |
8792 KB |
Output is correct |
10 |
Correct |
79 ms |
8684 KB |
Output is correct |
11 |
Correct |
78 ms |
8684 KB |
Output is correct |
12 |
Correct |
125 ms |
9936 KB |
Output is correct |
13 |
Correct |
117 ms |
9708 KB |
Output is correct |
14 |
Incorrect |
228 ms |
13976 KB |
Output isn't correct |
15 |
Correct |
81 ms |
8784 KB |
Output is correct |
16 |
Correct |
85 ms |
8760 KB |
Output is correct |
17 |
Correct |
88 ms |
9232 KB |
Output is correct |
18 |
Correct |
88 ms |
9196 KB |
Output is correct |
19 |
Correct |
94 ms |
9196 KB |
Output is correct |
20 |
Correct |
95 ms |
9324 KB |
Output is correct |
21 |
Correct |
105 ms |
9436 KB |
Output is correct |
22 |
Correct |
102 ms |
9452 KB |
Output is correct |
23 |
Correct |
124 ms |
9580 KB |
Output is correct |
24 |
Correct |
148 ms |
11840 KB |
Output is correct |
25 |
Correct |
132 ms |
10092 KB |
Output is correct |
26 |
Correct |
159 ms |
11944 KB |
Output is correct |
27 |
Correct |
140 ms |
10176 KB |
Output is correct |
28 |
Correct |
166 ms |
12160 KB |
Output is correct |
29 |
Correct |
179 ms |
12584 KB |
Output is correct |
30 |
Correct |
172 ms |
12356 KB |
Output is correct |
31 |
Correct |
190 ms |
13216 KB |
Output is correct |
32 |
Correct |
181 ms |
13152 KB |
Output is correct |
33 |
Correct |
632 ms |
24780 KB |
Output is correct |
34 |
Correct |
686 ms |
25072 KB |
Output is correct |
35 |
Correct |
832 ms |
25456 KB |
Output is correct |
36 |
Correct |
570 ms |
27136 KB |
Output is correct |
37 |
Correct |
623 ms |
27356 KB |
Output is correct |
38 |
Correct |
882 ms |
28056 KB |
Output is correct |
39 |
Correct |
665 ms |
32864 KB |
Output is correct |
40 |
Correct |
746 ms |
32964 KB |
Output is correct |
41 |
Execution timed out |
1092 ms |
33792 KB |
Time limit exceeded |
42 |
Correct |
688 ms |
36664 KB |
Output is correct |
43 |
Correct |
784 ms |
36880 KB |
Output is correct |
44 |
Execution timed out |
1088 ms |
37376 KB |
Time limit exceeded |
45 |
Correct |
780 ms |
39448 KB |
Output is correct |
46 |
Correct |
798 ms |
39736 KB |
Output is correct |
47 |
Execution timed out |
1064 ms |
40404 KB |
Time limit exceeded |
48 |
Correct |
749 ms |
47460 KB |
Output is correct |
49 |
Correct |
813 ms |
43728 KB |
Output is correct |
50 |
Execution timed out |
1097 ms |
49016 KB |
Time limit exceeded |
51 |
Correct |
822 ms |
53432 KB |
Output is correct |
52 |
Correct |
898 ms |
53360 KB |
Output is correct |
53 |
Execution timed out |
1097 ms |
55052 KB |
Time limit exceeded |
54 |
Correct |
858 ms |
59820 KB |
Output is correct |
55 |
Correct |
918 ms |
59980 KB |
Output is correct |
56 |
Execution timed out |
1090 ms |
60772 KB |
Time limit exceeded |
57 |
Correct |
867 ms |
61736 KB |
Output is correct |
58 |
Correct |
969 ms |
62036 KB |
Output is correct |
59 |
Execution timed out |
1069 ms |
62936 KB |
Time limit exceeded |
60 |
Runtime error |
661 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
61 |
Runtime error |
693 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
62 |
Runtime error |
681 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
63 |
Runtime error |
697 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
64 |
Runtime error |
700 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
65 |
Runtime error |
690 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
66 |
Runtime error |
734 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
67 |
Runtime error |
706 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 |
793 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
70 |
Runtime error |
842 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
71 |
Runtime error |
795 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
72 |
Runtime error |
800 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
73 |
Runtime error |
655 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
74 |
Runtime error |
647 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
75 |
Runtime error |
674 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
76 |
Runtime error |
677 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
77 |
Runtime error |
677 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
78 |
Runtime error |
712 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
79 |
Runtime error |
726 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
80 |
Runtime error |
720 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
81 |
Runtime error |
726 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
82 |
Runtime error |
721 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
83 |
Runtime error |
709 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
84 |
Runtime error |
729 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
85 |
Runtime error |
731 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
86 |
Runtime error |
688 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
87 |
Runtime error |
732 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
88 |
Runtime error |
725 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
89 |
Runtime error |
721 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
90 |
Runtime error |
722 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
91 |
Runtime error |
721 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
92 |
Runtime error |
726 ms |
65536 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |