#include <bits/stdc++.h>
using namespace std ;
const int N = 800 ;
int dx[]{0 , 0 , 1 , -1} ;
int dy[]{1 , -1 , 0 , 0} ;
int n , s ;
pair <int , int> Honey , Home ;
char a[N][N] ;
bool inside(int x , int y) {
return (x >= 0 && y >= 0 && x < n && y < n && a[x][y] != 'T') ;
}
bool val(int x , int y) {
return x / s < y ;
}
bool ok(int time) {
vector<vector<int>> mn2(N , vector<int>(N , 1e9)) ;
queue<pair <int , int>> q2 ;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < n ; j++) {
if(a[i][j] == 'H') {
q2.push({i, j}) ;
mn2[i][j] = 0 ;
}
}
}
while(q2.size()) {
pair <int , int> t = q2.front() ;
q2.pop() ;
for(int i = 0 ; i < 4 ; i++) {
int x = t.first + dx[i] ;
int y = t.second + dy[i] ;
if(inside(x , y) && mn2[t.first][t.second] + 1 < mn2[x][y]) {
mn2[x][y] = mn2[t.first][t.second] + 1 ;
q2.push({x, y}) ;
}
}
}
vector<vector<int>> mn(N , vector<int>(N , 1e9)) ;
queue <pair <int , int>> q ;
mn[Honey.first][Honey.second] = 0 ;
if(mn[Honey.first][Honey.second] > time)
q.push(Honey) ;
while(!q.empty()) {
pair <int , int> t = q.front() ;
q.pop() ;
for(int i = 0 ; i < 4 ; i++) {
int x = t.first + dx[i] ;
int y = t.second + dy[i] ;
if(val(mn[t.first][t.second] + 1 , mn2[x][y] - time) && inside(x , y) && mn[t.first][t.second] + 1 < mn[x][y]) {
mn[x][y] = mn[t.first][t.second] + 1 ;
q.push({x , y}) ;
}
}
}
if(mn[Home.first][Home.second] == 1e9)
return 0 ;
return 1 ;
}
int main() {
cin >> n >> s ;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < n ; j++) {
cin >> a[i][j] ;
if(a[i][j] == 'M')
Honey = {i , j} ;
if(a[i][j] == 'D')
Home = {i , j} ;
}
}
int l = 0 , r = n * n + 1 ;
while(l + 1 < r) {
int m = (l + r) / 2 ;
if(ok(m))
l = m ;
else
r = m ;
}
cout << l << endl ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
5496 KB |
Output isn't correct |
2 |
Incorrect |
10 ms |
5496 KB |
Output isn't correct |
3 |
Incorrect |
6 ms |
5536 KB |
Output isn't correct |
4 |
Incorrect |
9 ms |
5540 KB |
Output isn't correct |
5 |
Correct |
11 ms |
5496 KB |
Output is correct |
6 |
Incorrect |
9 ms |
5496 KB |
Output isn't correct |
7 |
Incorrect |
282 ms |
6136 KB |
Output isn't correct |
8 |
Incorrect |
11 ms |
5524 KB |
Output isn't correct |
9 |
Incorrect |
11 ms |
5548 KB |
Output isn't correct |
10 |
Incorrect |
14 ms |
5524 KB |
Output isn't correct |
11 |
Incorrect |
12 ms |
5540 KB |
Output isn't correct |
12 |
Incorrect |
16 ms |
5628 KB |
Output isn't correct |
13 |
Correct |
10 ms |
5548 KB |
Output is correct |
14 |
Incorrect |
12 ms |
5564 KB |
Output isn't correct |
15 |
Incorrect |
12 ms |
5528 KB |
Output isn't correct |
16 |
Incorrect |
16 ms |
5524 KB |
Output isn't correct |
17 |
Incorrect |
13 ms |
5628 KB |
Output isn't correct |
18 |
Incorrect |
14 ms |
5596 KB |
Output isn't correct |
19 |
Incorrect |
12 ms |
5524 KB |
Output isn't correct |
20 |
Incorrect |
19 ms |
5600 KB |
Output isn't correct |
21 |
Incorrect |
16 ms |
5624 KB |
Output isn't correct |
22 |
Incorrect |
16 ms |
5552 KB |
Output isn't correct |
23 |
Incorrect |
29 ms |
5540 KB |
Output isn't correct |
24 |
Incorrect |
16 ms |
5548 KB |
Output isn't correct |
25 |
Incorrect |
18 ms |
5612 KB |
Output isn't correct |
26 |
Incorrect |
22 ms |
5552 KB |
Output isn't correct |
27 |
Incorrect |
18 ms |
5652 KB |
Output isn't correct |
28 |
Incorrect |
25 ms |
5596 KB |
Output isn't correct |
29 |
Incorrect |
31 ms |
5648 KB |
Output isn't correct |
30 |
Incorrect |
18 ms |
5600 KB |
Output isn't correct |
31 |
Incorrect |
19 ms |
5624 KB |
Output isn't correct |
32 |
Incorrect |
18 ms |
5588 KB |
Output isn't correct |
33 |
Incorrect |
45 ms |
5908 KB |
Output isn't correct |
34 |
Incorrect |
59 ms |
5812 KB |
Output isn't correct |
35 |
Incorrect |
49 ms |
5772 KB |
Output isn't correct |
36 |
Incorrect |
63 ms |
5824 KB |
Output isn't correct |
37 |
Incorrect |
60 ms |
5884 KB |
Output isn't correct |
38 |
Incorrect |
62 ms |
5828 KB |
Output isn't correct |
39 |
Incorrect |
64 ms |
5880 KB |
Output isn't correct |
40 |
Incorrect |
67 ms |
5860 KB |
Output isn't correct |
41 |
Incorrect |
98 ms |
5888 KB |
Output isn't correct |
42 |
Incorrect |
70 ms |
5916 KB |
Output isn't correct |
43 |
Incorrect |
71 ms |
5908 KB |
Output isn't correct |
44 |
Incorrect |
126 ms |
5904 KB |
Output isn't correct |
45 |
Incorrect |
92 ms |
5940 KB |
Output isn't correct |
46 |
Incorrect |
83 ms |
5940 KB |
Output isn't correct |
47 |
Incorrect |
111 ms |
5960 KB |
Output isn't correct |
48 |
Incorrect |
104 ms |
6032 KB |
Output isn't correct |
49 |
Incorrect |
94 ms |
5976 KB |
Output isn't correct |
50 |
Incorrect |
119 ms |
5980 KB |
Output isn't correct |
51 |
Incorrect |
126 ms |
6008 KB |
Output isn't correct |
52 |
Incorrect |
129 ms |
6012 KB |
Output isn't correct |
53 |
Incorrect |
140 ms |
6016 KB |
Output isn't correct |
54 |
Incorrect |
112 ms |
6032 KB |
Output isn't correct |
55 |
Incorrect |
123 ms |
6016 KB |
Output isn't correct |
56 |
Incorrect |
154 ms |
6052 KB |
Output isn't correct |
57 |
Incorrect |
133 ms |
6120 KB |
Output isn't correct |
58 |
Incorrect |
129 ms |
6088 KB |
Output isn't correct |
59 |
Incorrect |
163 ms |
6084 KB |
Output isn't correct |
60 |
Incorrect |
151 ms |
6116 KB |
Output isn't correct |
61 |
Incorrect |
156 ms |
6124 KB |
Output isn't correct |
62 |
Incorrect |
207 ms |
6124 KB |
Output isn't correct |
63 |
Correct |
330 ms |
6116 KB |
Output is correct |
64 |
Incorrect |
309 ms |
6104 KB |
Output isn't correct |
65 |
Incorrect |
309 ms |
6108 KB |
Output isn't correct |
66 |
Incorrect |
328 ms |
6080 KB |
Output isn't correct |
67 |
Incorrect |
300 ms |
6112 KB |
Output isn't correct |
68 |
Correct |
387 ms |
6132 KB |
Output is correct |
69 |
Incorrect |
339 ms |
6124 KB |
Output isn't correct |
70 |
Incorrect |
359 ms |
6132 KB |
Output isn't correct |
71 |
Incorrect |
339 ms |
6124 KB |
Output isn't correct |
72 |
Incorrect |
355 ms |
6124 KB |
Output isn't correct |
73 |
Incorrect |
317 ms |
6140 KB |
Output isn't correct |
74 |
Incorrect |
314 ms |
6152 KB |
Output isn't correct |
75 |
Incorrect |
333 ms |
6124 KB |
Output isn't correct |
76 |
Incorrect |
309 ms |
6156 KB |
Output isn't correct |
77 |
Incorrect |
336 ms |
6132 KB |
Output isn't correct |
78 |
Incorrect |
295 ms |
6156 KB |
Output isn't correct |
79 |
Incorrect |
287 ms |
6160 KB |
Output isn't correct |
80 |
Incorrect |
278 ms |
6144 KB |
Output isn't correct |
81 |
Incorrect |
269 ms |
6152 KB |
Output isn't correct |
82 |
Incorrect |
340 ms |
6140 KB |
Output isn't correct |
83 |
Correct |
286 ms |
6144 KB |
Output is correct |
84 |
Incorrect |
317 ms |
6160 KB |
Output isn't correct |
85 |
Incorrect |
291 ms |
6116 KB |
Output isn't correct |
86 |
Incorrect |
328 ms |
6156 KB |
Output isn't correct |
87 |
Incorrect |
288 ms |
6128 KB |
Output isn't correct |
88 |
Correct |
308 ms |
6148 KB |
Output is correct |
89 |
Incorrect |
335 ms |
6128 KB |
Output isn't correct |
90 |
Incorrect |
302 ms |
6136 KB |
Output isn't correct |
91 |
Incorrect |
299 ms |
6148 KB |
Output isn't correct |
92 |
Incorrect |
291 ms |
6136 KB |
Output isn't correct |