#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];
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> x = bear.front();
bear.pop();
if(ceil((float)bear_dist[x.first][x.second]/(float)s) +eat >= bee_dist[x.first][x.second])continue;
if(x.first < n-1 && mp[x.first+1][x.second]=='D')return true;
if(x.first > 0 && mp[x.first-1][x.second]=='D')return true;
if(x.second < n-1 && mp[x.first][x.second+1]=='D')return true;
if(x.second > 0 && mp[x.first][x.second-1]=='D')return true;
if(x.first < n-1 && (mp[x.first+1][x.second]!='T' || mp[x.first+1][x.second]!='H') && bear_dist[x.first+1][x.second]==-1){
bear_dist[x.first+1][x.second]=bear_dist[x.first][x.second]+1;
bear.push({x.first+1, x.second});
}
if(x.first > 0 && (mp[x.first-1][x.second]!='T' || mp[x.first-1][x.second]!='H') && bear_dist[x.first-1][x.second]==-1){
bear_dist[x.first-1][x.second]=bear_dist[x.first][x.second]+1;
bear.push({x.first-1, x.second});
}
if(x.second < n-1 && (mp[x.first][x.second+1]!='T' || mp[x.first][x.second+1]!='H') && bear_dist[x.first][x.second+1]==-1){
bear_dist[x.first][x.second+1]=bear_dist[x.first][x.second]+1;
bear.push({x.first, x.second+1});
}
if(x.second > 0 && (mp[x.first][x.second-1]!='T' || mp[x.first][x.second-1]!='H') && bear_dist[x.first][x.second-1]==-1){
bear_dist[x.first][x.second-1]=bear_dist[x.first][x.second]+1;
bear.push({x.first, x.second-1});
}
}
return false;
}
signed main(){
cin >> n >> s;
mp.resize(n, vector<char>(n));
bee_dist.resize(n, vector<int>(n, INF));
queue<pair<int, int>> bees;
for(int i = 0; i < n; i++){
for(int j = 0; 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> x = bees.front();
bees.pop();
if(x.first < n-1 && (mp[x.first+1][x.second]!='T' || mp[x.first+1][x.second]!='D' || mp[x.first+1][x.second]!='H') && bee_dist[x.first+1][x.second]==INF){
bee_dist[x.first+1][x.second]=bee_dist[x.first][x.second]+1;
bees.push({x.first+1, x.second});
}
if(x.first > 0 && (mp[x.first-1][x.second]!='T' || mp[x.first-1][x.second]!='D' || mp[x.first-1][x.second]!='H') && bee_dist[x.first-1][x.second]==INF){
bee_dist[x.first-1][x.second]=bee_dist[x.first][x.second]+1;
bees.push({x.first-1, x.second});
}
if(x.second < n-1 && (mp[x.first][x.second+1]!='T' || mp[x.first][x.second+1]!='D' || mp[x.first][x.second+1]!='H') && bee_dist[x.first][x.second+1]==INF){
bee_dist[x.first][x.second+1]=bee_dist[x.first][x.second]+1;
bees.push({x.first, x.second+1});
}
if(x.second > 0 && (mp[x.first][x.second-1]!='T' || mp[x.first][x.second-1]!='D' || mp[x.first][x.second-1]!='H') && bee_dist[x.first][x.second-1]==INF){
bee_dist[x.first][x.second-1]=bee_dist[x.first][x.second]+1;
bees.push({x.first, x.second-1});
}
}
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 |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
1 ms |
2908 KB |
Output is correct |
4 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
7 |
Incorrect |
60 ms |
6888 KB |
Output isn't correct |
8 |
Correct |
1 ms |
2904 KB |
Output is correct |
9 |
Incorrect |
1 ms |
2904 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
13 |
Correct |
1 ms |
2908 KB |
Output is correct |
14 |
Correct |
2 ms |
2908 KB |
Output is correct |
15 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
17 |
Incorrect |
2 ms |
2908 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
2980 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
2904 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
2744 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
28 |
Incorrect |
2 ms |
2908 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
2908 KB |
Output isn't correct |
30 |
Incorrect |
2 ms |
2908 KB |
Output isn't correct |
31 |
Incorrect |
2 ms |
2908 KB |
Output isn't correct |
32 |
Incorrect |
2 ms |
2908 KB |
Output isn't correct |
33 |
Incorrect |
6 ms |
3676 KB |
Output isn't correct |
34 |
Incorrect |
10 ms |
3488 KB |
Output isn't correct |
35 |
Incorrect |
19 ms |
3528 KB |
Output isn't correct |
36 |
Incorrect |
8 ms |
3932 KB |
Output isn't correct |
37 |
Incorrect |
11 ms |
3928 KB |
Output isn't correct |
38 |
Incorrect |
32 ms |
3928 KB |
Output isn't correct |
39 |
Incorrect |
11 ms |
4132 KB |
Output isn't correct |
40 |
Incorrect |
16 ms |
3968 KB |
Output isn't correct |
41 |
Incorrect |
32 ms |
4132 KB |
Output isn't correct |
42 |
Incorrect |
12 ms |
4440 KB |
Output isn't correct |
43 |
Incorrect |
13 ms |
4284 KB |
Output isn't correct |
44 |
Incorrect |
46 ms |
4444 KB |
Output isn't correct |
45 |
Incorrect |
15 ms |
4696 KB |
Output isn't correct |
46 |
Incorrect |
32 ms |
4700 KB |
Output isn't correct |
47 |
Incorrect |
60 ms |
4548 KB |
Output isn't correct |
48 |
Incorrect |
16 ms |
4920 KB |
Output isn't correct |
49 |
Incorrect |
24 ms |
4952 KB |
Output isn't correct |
50 |
Incorrect |
51 ms |
5116 KB |
Output isn't correct |
51 |
Incorrect |
19 ms |
5460 KB |
Output isn't correct |
52 |
Incorrect |
28 ms |
5476 KB |
Output isn't correct |
53 |
Incorrect |
69 ms |
5468 KB |
Output isn't correct |
54 |
Incorrect |
21 ms |
5724 KB |
Output isn't correct |
55 |
Incorrect |
34 ms |
5684 KB |
Output isn't correct |
56 |
Incorrect |
71 ms |
5684 KB |
Output isn't correct |
57 |
Incorrect |
22 ms |
6224 KB |
Output isn't correct |
58 |
Incorrect |
39 ms |
6236 KB |
Output isn't correct |
59 |
Incorrect |
91 ms |
6236 KB |
Output isn't correct |
60 |
Incorrect |
26 ms |
6744 KB |
Output isn't correct |
61 |
Incorrect |
42 ms |
6748 KB |
Output isn't correct |
62 |
Incorrect |
103 ms |
6740 KB |
Output isn't correct |
63 |
Incorrect |
24 ms |
6748 KB |
Output isn't correct |
64 |
Incorrect |
27 ms |
6548 KB |
Output isn't correct |
65 |
Incorrect |
33 ms |
6748 KB |
Output isn't correct |
66 |
Incorrect |
24 ms |
6736 KB |
Output isn't correct |
67 |
Incorrect |
24 ms |
6744 KB |
Output isn't correct |
68 |
Incorrect |
29 ms |
6740 KB |
Output isn't correct |
69 |
Incorrect |
32 ms |
6736 KB |
Output isn't correct |
70 |
Incorrect |
30 ms |
6720 KB |
Output isn't correct |
71 |
Incorrect |
34 ms |
6748 KB |
Output isn't correct |
72 |
Incorrect |
31 ms |
6928 KB |
Output isn't correct |
73 |
Correct |
32 ms |
6992 KB |
Output is correct |
74 |
Incorrect |
46 ms |
6996 KB |
Output isn't correct |
75 |
Incorrect |
51 ms |
6996 KB |
Output isn't correct |
76 |
Incorrect |
47 ms |
7004 KB |
Output isn't correct |
77 |
Incorrect |
46 ms |
6996 KB |
Output isn't correct |
78 |
Correct |
56 ms |
6820 KB |
Output is correct |
79 |
Incorrect |
43 ms |
6996 KB |
Output isn't correct |
80 |
Incorrect |
46 ms |
6992 KB |
Output isn't correct |
81 |
Incorrect |
52 ms |
7028 KB |
Output isn't correct |
82 |
Incorrect |
43 ms |
7020 KB |
Output isn't correct |
83 |
Incorrect |
51 ms |
6748 KB |
Output isn't correct |
84 |
Incorrect |
50 ms |
6740 KB |
Output isn't correct |
85 |
Incorrect |
54 ms |
6740 KB |
Output isn't correct |
86 |
Incorrect |
50 ms |
7004 KB |
Output isn't correct |
87 |
Incorrect |
48 ms |
7000 KB |
Output isn't correct |
88 |
Incorrect |
53 ms |
6740 KB |
Output isn't correct |
89 |
Incorrect |
52 ms |
6736 KB |
Output isn't correct |
90 |
Incorrect |
57 ms |
6740 KB |
Output isn't correct |
91 |
Incorrect |
57 ms |
6816 KB |
Output isn't correct |
92 |
Incorrect |
56 ms |
6740 KB |
Output isn't correct |