#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&&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||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;
}
}
// cout << "hi1" << endl;
bfs(d1, 0);
// cout << "hi2" << endl;
while(lb<=rb) {
int mb=(lb+rb)/2;
// cout << "bs " << mb << endl;
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 |
9 ms |
5368 KB |
Output is correct |
2 |
Correct |
9 ms |
5504 KB |
Output is correct |
3 |
Correct |
8 ms |
5556 KB |
Output is correct |
4 |
Correct |
9 ms |
5556 KB |
Output is correct |
5 |
Incorrect |
9 ms |
5584 KB |
Output isn't correct |
6 |
Correct |
9 ms |
5784 KB |
Output is correct |
7 |
Correct |
131 ms |
11900 KB |
Output is correct |
8 |
Correct |
9 ms |
11900 KB |
Output is correct |
9 |
Correct |
9 ms |
11900 KB |
Output is correct |
10 |
Correct |
8 ms |
11900 KB |
Output is correct |
11 |
Correct |
9 ms |
11900 KB |
Output is correct |
12 |
Correct |
9 ms |
11900 KB |
Output is correct |
13 |
Incorrect |
9 ms |
11900 KB |
Output isn't correct |
14 |
Correct |
2 ms |
11900 KB |
Output is correct |
15 |
Correct |
9 ms |
11900 KB |
Output is correct |
16 |
Correct |
9 ms |
11900 KB |
Output is correct |
17 |
Correct |
8 ms |
11900 KB |
Output is correct |
18 |
Correct |
8 ms |
11900 KB |
Output is correct |
19 |
Correct |
9 ms |
11900 KB |
Output is correct |
20 |
Correct |
9 ms |
11900 KB |
Output is correct |
21 |
Correct |
9 ms |
11900 KB |
Output is correct |
22 |
Correct |
9 ms |
11900 KB |
Output is correct |
23 |
Correct |
8 ms |
11900 KB |
Output is correct |
24 |
Correct |
8 ms |
11900 KB |
Output is correct |
25 |
Correct |
9 ms |
11900 KB |
Output is correct |
26 |
Correct |
10 ms |
11900 KB |
Output is correct |
27 |
Correct |
11 ms |
11900 KB |
Output is correct |
28 |
Correct |
9 ms |
11900 KB |
Output is correct |
29 |
Correct |
9 ms |
11900 KB |
Output is correct |
30 |
Correct |
8 ms |
11900 KB |
Output is correct |
31 |
Correct |
9 ms |
11900 KB |
Output is correct |
32 |
Correct |
9 ms |
11900 KB |
Output is correct |
33 |
Correct |
14 ms |
11900 KB |
Output is correct |
34 |
Correct |
13 ms |
11900 KB |
Output is correct |
35 |
Correct |
33 ms |
11900 KB |
Output is correct |
36 |
Correct |
16 ms |
11900 KB |
Output is correct |
37 |
Correct |
17 ms |
11900 KB |
Output is correct |
38 |
Correct |
41 ms |
11900 KB |
Output is correct |
39 |
Correct |
16 ms |
11900 KB |
Output is correct |
40 |
Correct |
18 ms |
11900 KB |
Output is correct |
41 |
Correct |
52 ms |
11900 KB |
Output is correct |
42 |
Correct |
22 ms |
11900 KB |
Output is correct |
43 |
Correct |
18 ms |
11900 KB |
Output is correct |
44 |
Correct |
65 ms |
11900 KB |
Output is correct |
45 |
Correct |
21 ms |
11900 KB |
Output is correct |
46 |
Correct |
27 ms |
11900 KB |
Output is correct |
47 |
Correct |
85 ms |
11900 KB |
Output is correct |
48 |
Correct |
21 ms |
11900 KB |
Output is correct |
49 |
Correct |
23 ms |
12172 KB |
Output is correct |
50 |
Correct |
117 ms |
13168 KB |
Output is correct |
51 |
Correct |
25 ms |
13268 KB |
Output is correct |
52 |
Correct |
26 ms |
13684 KB |
Output is correct |
53 |
Correct |
117 ms |
14868 KB |
Output is correct |
54 |
Correct |
29 ms |
14900 KB |
Output is correct |
55 |
Correct |
28 ms |
15384 KB |
Output is correct |
56 |
Correct |
143 ms |
16760 KB |
Output is correct |
57 |
Correct |
30 ms |
16760 KB |
Output is correct |
58 |
Correct |
33 ms |
17408 KB |
Output is correct |
59 |
Correct |
194 ms |
18984 KB |
Output is correct |
60 |
Correct |
35 ms |
18984 KB |
Output is correct |
61 |
Correct |
38 ms |
19524 KB |
Output is correct |
62 |
Correct |
193 ms |
21304 KB |
Output is correct |
63 |
Incorrect |
136 ms |
21304 KB |
Output isn't correct |
64 |
Correct |
282 ms |
21788 KB |
Output is correct |
65 |
Correct |
199 ms |
22564 KB |
Output is correct |
66 |
Correct |
155 ms |
23188 KB |
Output is correct |
67 |
Correct |
161 ms |
23672 KB |
Output is correct |
68 |
Incorrect |
93 ms |
24452 KB |
Output isn't correct |
69 |
Correct |
73 ms |
24948 KB |
Output is correct |
70 |
Correct |
60 ms |
25656 KB |
Output is correct |
71 |
Correct |
71 ms |
26268 KB |
Output is correct |
72 |
Correct |
50 ms |
26924 KB |
Output is correct |
73 |
Correct |
55 ms |
29540 KB |
Output is correct |
74 |
Correct |
83 ms |
30168 KB |
Output is correct |
75 |
Correct |
119 ms |
30796 KB |
Output is correct |
76 |
Correct |
91 ms |
31424 KB |
Output is correct |
77 |
Correct |
92 ms |
32116 KB |
Output is correct |
78 |
Correct |
124 ms |
32680 KB |
Output is correct |
79 |
Correct |
74 ms |
33308 KB |
Output is correct |
80 |
Correct |
86 ms |
34032 KB |
Output is correct |
81 |
Correct |
105 ms |
34564 KB |
Output is correct |
82 |
Correct |
84 ms |
35192 KB |
Output is correct |
83 |
Incorrect |
120 ms |
35840 KB |
Output isn't correct |
84 |
Correct |
113 ms |
36468 KB |
Output is correct |
85 |
Correct |
114 ms |
37096 KB |
Output is correct |
86 |
Correct |
117 ms |
37756 KB |
Output is correct |
87 |
Correct |
118 ms |
38384 KB |
Output is correct |
88 |
Incorrect |
127 ms |
39012 KB |
Output isn't correct |
89 |
Correct |
117 ms |
39588 KB |
Output is correct |
90 |
Correct |
117 ms |
40284 KB |
Output is correct |
91 |
Correct |
128 ms |
40912 KB |
Output is correct |
92 |
Correct |
124 ms |
41540 KB |
Output is correct |