#include <bits/stdc++.h>
using namespace std ;
const int N = 1001 ;
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 ;
}
int ok(int time) {
int mn2[n][n] ;
fill_n(&mn2[0][0] , n*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}) ;
}
}
}
int mn[n][n] ;
fill_n(&mn[0][0] , n * n , 1e9 ) ;
queue <pair <int , int>> q ;
mn[Honey.first][Honey.second] = 0 ;
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() {
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cout.tie(0) ;
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 = 1e9 ;
while(l + 1 < r) {
int m = (l + r) / 2 ;
if(ok(m))
l = m ;
else
r = m ;
}
cout << l << "\n" ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
431 ms |
6968 KB |
Output isn't correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
14 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
432 KB |
Output is correct |
25 |
Correct |
1 ms |
556 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
3 ms |
344 KB |
Output is correct |
28 |
Correct |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
348 KB |
Output is correct |
32 |
Correct |
3 ms |
348 KB |
Output is correct |
33 |
Correct |
31 ms |
1888 KB |
Output is correct |
34 |
Correct |
31 ms |
1628 KB |
Output is correct |
35 |
Correct |
55 ms |
1880 KB |
Output is correct |
36 |
Correct |
39 ms |
2140 KB |
Output is correct |
37 |
Correct |
39 ms |
2140 KB |
Output is correct |
38 |
Runtime error |
68 ms |
4028 KB |
Execution killed with signal 11 |
39 |
Correct |
53 ms |
2664 KB |
Output is correct |
40 |
Correct |
62 ms |
2664 KB |
Output is correct |
41 |
Correct |
94 ms |
2672 KB |
Output is correct |
42 |
Correct |
68 ms |
3132 KB |
Output is correct |
43 |
Runtime error |
35 ms |
5688 KB |
Execution killed with signal 11 |
44 |
Correct |
131 ms |
3164 KB |
Output is correct |
45 |
Correct |
76 ms |
3640 KB |
Output is correct |
46 |
Correct |
77 ms |
3644 KB |
Output is correct |
47 |
Correct |
154 ms |
3416 KB |
Output is correct |
48 |
Correct |
124 ms |
4184 KB |
Output is correct |
49 |
Correct |
97 ms |
4184 KB |
Output is correct |
50 |
Correct |
175 ms |
4184 KB |
Output is correct |
51 |
Correct |
111 ms |
4652 KB |
Output is correct |
52 |
Correct |
131 ms |
4700 KB |
Output is correct |
53 |
Runtime error |
183 ms |
8912 KB |
Execution killed with signal 11 |
54 |
Correct |
125 ms |
5208 KB |
Output is correct |
55 |
Correct |
117 ms |
5212 KB |
Output is correct |
56 |
Correct |
234 ms |
5448 KB |
Output is correct |
57 |
Correct |
143 ms |
5976 KB |
Output is correct |
58 |
Correct |
127 ms |
5980 KB |
Output is correct |
59 |
Correct |
263 ms |
6140 KB |
Output is correct |
60 |
Correct |
158 ms |
6748 KB |
Output is correct |
61 |
Correct |
150 ms |
6744 KB |
Output is correct |
62 |
Correct |
303 ms |
6740 KB |
Output is correct |
63 |
Correct |
412 ms |
6688 KB |
Output is correct |
64 |
Correct |
503 ms |
6860 KB |
Output is correct |
65 |
Correct |
486 ms |
6864 KB |
Output is correct |
66 |
Correct |
448 ms |
6744 KB |
Output is correct |
67 |
Incorrect |
416 ms |
6748 KB |
Output isn't correct |
68 |
Correct |
432 ms |
6888 KB |
Output is correct |
69 |
Correct |
448 ms |
6892 KB |
Output is correct |
70 |
Correct |
453 ms |
6896 KB |
Output is correct |
71 |
Correct |
417 ms |
6892 KB |
Output is correct |
72 |
Incorrect |
424 ms |
6888 KB |
Output isn't correct |
73 |
Incorrect |
395 ms |
7252 KB |
Output isn't correct |
74 |
Correct |
415 ms |
7004 KB |
Output is correct |
75 |
Correct |
430 ms |
7136 KB |
Output is correct |
76 |
Correct |
411 ms |
7128 KB |
Output is correct |
77 |
Correct |
421 ms |
7004 KB |
Output is correct |
78 |
Incorrect |
371 ms |
7252 KB |
Output isn't correct |
79 |
Correct |
381 ms |
7116 KB |
Output is correct |
80 |
Correct |
378 ms |
7004 KB |
Output is correct |
81 |
Correct |
376 ms |
6992 KB |
Output is correct |
82 |
Correct |
380 ms |
6992 KB |
Output is correct |
83 |
Correct |
381 ms |
6992 KB |
Output is correct |
84 |
Correct |
391 ms |
6992 KB |
Output is correct |
85 |
Correct |
402 ms |
7248 KB |
Output is correct |
86 |
Correct |
393 ms |
7000 KB |
Output is correct |
87 |
Correct |
390 ms |
6996 KB |
Output is correct |
88 |
Correct |
376 ms |
6992 KB |
Output is correct |
89 |
Correct |
391 ms |
7028 KB |
Output is correct |
90 |
Correct |
400 ms |
7252 KB |
Output is correct |
91 |
Correct |
391 ms |
7024 KB |
Output is correct |
92 |
Correct |
407 ms |
6996 KB |
Output is correct |