import java.util.*;
import java.io.*;
public class mecho {
private static int[] dx = {-1, 0, 1, 0};
private static int[] dy = {0, -1, 0, 1};
private static int n, s;
private static char[][] grid;
private static int[][] bees;
private static int[] start, end;
private static boolean valid(int x, int y){
return 0 <= x && x < n && 0 <= y && y < n;
}
private static boolean check(int x){
if(bees[start[0]][start[1]] <= x){
return false;
}
boolean[][] dist = new boolean[n][n];
Queue<int[]> cur = new LinkedList<>();
cur.add(new int[]{start[0], start[1], 0});
dist[start[0]][start[1]] = true;
while(!cur.isEmpty()){
int[] z = cur.poll();
for (int i = 0; i < 4; i++) {
int fx = z[0] + dx[i];
int fy = z[1] + dy[i];
if(valid(fx, fy) && !dist[fx][fy] && grid[fx][fy] != 'T' && (z[2] + 1)/s + x < bees[fx][fy]){
dist[fx][fy] = true;
cur.add(new int[]{fx, fy, z[2] + 1});
}
}
}
return dist[end[0]][end[1]];
}
public static void main(String[] args) throws IOException{
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(r.readLine());
n = Integer.parseInt(st.nextToken()); s = Integer.parseInt(st.nextToken());
grid = new char[n][n];
start = new int[]{-1, -1}; end = new int[]{-1, -1};
Queue<int[]> hives = new LinkedList<>();
bees = new int[n][n];
for (int i = 0; i < n; i++) {
Arrays.fill(bees[i], Integer.MAX_VALUE);
}
for (int i = 0; i < n; i++) {
String str = r.readLine();
for (int j = 0; j < n; j++) {
if(str.charAt(j) == 'M'){
start = new int[]{i, j};
}
if(str.charAt(j) == 'D'){
end = new int[]{i, j};
}
if(str.charAt(j) == 'H'){
hives.add(new int[]{i, j, 0});
bees[i][j] = 0;
}
grid[i][j] = str.charAt(j);
}
}
while(!hives.isEmpty()){
int[] x = hives.poll();
for (int i = 0; i < 4; i++) {
int fx = x[0] + dx[i];
int fy = x[1] + dy[i];
if(valid(fx, fy) && bees[fx][fy] == Integer.MAX_VALUE && (grid[fx][fy] == 'G' || grid[fx][fy] == 'M')){
bees[fx][fy] = x[2] + 1;
hives.add(new int[]{fx, fy, x[2] + 1});
}
}
}
int left = 0;
int right = n * n;
while(left < right){
int mid = left + (right - left + 1)/2;
if(check(mid)){
left = mid;
}
else{
right = mid - 1;
}
}
if(check(right)){
pw.println(right);
}
else{
pw.println(-1);
}
pw.close();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
8432 KB |
Output is correct |
2 |
Correct |
51 ms |
8444 KB |
Output is correct |
3 |
Correct |
51 ms |
8352 KB |
Output is correct |
4 |
Correct |
49 ms |
8252 KB |
Output is correct |
5 |
Correct |
47 ms |
8580 KB |
Output is correct |
6 |
Correct |
50 ms |
8440 KB |
Output is correct |
7 |
Correct |
339 ms |
23804 KB |
Output is correct |
8 |
Correct |
50 ms |
8248 KB |
Output is correct |
9 |
Correct |
48 ms |
8184 KB |
Output is correct |
10 |
Correct |
54 ms |
8376 KB |
Output is correct |
11 |
Correct |
48 ms |
8468 KB |
Output is correct |
12 |
Correct |
56 ms |
8756 KB |
Output is correct |
13 |
Correct |
56 ms |
8624 KB |
Output is correct |
14 |
Correct |
90 ms |
11208 KB |
Output is correct |
15 |
Correct |
49 ms |
8440 KB |
Output is correct |
16 |
Correct |
55 ms |
8344 KB |
Output is correct |
17 |
Correct |
51 ms |
8496 KB |
Output is correct |
18 |
Correct |
52 ms |
8464 KB |
Output is correct |
19 |
Correct |
50 ms |
8384 KB |
Output is correct |
20 |
Correct |
51 ms |
8388 KB |
Output is correct |
21 |
Correct |
54 ms |
8692 KB |
Output is correct |
22 |
Correct |
57 ms |
8508 KB |
Output is correct |
23 |
Correct |
56 ms |
8644 KB |
Output is correct |
24 |
Correct |
57 ms |
8976 KB |
Output is correct |
25 |
Correct |
59 ms |
8760 KB |
Output is correct |
26 |
Correct |
71 ms |
10652 KB |
Output is correct |
27 |
Correct |
62 ms |
8684 KB |
Output is correct |
28 |
Correct |
70 ms |
10792 KB |
Output is correct |
29 |
Correct |
63 ms |
8856 KB |
Output is correct |
30 |
Correct |
70 ms |
10604 KB |
Output is correct |
31 |
Correct |
76 ms |
10708 KB |
Output is correct |
32 |
Correct |
72 ms |
10808 KB |
Output is correct |
33 |
Correct |
169 ms |
14748 KB |
Output is correct |
34 |
Correct |
196 ms |
15056 KB |
Output is correct |
35 |
Correct |
206 ms |
15944 KB |
Output is correct |
36 |
Correct |
167 ms |
15176 KB |
Output is correct |
37 |
Correct |
184 ms |
15672 KB |
Output is correct |
38 |
Correct |
214 ms |
16448 KB |
Output is correct |
39 |
Correct |
174 ms |
15392 KB |
Output is correct |
40 |
Correct |
198 ms |
15892 KB |
Output is correct |
41 |
Correct |
249 ms |
17480 KB |
Output is correct |
42 |
Correct |
186 ms |
15800 KB |
Output is correct |
43 |
Correct |
194 ms |
16208 KB |
Output is correct |
44 |
Correct |
251 ms |
18332 KB |
Output is correct |
45 |
Correct |
182 ms |
16356 KB |
Output is correct |
46 |
Correct |
205 ms |
16808 KB |
Output is correct |
47 |
Correct |
273 ms |
18800 KB |
Output is correct |
48 |
Correct |
180 ms |
16872 KB |
Output is correct |
49 |
Correct |
207 ms |
17272 KB |
Output is correct |
50 |
Correct |
308 ms |
19296 KB |
Output is correct |
51 |
Correct |
182 ms |
17344 KB |
Output is correct |
52 |
Correct |
215 ms |
17840 KB |
Output is correct |
53 |
Correct |
316 ms |
19792 KB |
Output is correct |
54 |
Correct |
189 ms |
18264 KB |
Output is correct |
55 |
Correct |
216 ms |
19312 KB |
Output is correct |
56 |
Correct |
337 ms |
21292 KB |
Output is correct |
57 |
Correct |
189 ms |
19408 KB |
Output is correct |
58 |
Correct |
221 ms |
20648 KB |
Output is correct |
59 |
Correct |
348 ms |
22320 KB |
Output is correct |
60 |
Correct |
195 ms |
21088 KB |
Output is correct |
61 |
Correct |
223 ms |
21368 KB |
Output is correct |
62 |
Correct |
379 ms |
23700 KB |
Output is correct |
63 |
Correct |
318 ms |
23904 KB |
Output is correct |
64 |
Correct |
433 ms |
23760 KB |
Output is correct |
65 |
Correct |
384 ms |
23732 KB |
Output is correct |
66 |
Correct |
361 ms |
23488 KB |
Output is correct |
67 |
Correct |
331 ms |
23596 KB |
Output is correct |
68 |
Correct |
243 ms |
22156 KB |
Output is correct |
69 |
Correct |
235 ms |
21060 KB |
Output is correct |
70 |
Correct |
237 ms |
21496 KB |
Output is correct |
71 |
Correct |
244 ms |
20612 KB |
Output is correct |
72 |
Correct |
215 ms |
18692 KB |
Output is correct |
73 |
Correct |
246 ms |
25768 KB |
Output is correct |
74 |
Correct |
303 ms |
25872 KB |
Output is correct |
75 |
Correct |
312 ms |
25832 KB |
Output is correct |
76 |
Correct |
313 ms |
25888 KB |
Output is correct |
77 |
Correct |
330 ms |
25956 KB |
Output is correct |
78 |
Correct |
357 ms |
25496 KB |
Output is correct |
79 |
Correct |
319 ms |
25408 KB |
Output is correct |
80 |
Correct |
298 ms |
25600 KB |
Output is correct |
81 |
Correct |
347 ms |
25548 KB |
Output is correct |
82 |
Correct |
333 ms |
25612 KB |
Output is correct |
83 |
Correct |
339 ms |
24916 KB |
Output is correct |
84 |
Correct |
348 ms |
25036 KB |
Output is correct |
85 |
Correct |
331 ms |
25448 KB |
Output is correct |
86 |
Correct |
331 ms |
25304 KB |
Output is correct |
87 |
Correct |
337 ms |
24852 KB |
Output is correct |
88 |
Correct |
335 ms |
24756 KB |
Output is correct |
89 |
Correct |
355 ms |
25052 KB |
Output is correct |
90 |
Correct |
373 ms |
24784 KB |
Output is correct |
91 |
Correct |
351 ms |
25048 KB |
Output is correct |
92 |
Correct |
354 ms |
24728 KB |
Output is correct |