#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int n, s;
vector<vector<char>> mp;
pair<int, int> m;
vector<vector<int>> bee_dist;
int bear_dist[805][805];
vector<pair<int, int>> allowed_moves{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool check(int eat){
queue<pair<int, int>> bear;
memset(bear_dist, -1, sizeof(bear_dist));
bear_dist[m.first][m.second] = 0;
bear.push(m);
while(!bear.empty()){
pair<int, int> pos = bear.front();
bear.pop();
if(bear_dist[pos.first][pos.second]/s + eat >= bee_dist[pos.first][pos.second])continue;
for(pair<int, int> u : allowed_moves){
int x = pos.first + u.first, y = pos.second + u.second;
if(x > 0 && x < n+1 && y > 0 && y < n+1 && mp[x][y]=='D')return true;
if(bear_dist[x][y]==-1 && x > 0 && x < n+1 && y > 0 && y < n+1 && mp[x][y] != 'D' && mp[x][y] != 'T'){
bear_dist[x][y]=bear_dist[pos.first][pos.second] + 1;
bear.push({x, y});
}
}
}
return false;
}
signed main(){
cin >> n >> s;
mp.resize(n+2, vector<char>(n+2));
bee_dist.resize(n+2, vector<int>(n+2, INF));
queue<pair<int, int>> bees;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cin >> mp[i][j];
if(mp[i][j]=='M')m={i, j};
else if(mp[i][j]=='H'){
bees.push({i, j});
bee_dist[i][j]=0;
}
}
}
while(!bees.empty()){
pair<int, int> pos = bees.front();
bees.pop();
for(pair<int, int> u : allowed_moves){
int x = pos.first+u.first, y = pos.second+u.second;
if(bee_dist[x][y]==INF && x > 0 && x < n+1 && y > 0 && y < n+1 && mp[x][y]!= 'D' && mp[x][y] != 'T'){
bee_dist[x][y] = bee_dist[pos.first][pos.second]+1;
bees.push({x, y});
}
}
}
int l = -1, r = n*n;
while(l<r){
int mid = (l+r+1)>>1;
if(check(mid))l=mid;
else r=mid-1;
}
cout << l << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2904 KB |
Output is correct |
2 |
Correct |
2 ms |
2904 KB |
Output is correct |
3 |
Correct |
1 ms |
2904 KB |
Output is correct |
4 |
Correct |
1 ms |
2908 KB |
Output is correct |
5 |
Correct |
1 ms |
2908 KB |
Output is correct |
6 |
Correct |
2 ms |
2908 KB |
Output is correct |
7 |
Correct |
54 ms |
6748 KB |
Output is correct |
8 |
Correct |
2 ms |
2904 KB |
Output is correct |
9 |
Correct |
1 ms |
2748 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
1 ms |
2916 KB |
Output is correct |
12 |
Correct |
2 ms |
2908 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
3000 KB |
Output is correct |
15 |
Correct |
1 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
2908 KB |
Output is correct |
17 |
Correct |
1 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2908 KB |
Output is correct |
19 |
Correct |
2 ms |
2908 KB |
Output is correct |
20 |
Correct |
1 ms |
2908 KB |
Output is correct |
21 |
Correct |
2 ms |
2908 KB |
Output is correct |
22 |
Correct |
2 ms |
3160 KB |
Output is correct |
23 |
Correct |
1 ms |
3000 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
2 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2908 KB |
Output is correct |
28 |
Correct |
1 ms |
2908 KB |
Output is correct |
29 |
Correct |
2 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
5 ms |
3504 KB |
Output is correct |
34 |
Correct |
5 ms |
3676 KB |
Output is correct |
35 |
Correct |
15 ms |
3676 KB |
Output is correct |
36 |
Correct |
8 ms |
3932 KB |
Output is correct |
37 |
Correct |
8 ms |
3932 KB |
Output is correct |
38 |
Correct |
18 ms |
3952 KB |
Output is correct |
39 |
Correct |
9 ms |
4188 KB |
Output is correct |
40 |
Correct |
9 ms |
4148 KB |
Output is correct |
41 |
Correct |
25 ms |
4188 KB |
Output is correct |
42 |
Correct |
9 ms |
4444 KB |
Output is correct |
43 |
Correct |
10 ms |
4444 KB |
Output is correct |
44 |
Correct |
34 ms |
4444 KB |
Output is correct |
45 |
Correct |
11 ms |
4696 KB |
Output is correct |
46 |
Correct |
11 ms |
4804 KB |
Output is correct |
47 |
Correct |
34 ms |
4700 KB |
Output is correct |
48 |
Correct |
17 ms |
4956 KB |
Output is correct |
49 |
Correct |
17 ms |
5060 KB |
Output is correct |
50 |
Correct |
40 ms |
4956 KB |
Output is correct |
51 |
Correct |
16 ms |
5464 KB |
Output is correct |
52 |
Correct |
15 ms |
5468 KB |
Output is correct |
53 |
Correct |
47 ms |
5500 KB |
Output is correct |
54 |
Correct |
18 ms |
5832 KB |
Output is correct |
55 |
Correct |
16 ms |
5828 KB |
Output is correct |
56 |
Correct |
56 ms |
5724 KB |
Output is correct |
57 |
Correct |
19 ms |
6188 KB |
Output is correct |
58 |
Correct |
20 ms |
6236 KB |
Output is correct |
59 |
Correct |
66 ms |
6124 KB |
Output is correct |
60 |
Correct |
22 ms |
6740 KB |
Output is correct |
61 |
Correct |
25 ms |
6748 KB |
Output is correct |
62 |
Correct |
73 ms |
6744 KB |
Output is correct |
63 |
Correct |
73 ms |
6736 KB |
Output is correct |
64 |
Correct |
125 ms |
6744 KB |
Output is correct |
65 |
Correct |
103 ms |
6780 KB |
Output is correct |
66 |
Correct |
83 ms |
6740 KB |
Output is correct |
67 |
Correct |
77 ms |
6736 KB |
Output is correct |
68 |
Correct |
40 ms |
6736 KB |
Output is correct |
69 |
Correct |
41 ms |
6692 KB |
Output is correct |
70 |
Correct |
32 ms |
6748 KB |
Output is correct |
71 |
Correct |
40 ms |
6740 KB |
Output is correct |
72 |
Correct |
32 ms |
6736 KB |
Output is correct |
73 |
Correct |
30 ms |
6996 KB |
Output is correct |
74 |
Correct |
46 ms |
6996 KB |
Output is correct |
75 |
Correct |
51 ms |
7004 KB |
Output is correct |
76 |
Correct |
43 ms |
7060 KB |
Output is correct |
77 |
Correct |
44 ms |
7000 KB |
Output is correct |
78 |
Correct |
59 ms |
7004 KB |
Output is correct |
79 |
Correct |
40 ms |
6984 KB |
Output is correct |
80 |
Correct |
44 ms |
7008 KB |
Output is correct |
81 |
Correct |
48 ms |
6840 KB |
Output is correct |
82 |
Correct |
49 ms |
6992 KB |
Output is correct |
83 |
Correct |
53 ms |
6992 KB |
Output is correct |
84 |
Correct |
47 ms |
6768 KB |
Output is correct |
85 |
Correct |
57 ms |
6992 KB |
Output is correct |
86 |
Correct |
53 ms |
7004 KB |
Output is correct |
87 |
Correct |
51 ms |
7004 KB |
Output is correct |
88 |
Correct |
52 ms |
6940 KB |
Output is correct |
89 |
Correct |
61 ms |
6740 KB |
Output is correct |
90 |
Correct |
57 ms |
6740 KB |
Output is correct |
91 |
Correct |
55 ms |
6744 KB |
Output is correct |
92 |
Correct |
59 ms |
6940 KB |
Output is correct |