#include <bits/stdc++.h>
using namespace std;
const int mxN=800;
int n, s, d1[mxN][mxN], d2[mxN][mxN], si, sj, ti, tj, lb=0, rb=mxN*mxN, nb[4][2]={{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
string g[mxN];
array<int, 2> qu[mxN*mxN];
void bfs(int d[mxN][mxN], int a) {
int qt=0;
for(int i=0; i<n; ++i)
for(int j=0; j<n; ++j)
if(!d[i][j])
qu[qt++]={i, j};
for(int i=0; i<qt; ++i) {
array<int, 2> u=qu[i];
if(a>=0&&d[u[0]][u[1]]/s+a>=d1[u[0]][u[1]])
continue;
for(int j=0; j<4; ++j) {
int ni=u[0]+nb[j][0], nj=u[1]+nb[j][1];
if(ni>=0&&ni<n&&nj>=0&&nj<n&&g[ni][nj]!='T'&&(a>=0||g[ni][nj]!='D')&&d[ni][nj]>1e7) {
d[ni][nj]=d[u[0]][u[1]]+1;
qu[qt++]={ni, nj};
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> s;
for(int i=0; i<n; ++i)
cin >> g[i];
memset(d1, 1, sizeof(d1));
for(int i=0; i<n; ++i) {
for(int j=0; j<n; ++j) {
if(g[i][j]=='M') {
si=i;
sj=j;
} else if(g[i][j]=='D') {
ti=i;
tj=j;
} else if(g[i][j]=='H')
d1[i][j]=0;
}
}
bfs(d1, -1);
while(lb<=rb) {
int mb=(lb+rb)/2;
memset(d2, 1, sizeof(d2));
d2[si][sj]=0;
bfs(d2, mb);
if(d2[ti][tj]<1e7)
lb=mb+1;
else
rb=mb-1;
}
cout << rb;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5324 KB |
Output is correct |
2 |
Correct |
3 ms |
5324 KB |
Output is correct |
3 |
Correct |
3 ms |
5324 KB |
Output is correct |
4 |
Correct |
4 ms |
5324 KB |
Output is correct |
5 |
Correct |
4 ms |
5324 KB |
Output is correct |
6 |
Correct |
4 ms |
5324 KB |
Output is correct |
7 |
Correct |
75 ms |
11004 KB |
Output is correct |
8 |
Correct |
4 ms |
5324 KB |
Output is correct |
9 |
Correct |
3 ms |
5324 KB |
Output is correct |
10 |
Correct |
4 ms |
5324 KB |
Output is correct |
11 |
Correct |
3 ms |
5324 KB |
Output is correct |
12 |
Correct |
4 ms |
5324 KB |
Output is correct |
13 |
Correct |
4 ms |
5324 KB |
Output is correct |
14 |
Correct |
4 ms |
5324 KB |
Output is correct |
15 |
Correct |
4 ms |
5324 KB |
Output is correct |
16 |
Correct |
4 ms |
5324 KB |
Output is correct |
17 |
Correct |
4 ms |
5324 KB |
Output is correct |
18 |
Correct |
3 ms |
5336 KB |
Output is correct |
19 |
Correct |
4 ms |
5324 KB |
Output is correct |
20 |
Correct |
3 ms |
5324 KB |
Output is correct |
21 |
Correct |
4 ms |
5324 KB |
Output is correct |
22 |
Correct |
4 ms |
5324 KB |
Output is correct |
23 |
Correct |
4 ms |
5324 KB |
Output is correct |
24 |
Correct |
4 ms |
5324 KB |
Output is correct |
25 |
Correct |
4 ms |
5324 KB |
Output is correct |
26 |
Correct |
4 ms |
5372 KB |
Output is correct |
27 |
Correct |
4 ms |
5324 KB |
Output is correct |
28 |
Correct |
4 ms |
5324 KB |
Output is correct |
29 |
Correct |
4 ms |
5324 KB |
Output is correct |
30 |
Correct |
4 ms |
5324 KB |
Output is correct |
31 |
Correct |
4 ms |
5324 KB |
Output is correct |
32 |
Correct |
4 ms |
5324 KB |
Output is correct |
33 |
Correct |
9 ms |
5964 KB |
Output is correct |
34 |
Correct |
7 ms |
5836 KB |
Output is correct |
35 |
Correct |
20 ms |
6092 KB |
Output is correct |
36 |
Correct |
8 ms |
6120 KB |
Output is correct |
37 |
Correct |
9 ms |
6116 KB |
Output is correct |
38 |
Correct |
27 ms |
6348 KB |
Output is correct |
39 |
Correct |
12 ms |
6348 KB |
Output is correct |
40 |
Correct |
11 ms |
6348 KB |
Output is correct |
41 |
Correct |
32 ms |
6732 KB |
Output is correct |
42 |
Correct |
11 ms |
6600 KB |
Output is correct |
43 |
Correct |
11 ms |
6596 KB |
Output is correct |
44 |
Correct |
39 ms |
6988 KB |
Output is correct |
45 |
Correct |
12 ms |
6732 KB |
Output is correct |
46 |
Correct |
14 ms |
6844 KB |
Output is correct |
47 |
Correct |
46 ms |
7372 KB |
Output is correct |
48 |
Correct |
13 ms |
7068 KB |
Output is correct |
49 |
Correct |
19 ms |
7116 KB |
Output is correct |
50 |
Correct |
61 ms |
7828 KB |
Output is correct |
51 |
Correct |
14 ms |
7372 KB |
Output is correct |
52 |
Correct |
15 ms |
7384 KB |
Output is correct |
53 |
Correct |
70 ms |
8248 KB |
Output is correct |
54 |
Correct |
17 ms |
7756 KB |
Output is correct |
55 |
Correct |
17 ms |
7784 KB |
Output is correct |
56 |
Correct |
87 ms |
8780 KB |
Output is correct |
57 |
Correct |
19 ms |
8140 KB |
Output is correct |
58 |
Correct |
22 ms |
8140 KB |
Output is correct |
59 |
Correct |
102 ms |
9216 KB |
Output is correct |
60 |
Correct |
25 ms |
8536 KB |
Output is correct |
61 |
Correct |
26 ms |
8524 KB |
Output is correct |
62 |
Correct |
110 ms |
9632 KB |
Output is correct |
63 |
Correct |
99 ms |
8988 KB |
Output is correct |
64 |
Correct |
177 ms |
8992 KB |
Output is correct |
65 |
Correct |
142 ms |
8988 KB |
Output is correct |
66 |
Correct |
119 ms |
8908 KB |
Output is correct |
67 |
Correct |
109 ms |
8984 KB |
Output is correct |
68 |
Correct |
51 ms |
8992 KB |
Output is correct |
69 |
Correct |
47 ms |
8908 KB |
Output is correct |
70 |
Correct |
39 ms |
8952 KB |
Output is correct |
71 |
Correct |
45 ms |
8992 KB |
Output is correct |
72 |
Correct |
35 ms |
8876 KB |
Output is correct |
73 |
Correct |
36 ms |
10948 KB |
Output is correct |
74 |
Correct |
54 ms |
10904 KB |
Output is correct |
75 |
Correct |
67 ms |
10956 KB |
Output is correct |
76 |
Correct |
65 ms |
10956 KB |
Output is correct |
77 |
Correct |
58 ms |
10952 KB |
Output is correct |
78 |
Correct |
75 ms |
10964 KB |
Output is correct |
79 |
Correct |
48 ms |
10960 KB |
Output is correct |
80 |
Correct |
57 ms |
10964 KB |
Output is correct |
81 |
Correct |
65 ms |
10876 KB |
Output is correct |
82 |
Correct |
57 ms |
10956 KB |
Output is correct |
83 |
Correct |
72 ms |
10956 KB |
Output is correct |
84 |
Correct |
63 ms |
10956 KB |
Output is correct |
85 |
Correct |
70 ms |
10956 KB |
Output is correct |
86 |
Correct |
72 ms |
10948 KB |
Output is correct |
87 |
Correct |
69 ms |
10820 KB |
Output is correct |
88 |
Correct |
73 ms |
10956 KB |
Output is correct |
89 |
Correct |
73 ms |
10956 KB |
Output is correct |
90 |
Correct |
74 ms |
10956 KB |
Output is correct |
91 |
Correct |
79 ms |
10972 KB |
Output is correct |
92 |
Correct |
82 ms |
10960 KB |
Output is correct |