#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define FOR(i, x, y) for(ll i=x; i<y; i++)
#define FORNEG(i, x, y) for(ll i=x; i>y; i--)
#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL)
ll grid[801][801];
ll visited[801][801];
ll n,s;
vector<vector<ll>> hives;
vector<ll> start;
vector<ll> endd;
vector<vector<ll>> ops = {{0,1}, {0,-1}, {1,0}, {-1,0}};
void bfsinit(){
queue<vector<ll>> d;
for (auto&i : hives){
d.push({i[0], i[1], 0});
}
while (d.size()){
vector<ll> node = d.front();;
d.pop();
for (auto&i : ops){
ll a = node[0] + i[0];
ll b = node[1] + i[1];
if (0<=a && a<n && 0<=b && b<n){
if (grid[a][b] == -3){
d.push({a,b,node[2]+1});
grid[a][b] = node[2]+1;
}
}
}
}
}
bool test(ll time){
queue<vector<ll>> d;
if (grid[start[0]][start[1]] <= time){
return false;
}
FOR(i,0,n){
FOR(j,0,n){
visited[i][j] = false;
}
}
d.push({start[0], start[1], 0});
while (d.size()){
vector<ll> node = d.front();
d.pop();
if (node[0]==endd[0] && node[1] == endd[1]){
return true;
}
for (auto&i : ops){
ll a = node[0]+i[0];
ll b = node[1] + i[1];
if (0<=a && a<n && 0<=b && b<n && !(visited[a][b])){
if (grid[a][b] == -2) continue;
if (grid[a][b] == -1 || grid[a][b] > time+(node[2]+1)/s){
d.push({a,b,node[2]+1});
visited[a][b] = true;
}
}
}
}
return false;
}
int main(){
fast();
cin >> n >> s;
FOR(i,0,n){
string sus;
cin >> sus;
FOR(j,0,n){
grid[i][j] = sus[j];
}
}
FOR(i,0,n){
FOR(j,0,n){
if (grid[i][j] == 'T') grid[i][j] = -2;
else if (grid[i][j] == 'D'){
grid[i][j] = -1;
endd = {i,j};
}
else if (grid[i][j] == 'H'){
grid[i][j] = 0;
hives.push_back({i,j});
}
else if (grid[i][j] == 'M'){
start = {i,j};
grid[i][j] = -3;
}
else{
grid[i][j] = -3;
}
}
}
bfsinit();
ll lo = -1;
ll hi = n*n;
while (lo<hi){
ll mid = lo+(hi-lo+1)/2;
if (test(mid)) lo = mid;
else hi = mid-1;
}
cout << lo;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
328 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
270 ms |
11880 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
456 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
580 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
580 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
708 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
26 |
Correct |
1 ms |
852 KB |
Output is correct |
27 |
Correct |
1 ms |
852 KB |
Output is correct |
28 |
Correct |
1 ms |
852 KB |
Output is correct |
29 |
Correct |
1 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
1 ms |
980 KB |
Output is correct |
32 |
Correct |
2 ms |
980 KB |
Output is correct |
33 |
Correct |
7 ms |
4820 KB |
Output is correct |
34 |
Correct |
9 ms |
4828 KB |
Output is correct |
35 |
Correct |
48 ms |
4788 KB |
Output is correct |
36 |
Correct |
8 ms |
5460 KB |
Output is correct |
37 |
Correct |
10 ms |
5460 KB |
Output is correct |
38 |
Correct |
69 ms |
5492 KB |
Output is correct |
39 |
Correct |
10 ms |
6308 KB |
Output is correct |
40 |
Correct |
16 ms |
6100 KB |
Output is correct |
41 |
Correct |
113 ms |
6208 KB |
Output is correct |
42 |
Correct |
12 ms |
6816 KB |
Output is correct |
43 |
Correct |
14 ms |
6740 KB |
Output is correct |
44 |
Correct |
113 ms |
6832 KB |
Output is correct |
45 |
Correct |
14 ms |
7476 KB |
Output is correct |
46 |
Correct |
21 ms |
7376 KB |
Output is correct |
47 |
Correct |
154 ms |
7504 KB |
Output is correct |
48 |
Correct |
17 ms |
8068 KB |
Output is correct |
49 |
Correct |
19 ms |
8008 KB |
Output is correct |
50 |
Correct |
147 ms |
8248 KB |
Output is correct |
51 |
Correct |
18 ms |
8788 KB |
Output is correct |
52 |
Correct |
23 ms |
8640 KB |
Output is correct |
53 |
Correct |
233 ms |
8944 KB |
Output is correct |
54 |
Correct |
24 ms |
9352 KB |
Output is correct |
55 |
Correct |
25 ms |
9348 KB |
Output is correct |
56 |
Correct |
266 ms |
9720 KB |
Output is correct |
57 |
Correct |
27 ms |
10164 KB |
Output is correct |
58 |
Correct |
41 ms |
10040 KB |
Output is correct |
59 |
Correct |
269 ms |
10368 KB |
Output is correct |
60 |
Correct |
31 ms |
10896 KB |
Output is correct |
61 |
Correct |
34 ms |
10976 KB |
Output is correct |
62 |
Correct |
379 ms |
11056 KB |
Output is correct |
63 |
Correct |
198 ms |
11028 KB |
Output is correct |
64 |
Correct |
418 ms |
11024 KB |
Output is correct |
65 |
Correct |
288 ms |
11036 KB |
Output is correct |
66 |
Correct |
282 ms |
11044 KB |
Output is correct |
67 |
Correct |
218 ms |
11036 KB |
Output is correct |
68 |
Correct |
107 ms |
11200 KB |
Output is correct |
69 |
Correct |
94 ms |
11192 KB |
Output is correct |
70 |
Correct |
61 ms |
11208 KB |
Output is correct |
71 |
Correct |
76 ms |
11204 KB |
Output is correct |
72 |
Correct |
50 ms |
11200 KB |
Output is correct |
73 |
Correct |
108 ms |
13016 KB |
Output is correct |
74 |
Correct |
147 ms |
13020 KB |
Output is correct |
75 |
Correct |
181 ms |
13004 KB |
Output is correct |
76 |
Correct |
171 ms |
13012 KB |
Output is correct |
77 |
Correct |
171 ms |
13008 KB |
Output is correct |
78 |
Correct |
198 ms |
12792 KB |
Output is correct |
79 |
Correct |
134 ms |
12780 KB |
Output is correct |
80 |
Correct |
151 ms |
12788 KB |
Output is correct |
81 |
Correct |
166 ms |
12792 KB |
Output is correct |
82 |
Correct |
184 ms |
12748 KB |
Output is correct |
83 |
Correct |
207 ms |
12484 KB |
Output is correct |
84 |
Correct |
184 ms |
12552 KB |
Output is correct |
85 |
Correct |
223 ms |
12588 KB |
Output is correct |
86 |
Correct |
189 ms |
12480 KB |
Output is correct |
87 |
Correct |
204 ms |
12472 KB |
Output is correct |
88 |
Correct |
222 ms |
12144 KB |
Output is correct |
89 |
Correct |
191 ms |
12136 KB |
Output is correct |
90 |
Correct |
228 ms |
12140 KB |
Output is correct |
91 |
Correct |
223 ms |
12116 KB |
Output is correct |
92 |
Correct |
226 ms |
12148 KB |
Output is correct |