/*
- Binary search by answer
- BFS on all cells that Mecho can beat the bees to
*/
#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 |
3 ms |
5976 KB |
Output is correct |
2 |
Correct |
3 ms |
5980 KB |
Output is correct |
3 |
Correct |
3 ms |
5980 KB |
Output is correct |
4 |
Correct |
4 ms |
6136 KB |
Output is correct |
5 |
Correct |
2 ms |
5980 KB |
Output is correct |
6 |
Correct |
3 ms |
5980 KB |
Output is correct |
7 |
Correct |
71 ms |
11164 KB |
Output is correct |
8 |
Correct |
2 ms |
5980 KB |
Output is correct |
9 |
Correct |
3 ms |
5980 KB |
Output is correct |
10 |
Correct |
2 ms |
5980 KB |
Output is correct |
11 |
Correct |
3 ms |
6232 KB |
Output is correct |
12 |
Correct |
2 ms |
6236 KB |
Output is correct |
13 |
Correct |
3 ms |
6236 KB |
Output is correct |
14 |
Correct |
3 ms |
6248 KB |
Output is correct |
15 |
Correct |
3 ms |
5980 KB |
Output is correct |
16 |
Correct |
3 ms |
5980 KB |
Output is correct |
17 |
Correct |
2 ms |
5980 KB |
Output is correct |
18 |
Correct |
2 ms |
5976 KB |
Output is correct |
19 |
Correct |
2 ms |
5980 KB |
Output is correct |
20 |
Correct |
2 ms |
5980 KB |
Output is correct |
21 |
Correct |
3 ms |
5980 KB |
Output is correct |
22 |
Correct |
2 ms |
6232 KB |
Output is correct |
23 |
Correct |
2 ms |
6080 KB |
Output is correct |
24 |
Correct |
3 ms |
5976 KB |
Output is correct |
25 |
Correct |
4 ms |
6488 KB |
Output is correct |
26 |
Correct |
3 ms |
6236 KB |
Output is correct |
27 |
Correct |
3 ms |
6236 KB |
Output is correct |
28 |
Correct |
3 ms |
6232 KB |
Output is correct |
29 |
Correct |
3 ms |
6236 KB |
Output is correct |
30 |
Correct |
2 ms |
6236 KB |
Output is correct |
31 |
Correct |
2 ms |
6236 KB |
Output is correct |
32 |
Correct |
2 ms |
6236 KB |
Output is correct |
33 |
Correct |
6 ms |
8540 KB |
Output is correct |
34 |
Correct |
6 ms |
8540 KB |
Output is correct |
35 |
Correct |
16 ms |
8572 KB |
Output is correct |
36 |
Correct |
7 ms |
8536 KB |
Output is correct |
37 |
Correct |
7 ms |
8540 KB |
Output is correct |
38 |
Correct |
20 ms |
8612 KB |
Output is correct |
39 |
Correct |
8 ms |
8540 KB |
Output is correct |
40 |
Correct |
8 ms |
8540 KB |
Output is correct |
41 |
Correct |
26 ms |
8660 KB |
Output is correct |
42 |
Correct |
9 ms |
8536 KB |
Output is correct |
43 |
Correct |
11 ms |
8540 KB |
Output is correct |
44 |
Correct |
38 ms |
8540 KB |
Output is correct |
45 |
Correct |
10 ms |
8792 KB |
Output is correct |
46 |
Correct |
11 ms |
8540 KB |
Output is correct |
47 |
Correct |
40 ms |
8540 KB |
Output is correct |
48 |
Correct |
11 ms |
8792 KB |
Output is correct |
49 |
Correct |
11 ms |
8824 KB |
Output is correct |
50 |
Correct |
52 ms |
8796 KB |
Output is correct |
51 |
Correct |
14 ms |
8792 KB |
Output is correct |
52 |
Correct |
16 ms |
8792 KB |
Output is correct |
53 |
Correct |
54 ms |
10840 KB |
Output is correct |
54 |
Correct |
15 ms |
9048 KB |
Output is correct |
55 |
Correct |
15 ms |
8796 KB |
Output is correct |
56 |
Correct |
62 ms |
10992 KB |
Output is correct |
57 |
Correct |
17 ms |
8792 KB |
Output is correct |
58 |
Correct |
18 ms |
8796 KB |
Output is correct |
59 |
Correct |
90 ms |
11072 KB |
Output is correct |
60 |
Correct |
20 ms |
11096 KB |
Output is correct |
61 |
Correct |
21 ms |
11100 KB |
Output is correct |
62 |
Correct |
94 ms |
11144 KB |
Output is correct |
63 |
Correct |
79 ms |
11144 KB |
Output is correct |
64 |
Correct |
146 ms |
11096 KB |
Output is correct |
65 |
Correct |
112 ms |
11148 KB |
Output is correct |
66 |
Correct |
100 ms |
11144 KB |
Output is correct |
67 |
Correct |
87 ms |
11096 KB |
Output is correct |
68 |
Correct |
42 ms |
11096 KB |
Output is correct |
69 |
Correct |
40 ms |
11160 KB |
Output is correct |
70 |
Correct |
32 ms |
11096 KB |
Output is correct |
71 |
Correct |
37 ms |
11096 KB |
Output is correct |
72 |
Correct |
27 ms |
11168 KB |
Output is correct |
73 |
Correct |
28 ms |
11096 KB |
Output is correct |
74 |
Correct |
55 ms |
11160 KB |
Output is correct |
75 |
Correct |
53 ms |
11420 KB |
Output is correct |
76 |
Correct |
44 ms |
11148 KB |
Output is correct |
77 |
Correct |
45 ms |
11100 KB |
Output is correct |
78 |
Correct |
72 ms |
11096 KB |
Output is correct |
79 |
Correct |
37 ms |
11100 KB |
Output is correct |
80 |
Correct |
44 ms |
11100 KB |
Output is correct |
81 |
Correct |
52 ms |
11100 KB |
Output is correct |
82 |
Correct |
45 ms |
11148 KB |
Output is correct |
83 |
Correct |
58 ms |
11100 KB |
Output is correct |
84 |
Correct |
52 ms |
11156 KB |
Output is correct |
85 |
Correct |
63 ms |
11100 KB |
Output is correct |
86 |
Correct |
53 ms |
11100 KB |
Output is correct |
87 |
Correct |
60 ms |
11096 KB |
Output is correct |
88 |
Correct |
62 ms |
11100 KB |
Output is correct |
89 |
Correct |
74 ms |
11096 KB |
Output is correct |
90 |
Correct |
63 ms |
11100 KB |
Output is correct |
91 |
Correct |
57 ms |
11144 KB |
Output is correct |
92 |
Correct |
63 ms |
11100 KB |
Output is correct |