#include <iostream>
#include <cstdio>
#include <vector>
#include <utility>
#include <cmath>
#include <algorithm>
#include <array>
#include <set>
#include <climits>
#include <map>
#include <memory>
#include <string>
#include <deque>
using namespace std;
#define input "mecho.in"
#define output "mecho.out"
int n, s;
int ans=-1; //will be maxed during the binary search
vector<vector<int>> bees_reach;
deque<pair<int, int>> bees_bfs; //r, c start off while taking input
vector<string> a;//input field
vector<vector<int>> reach_copy;
deque<pair<int, int>> bfs_copy;
vector<pair<int, int>> dirs = {
{1, 0},
{-1, 0},
{0, 1},
{0, -1}
};
bool check(int m) {
vector<vector<int>> mecho_reach = reach_copy;
deque<pair<int, int>> mecho_bfs = bfs_copy;
//do bfs with mecho's location except mech can never go to a tile with bees there less than or equal to (m*s)+steps
while (!mecho_bfs.empty()) {
pair<int, int> curr=mecho_bfs.front();
mecho_bfs.pop_front();
int r=curr.first, c=curr.second;
int curr_steps=mecho_reach[r][c];
//cout << "r " << r << " c " << c << " curr_steps " << curr_steps << endl;
if (a[r][c]=='D') {
return 1;
}
for (pair<int, int> dir: dirs) {
int new_r=r+dir.first, new_c=c+dir.second;
if (new_r<0 || new_r>=n || new_c<0 || new_c>=n || a[new_r][new_c]=='T' || mecho_reach[new_r][new_c]!=-1) continue;
if (bees_reach[new_r][new_c]<=(m*s)+curr_steps+1 && bees_reach[new_r][new_c]!=-1) continue;
mecho_reach[new_r][new_c]=curr_steps+1;
mecho_bfs.push_back({new_r, new_c});
}
}
return 0;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
//freopen(input, "r", stdin);
//freopen(output, "w", stdout);
cin >> n >> s;
bees_reach.resize(n, vector<int>(n, -1));
reach_copy.resize(n, vector<int>(n, -1));
a.reserve(n);
for (int r=0;r<n;r++) {
string s; cin >> s;
a.push_back(s);
for (int c=0;c<n;c++) {
if (a[r][c]=='H') {
bees_bfs.push_back({r, c});
bees_reach[r][c]=0;
}
else if (a[r][c]=='M') {
bfs_copy.push_back({r, c});
reach_copy[r][c]=0;
}
}
}
while (!bees_bfs.empty()) {
pair<int, int> curr=bees_bfs.front();
bees_bfs.pop_front();
int r=curr.first, c=curr.second; int curr_steps=bees_reach[r][c];
for (pair<int, int> dir: dirs) {
int new_r=r+dir.first, new_c=c+dir.second;
if (new_r<0 || new_r>=n || new_c<0 || new_c>=n || a[new_r][new_c]=='T' || a[new_r][new_c]=='D') continue;
if (bees_reach[new_r][new_c]==-1) {
bees_reach[new_r][new_c]=curr_steps+s;
bees_bfs.push_back({new_r, new_c});
}
}
}
int l=0, r=800*800;
while (l<=r) {
int m=(l+r)/2;
//cout << m << endl;
if (check(m)) {
ans=m;
l=m+1;
}
else {
r=m-1;
}
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
117 ms |
9336 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
324 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
5 ms |
1944 KB |
Output is correct |
34 |
Correct |
5 ms |
1996 KB |
Output is correct |
35 |
Correct |
28 ms |
2056 KB |
Output is correct |
36 |
Correct |
6 ms |
2508 KB |
Output is correct |
37 |
Correct |
6 ms |
2508 KB |
Output is correct |
38 |
Correct |
34 ms |
2612 KB |
Output is correct |
39 |
Correct |
10 ms |
3212 KB |
Output is correct |
40 |
Correct |
8 ms |
3168 KB |
Output is correct |
41 |
Correct |
44 ms |
3272 KB |
Output is correct |
42 |
Correct |
12 ms |
3896 KB |
Output is correct |
43 |
Correct |
9 ms |
3816 KB |
Output is correct |
44 |
Correct |
53 ms |
3852 KB |
Output is correct |
45 |
Correct |
15 ms |
4604 KB |
Output is correct |
46 |
Correct |
11 ms |
4556 KB |
Output is correct |
47 |
Correct |
66 ms |
4628 KB |
Output is correct |
48 |
Correct |
18 ms |
5416 KB |
Output is correct |
49 |
Correct |
12 ms |
5352 KB |
Output is correct |
50 |
Correct |
78 ms |
5412 KB |
Output is correct |
51 |
Correct |
21 ms |
6404 KB |
Output is correct |
52 |
Correct |
15 ms |
6220 KB |
Output is correct |
53 |
Correct |
101 ms |
6348 KB |
Output is correct |
54 |
Correct |
26 ms |
7148 KB |
Output is correct |
55 |
Correct |
17 ms |
7116 KB |
Output is correct |
56 |
Correct |
116 ms |
7184 KB |
Output is correct |
57 |
Correct |
29 ms |
8196 KB |
Output is correct |
58 |
Correct |
20 ms |
8148 KB |
Output is correct |
59 |
Correct |
138 ms |
8248 KB |
Output is correct |
60 |
Correct |
21 ms |
9164 KB |
Output is correct |
61 |
Correct |
20 ms |
9184 KB |
Output is correct |
62 |
Correct |
153 ms |
9264 KB |
Output is correct |
63 |
Correct |
134 ms |
9320 KB |
Output is correct |
64 |
Correct |
254 ms |
9324 KB |
Output is correct |
65 |
Correct |
185 ms |
9252 KB |
Output is correct |
66 |
Correct |
158 ms |
9368 KB |
Output is correct |
67 |
Correct |
148 ms |
9236 KB |
Output is correct |
68 |
Correct |
77 ms |
9272 KB |
Output is correct |
69 |
Correct |
72 ms |
9288 KB |
Output is correct |
70 |
Correct |
56 ms |
9276 KB |
Output is correct |
71 |
Correct |
65 ms |
9276 KB |
Output is correct |
72 |
Incorrect |
49 ms |
9304 KB |
Output isn't correct |
73 |
Incorrect |
63 ms |
9288 KB |
Output isn't correct |
74 |
Correct |
86 ms |
9312 KB |
Output is correct |
75 |
Correct |
104 ms |
9280 KB |
Output is correct |
76 |
Correct |
90 ms |
9292 KB |
Output is correct |
77 |
Correct |
90 ms |
9288 KB |
Output is correct |
78 |
Correct |
105 ms |
9304 KB |
Output is correct |
79 |
Correct |
73 ms |
9304 KB |
Output is correct |
80 |
Correct |
85 ms |
9296 KB |
Output is correct |
81 |
Correct |
95 ms |
9292 KB |
Output is correct |
82 |
Correct |
86 ms |
9280 KB |
Output is correct |
83 |
Correct |
107 ms |
9292 KB |
Output is correct |
84 |
Correct |
97 ms |
9288 KB |
Output is correct |
85 |
Correct |
108 ms |
9288 KB |
Output is correct |
86 |
Correct |
105 ms |
9292 KB |
Output is correct |
87 |
Correct |
107 ms |
9300 KB |
Output is correct |
88 |
Correct |
112 ms |
9292 KB |
Output is correct |
89 |
Correct |
113 ms |
9264 KB |
Output is correct |
90 |
Correct |
115 ms |
9300 KB |
Output is correct |
91 |
Correct |
112 ms |
9292 KB |
Output is correct |
92 |
Correct |
121 ms |
9292 KB |
Output is correct |