#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 ( (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 |
31 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 |
19 ms |
8192 KB |
Output is correct |
5 |
Correct |
26 ms |
8192 KB |
Output is correct |
6 |
Correct |
29 ms |
8192 KB |
Output is correct |
7 |
Correct |
133 ms |
12152 KB |
Output is correct |
8 |
Correct |
20 ms |
8192 KB |
Output is correct |
9 |
Correct |
23 ms |
8192 KB |
Output is correct |
10 |
Correct |
20 ms |
8192 KB |
Output is correct |
11 |
Correct |
19 ms |
8192 KB |
Output is correct |
12 |
Incorrect |
22 ms |
8448 KB |
Output isn't correct |
13 |
Correct |
21 ms |
8320 KB |
Output is correct |
14 |
Correct |
19 ms |
8448 KB |
Output is correct |
15 |
Correct |
30 ms |
8192 KB |
Output is correct |
16 |
Correct |
18 ms |
8192 KB |
Output is correct |
17 |
Correct |
31 ms |
8320 KB |
Output is correct |
18 |
Correct |
19 ms |
8320 KB |
Output is correct |
19 |
Correct |
19 ms |
8320 KB |
Output is correct |
20 |
Correct |
19 ms |
8320 KB |
Output is correct |
21 |
Correct |
21 ms |
8320 KB |
Output is correct |
22 |
Correct |
20 ms |
8320 KB |
Output is correct |
23 |
Correct |
21 ms |
8320 KB |
Output is correct |
24 |
Correct |
21 ms |
8320 KB |
Output is correct |
25 |
Correct |
20 ms |
8448 KB |
Output is correct |
26 |
Correct |
22 ms |
8448 KB |
Output is correct |
27 |
Correct |
25 ms |
8448 KB |
Output is correct |
28 |
Correct |
22 ms |
8448 KB |
Output is correct |
29 |
Correct |
21 ms |
8448 KB |
Output is correct |
30 |
Correct |
20 ms |
8448 KB |
Output is correct |
31 |
Correct |
27 ms |
8448 KB |
Output is correct |
32 |
Correct |
20 ms |
8448 KB |
Output is correct |
33 |
Correct |
35 ms |
9728 KB |
Output is correct |
34 |
Correct |
28 ms |
9728 KB |
Output is correct |
35 |
Correct |
59 ms |
9728 KB |
Output is correct |
36 |
Correct |
30 ms |
9984 KB |
Output is correct |
37 |
Correct |
41 ms |
9984 KB |
Output is correct |
38 |
Correct |
62 ms |
9984 KB |
Output is correct |
39 |
Correct |
40 ms |
10240 KB |
Output is correct |
40 |
Correct |
34 ms |
10112 KB |
Output is correct |
41 |
Correct |
62 ms |
10112 KB |
Output is correct |
42 |
Correct |
35 ms |
10624 KB |
Output is correct |
43 |
Correct |
38 ms |
10744 KB |
Output is correct |
44 |
Correct |
78 ms |
10616 KB |
Output is correct |
45 |
Correct |
40 ms |
10880 KB |
Output is correct |
46 |
Correct |
46 ms |
10880 KB |
Output is correct |
47 |
Correct |
115 ms |
10872 KB |
Output is correct |
48 |
Correct |
44 ms |
11128 KB |
Output is correct |
49 |
Correct |
50 ms |
11136 KB |
Output is correct |
50 |
Correct |
121 ms |
11128 KB |
Output is correct |
51 |
Correct |
48 ms |
11392 KB |
Output is correct |
52 |
Correct |
51 ms |
11392 KB |
Output is correct |
53 |
Correct |
134 ms |
11512 KB |
Output is correct |
54 |
Correct |
54 ms |
11512 KB |
Output is correct |
55 |
Correct |
56 ms |
11512 KB |
Output is correct |
56 |
Correct |
155 ms |
11640 KB |
Output is correct |
57 |
Correct |
57 ms |
11768 KB |
Output is correct |
58 |
Correct |
55 ms |
11768 KB |
Output is correct |
59 |
Correct |
189 ms |
11864 KB |
Output is correct |
60 |
Correct |
69 ms |
12024 KB |
Output is correct |
61 |
Correct |
62 ms |
12024 KB |
Output is correct |
62 |
Correct |
197 ms |
12128 KB |
Output is correct |
63 |
Correct |
156 ms |
12152 KB |
Output is correct |
64 |
Correct |
234 ms |
12120 KB |
Output is correct |
65 |
Correct |
233 ms |
12120 KB |
Output is correct |
66 |
Correct |
187 ms |
12024 KB |
Output is correct |
67 |
Correct |
155 ms |
12124 KB |
Output is correct |
68 |
Correct |
98 ms |
12364 KB |
Output is correct |
69 |
Correct |
102 ms |
12280 KB |
Output is correct |
70 |
Correct |
81 ms |
12024 KB |
Output is correct |
71 |
Correct |
85 ms |
12024 KB |
Output is correct |
72 |
Correct |
77 ms |
12152 KB |
Output is correct |
73 |
Correct |
86 ms |
12664 KB |
Output is correct |
74 |
Correct |
118 ms |
12280 KB |
Output is correct |
75 |
Correct |
115 ms |
12280 KB |
Output is correct |
76 |
Correct |
111 ms |
12396 KB |
Output is correct |
77 |
Correct |
111 ms |
12408 KB |
Output is correct |
78 |
Correct |
114 ms |
12280 KB |
Output is correct |
79 |
Correct |
110 ms |
12280 KB |
Output is correct |
80 |
Correct |
101 ms |
12280 KB |
Output is correct |
81 |
Correct |
119 ms |
12280 KB |
Output is correct |
82 |
Correct |
102 ms |
12280 KB |
Output is correct |
83 |
Correct |
145 ms |
12280 KB |
Output is correct |
84 |
Correct |
116 ms |
12212 KB |
Output is correct |
85 |
Correct |
118 ms |
12332 KB |
Output is correct |
86 |
Correct |
125 ms |
12280 KB |
Output is correct |
87 |
Correct |
130 ms |
12280 KB |
Output is correct |
88 |
Correct |
138 ms |
12280 KB |
Output is correct |
89 |
Correct |
155 ms |
12328 KB |
Output is correct |
90 |
Correct |
160 ms |
12280 KB |
Output is correct |
91 |
Correct |
149 ms |
12280 KB |
Output is correct |
92 |
Correct |
145 ms |
12280 KB |
Output is correct |