#include<bits/stdc++.h>
using namespace std ;
const int N = 1000 ;
string ma[N] ;
int dist[N][N], dist2[N][N] ;
int X [] = {0,0,-1,1}, Y[] = {1,-1,0,0} ;
int visi[N][N] ;
int main() {
int n, s ;
cin >> n >> s ;
for ( int i = 0 ; i < n ; ++i ) cin >> ma[i] ;
queue <pair<int,int>> bfs ;
pair<int,int> bg, en ;
for ( int i = 0 ; i < n ; ++i ) {
for ( int j = 0 ; j < n ; ++j ) {
dist2[i][j] = N * N * N ;
if ( ma[i][j] == 'H' ) bfs.push({i,j}), dist2[i][j] = 0, visi[i][j] = 1 ;
if ( ma[i][j] == 'T' ) visi[i][j] = 1 ;
if ( ma[i][j] == 'M' ) bg = {i,j} ;
if ( ma[i][j] == 'D' ) en = {i,j} ;
}
}
while ( bfs.size() ) {
pair<int,int> cur = bfs.front() ;
bfs.pop() ;
for ( int k = 0 ; k < 4 ; ++k ) {
int nwx = X[k] + cur.first ;
int nwy = Y[k] + cur.second ;
if ( nwx < 0 || nwy < 0 || nwx >= n || nwy >= n || visi[nwx][nwy] ) continue ;
visi[nwx][nwy] = 1 ;
dist2[nwx][nwy] = 1 + dist2[cur.first][cur.second] ;
bfs.push({nwx, nwy}) ;
}
}
int lw = 0, hg = 2000000000 ;
while ( lw <= hg ) {
int mid = (lw+hg) / 2 ;
memset ( visi, 0, sizeof visi ) ;
memset ( dist, 0, sizeof dist ) ;
if ( mid < dist2[bg.first][bg.second] ) bfs.push(bg) ;
visi[bg.first][bg.second] = 1 ;
while ( bfs.size() ) {
pair<int,int> cur = bfs.front() ;
bfs.pop() ;
for ( int k = 0 ; k < 4 ; ++k ) {
int nwx = X[k] + cur.first ;
int nwy = Y[k] + cur.second ;
if ( nwx < 0 || nwy < 0 || nwx >= n || nwy >= n || visi[nwx][nwy] || ma[nwx][nwy] == 'T' ) continue ;
if ( (1 + dist[cur.first][cur.second]) % s == 0 && (nwx != en.first || nwy != en.second) && (1 + dist[cur.first][cur.second]) / s + mid >= dist2[nwx][nwy] ) continue ;
visi[nwx][nwy] = 1 ;
dist[nwx][nwy] = 1 + dist[cur.first][cur.second] ;
bfs.push({nwx, nwy}) ;
}
}
if ( visi[en.first][en.second] ) lw = mid+1 ;
else hg = mid-1 ;
}
cout << hg << endl ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
8192 KB |
Output is correct |
2 |
Correct |
19 ms |
8192 KB |
Output is correct |
3 |
Correct |
20 ms |
8192 KB |
Output is correct |
4 |
Correct |
27 ms |
8248 KB |
Output is correct |
5 |
Incorrect |
21 ms |
8192 KB |
Output isn't correct |
6 |
Incorrect |
27 ms |
8192 KB |
Output isn't correct |
7 |
Incorrect |
154 ms |
12284 KB |
Output isn't correct |
8 |
Correct |
19 ms |
8192 KB |
Output is correct |
9 |
Correct |
18 ms |
8192 KB |
Output is correct |
10 |
Correct |
20 ms |
8192 KB |
Output is correct |
11 |
Incorrect |
19 ms |
8192 KB |
Output isn't correct |
12 |
Incorrect |
21 ms |
8448 KB |
Output isn't correct |
13 |
Correct |
19 ms |
8320 KB |
Output is correct |
14 |
Incorrect |
28 ms |
8448 KB |
Output isn't correct |
15 |
Correct |
19 ms |
8192 KB |
Output is correct |
16 |
Incorrect |
22 ms |
8192 KB |
Output isn't correct |
17 |
Correct |
19 ms |
8320 KB |
Output is correct |
18 |
Incorrect |
20 ms |
8320 KB |
Output isn't correct |
19 |
Correct |
19 ms |
8320 KB |
Output is correct |
20 |
Incorrect |
20 ms |
8320 KB |
Output isn't correct |
21 |
Correct |
21 ms |
8320 KB |
Output is correct |
22 |
Incorrect |
19 ms |
8320 KB |
Output isn't correct |
23 |
Correct |
22 ms |
8320 KB |
Output is correct |
24 |
Incorrect |
23 ms |
8320 KB |
Output isn't correct |
25 |
Correct |
21 ms |
8448 KB |
Output is correct |
26 |
Incorrect |
21 ms |
8448 KB |
Output isn't correct |
27 |
Correct |
21 ms |
8448 KB |
Output is correct |
28 |
Incorrect |
31 ms |
8448 KB |
Output isn't correct |
29 |
Correct |
28 ms |
8448 KB |
Output is correct |
30 |
Incorrect |
21 ms |
8448 KB |
Output isn't correct |
31 |
Correct |
25 ms |
8448 KB |
Output is correct |
32 |
Incorrect |
26 ms |
8448 KB |
Output isn't correct |
33 |
Correct |
39 ms |
9728 KB |
Output is correct |
34 |
Incorrect |
34 ms |
9728 KB |
Output isn't correct |
35 |
Incorrect |
48 ms |
9728 KB |
Output isn't correct |
36 |
Correct |
36 ms |
9984 KB |
Output is correct |
37 |
Incorrect |
37 ms |
9984 KB |
Output isn't correct |
38 |
Incorrect |
44 ms |
9984 KB |
Output isn't correct |
39 |
Correct |
34 ms |
10112 KB |
Output is correct |
40 |
Incorrect |
32 ms |
10112 KB |
Output isn't correct |
41 |
Incorrect |
67 ms |
10104 KB |
Output isn't correct |
42 |
Correct |
42 ms |
10624 KB |
Output is correct |
43 |
Incorrect |
44 ms |
10616 KB |
Output isn't correct |
44 |
Incorrect |
56 ms |
10624 KB |
Output isn't correct |
45 |
Correct |
41 ms |
10872 KB |
Output is correct |
46 |
Incorrect |
40 ms |
10880 KB |
Output isn't correct |
47 |
Incorrect |
67 ms |
10872 KB |
Output isn't correct |
48 |
Correct |
44 ms |
11128 KB |
Output is correct |
49 |
Incorrect |
44 ms |
11136 KB |
Output isn't correct |
50 |
Incorrect |
71 ms |
11136 KB |
Output isn't correct |
51 |
Correct |
49 ms |
11384 KB |
Output is correct |
52 |
Incorrect |
50 ms |
11392 KB |
Output isn't correct |
53 |
Incorrect |
100 ms |
11384 KB |
Output isn't correct |
54 |
Correct |
54 ms |
11640 KB |
Output is correct |
55 |
Incorrect |
56 ms |
11512 KB |
Output isn't correct |
56 |
Incorrect |
121 ms |
11512 KB |
Output isn't correct |
57 |
Correct |
56 ms |
11768 KB |
Output is correct |
58 |
Incorrect |
55 ms |
11768 KB |
Output isn't correct |
59 |
Incorrect |
134 ms |
11896 KB |
Output isn't correct |
60 |
Correct |
69 ms |
12024 KB |
Output is correct |
61 |
Incorrect |
69 ms |
12024 KB |
Output isn't correct |
62 |
Incorrect |
150 ms |
12024 KB |
Output isn't correct |
63 |
Correct |
157 ms |
12120 KB |
Output is correct |
64 |
Correct |
249 ms |
12024 KB |
Output is correct |
65 |
Incorrect |
211 ms |
12152 KB |
Output isn't correct |
66 |
Correct |
191 ms |
12024 KB |
Output is correct |
67 |
Correct |
162 ms |
12152 KB |
Output is correct |
68 |
Incorrect |
95 ms |
12152 KB |
Output isn't correct |
69 |
Correct |
123 ms |
12152 KB |
Output is correct |
70 |
Incorrect |
91 ms |
12152 KB |
Output isn't correct |
71 |
Incorrect |
86 ms |
12156 KB |
Output isn't correct |
72 |
Correct |
81 ms |
12152 KB |
Output is correct |
73 |
Correct |
97 ms |
12280 KB |
Output is correct |
74 |
Incorrect |
125 ms |
12280 KB |
Output isn't correct |
75 |
Incorrect |
118 ms |
12536 KB |
Output isn't correct |
76 |
Incorrect |
119 ms |
12280 KB |
Output isn't correct |
77 |
Incorrect |
111 ms |
12280 KB |
Output isn't correct |
78 |
Incorrect |
136 ms |
12280 KB |
Output isn't correct |
79 |
Incorrect |
137 ms |
12280 KB |
Output isn't correct |
80 |
Incorrect |
114 ms |
12280 KB |
Output isn't correct |
81 |
Incorrect |
125 ms |
12276 KB |
Output isn't correct |
82 |
Incorrect |
115 ms |
12280 KB |
Output isn't correct |
83 |
Incorrect |
143 ms |
12280 KB |
Output isn't correct |
84 |
Incorrect |
137 ms |
12280 KB |
Output isn't correct |
85 |
Incorrect |
115 ms |
12408 KB |
Output isn't correct |
86 |
Incorrect |
125 ms |
12280 KB |
Output isn't correct |
87 |
Incorrect |
130 ms |
12408 KB |
Output isn't correct |
88 |
Incorrect |
118 ms |
12280 KB |
Output isn't correct |
89 |
Incorrect |
126 ms |
12280 KB |
Output isn't correct |
90 |
Incorrect |
128 ms |
12280 KB |
Output isn't correct |
91 |
Incorrect |
126 ms |
12280 KB |
Output isn't correct |
92 |
Incorrect |
142 ms |
12412 KB |
Output isn't correct |