#include <bits/stdc++.h>
/*--------------------------------------------------------------------*/
#define INF 3*100*1000 + 5
#define MAX 1000000
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define intt int64_t
#define piic pair<int64_t, int64_t>
typedef long long ll;
using namespace std;
/*--------------------------------------------------------------------*/
int n, s;
pii b, home;
queue<pii> hives;
vector<string> grid;
int filltime[900][900]{INT_MAX}, beetime[900][900], visited[900][900];
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
int inside(int x, int y){
return (x> -1 && x < n && y > -1 && y < n && grid[x][y] != 'T');
}
int main(int argc, const char * argv[]) {
ios::sync_with_stdio(0); cin.tie(0);
fill(filltime[0], filltime[0] + 900*900, INT_MAX);
if(2 >= INT_MAX) cout << "Error\n";
cin >> n >> s;
for(int i = 0; i < n; ++i){
string str;
cin >> str;
grid.pb(str);
for(int j = 0; j < (int)str.length(); ++j){
if (str[j] == 'M') b = make_pair(i, j);
if (str[j] == 'D') home = make_pair(i, j);
if(str[j] == 'H'){
hives.push(make_pair(i, j));
filltime[i][j] = 0;
}
}
}
while(!hives.empty()){
pii bee = hives.front();
hives.pop();
int x,y;
x = bee.fi; y = bee.se;
for(int i = 0; i < 4; ++i){
if(!inside(x + dx[i], y + dy[i]) || grid[x+dx[i]][y+dy[i]] == 'D') continue;
if(filltime[x+dx[i]][y+dy[i]] != INT_MAX)
filltime[x+dx[i]][y+dy[i]] = min(filltime[x+dx[i]][y+dy[i]], filltime[x][y] + 1);
else {
filltime[x+dx[i]][y+dy[i]] = filltime[x][y] + 1;
hives.push(make_pair(x + dx[i], y + dy[i]));
}
}
}
int lo = 0, hi = filltime[b.fi][b.se];
int ans = 0;
bool realpos = false;
while(lo < hi){
queue<tuple<int, int, int, int>> q;
int tempans = lo + (hi-lo)/2;
fill(visited[0], visited[0] + 900*900, 0);
visited[b.fi][b.se] = 1;
q.push(make_tuple(b.fi, b.se, tempans, 0));
bool localpos = false;
// cout << "New Round " << tempans << '\n';
while(!q.empty()){
tuple<int, int, int, int> cur = q.front();
q.pop();
int x = get<0>(cur);
int y = get<1> (cur);
int a = get<2> (cur);
int res = get<3> (cur);
//cout << x << ' ' << y << ' ' << a << ' ' << res << ' ' << q.size() << ' ' << filltime[x][y] << '\n';
if(a + (double)res/s >= filltime[x][y]) continue;
if(x == home.fi && y == home.se) {
realpos = true;
localpos = true;
// cout << "Success!\n";
break;
}
int x1, y1;
if(res == s-1) {
y1 = 0;
x1 = a + 1;
} else {
x1 = a;
y1 = res + 1;
}
for(int i = 0; i < 4; ++i) {
if(!inside(x+dx[i],y+dy[i])) continue;
if(visited[x+dx[i]][y+dy[i]]) continue;
visited[x+dx[i]][y+dy[i]] = 1;
q.push(make_tuple(x+dx[i], y+dy[i], x1, y1));
}
}
// cout << pos << '\n';
if(localpos) {
ans = max(ans, tempans);
lo = tempans + 1;
} else {
hi = tempans;
}
}
if(realpos) cout << ans << '\n';
else cout << -1 << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6612 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
4 ms |
6612 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
4 ms |
6612 KB |
Output is correct |
6 |
Correct |
4 ms |
6612 KB |
Output is correct |
7 |
Correct |
75 ms |
7460 KB |
Output is correct |
8 |
Correct |
4 ms |
6612 KB |
Output is correct |
9 |
Correct |
4 ms |
6612 KB |
Output is correct |
10 |
Correct |
4 ms |
6612 KB |
Output is correct |
11 |
Correct |
3 ms |
6612 KB |
Output is correct |
12 |
Correct |
3 ms |
6612 KB |
Output is correct |
13 |
Correct |
3 ms |
6612 KB |
Output is correct |
14 |
Correct |
4 ms |
6612 KB |
Output is correct |
15 |
Correct |
4 ms |
6612 KB |
Output is correct |
16 |
Correct |
4 ms |
6612 KB |
Output is correct |
17 |
Correct |
4 ms |
6612 KB |
Output is correct |
18 |
Correct |
4 ms |
6612 KB |
Output is correct |
19 |
Correct |
5 ms |
6612 KB |
Output is correct |
20 |
Correct |
4 ms |
6612 KB |
Output is correct |
21 |
Correct |
4 ms |
6612 KB |
Output is correct |
22 |
Correct |
4 ms |
6612 KB |
Output is correct |
23 |
Correct |
4 ms |
6616 KB |
Output is correct |
24 |
Correct |
4 ms |
6612 KB |
Output is correct |
25 |
Correct |
4 ms |
6612 KB |
Output is correct |
26 |
Correct |
5 ms |
6612 KB |
Output is correct |
27 |
Correct |
4 ms |
6612 KB |
Output is correct |
28 |
Correct |
5 ms |
6612 KB |
Output is correct |
29 |
Correct |
4 ms |
6612 KB |
Output is correct |
30 |
Correct |
5 ms |
6612 KB |
Output is correct |
31 |
Correct |
4 ms |
6612 KB |
Output is correct |
32 |
Correct |
5 ms |
6612 KB |
Output is correct |
33 |
Correct |
7 ms |
6740 KB |
Output is correct |
34 |
Correct |
6 ms |
6740 KB |
Output is correct |
35 |
Correct |
17 ms |
6820 KB |
Output is correct |
36 |
Correct |
8 ms |
6740 KB |
Output is correct |
37 |
Correct |
7 ms |
6740 KB |
Output is correct |
38 |
Correct |
25 ms |
6868 KB |
Output is correct |
39 |
Correct |
8 ms |
6804 KB |
Output is correct |
40 |
Correct |
7 ms |
6856 KB |
Output is correct |
41 |
Correct |
28 ms |
6916 KB |
Output is correct |
42 |
Correct |
8 ms |
6868 KB |
Output is correct |
43 |
Correct |
8 ms |
6852 KB |
Output is correct |
44 |
Correct |
35 ms |
6952 KB |
Output is correct |
45 |
Correct |
9 ms |
6996 KB |
Output is correct |
46 |
Correct |
8 ms |
6996 KB |
Output is correct |
47 |
Correct |
42 ms |
6996 KB |
Output is correct |
48 |
Correct |
9 ms |
6996 KB |
Output is correct |
49 |
Correct |
10 ms |
6984 KB |
Output is correct |
50 |
Correct |
54 ms |
7072 KB |
Output is correct |
51 |
Correct |
11 ms |
7092 KB |
Output is correct |
52 |
Correct |
10 ms |
7124 KB |
Output is correct |
53 |
Correct |
57 ms |
7144 KB |
Output is correct |
54 |
Correct |
12 ms |
7124 KB |
Output is correct |
55 |
Correct |
12 ms |
7124 KB |
Output is correct |
56 |
Correct |
65 ms |
7120 KB |
Output is correct |
57 |
Correct |
13 ms |
7192 KB |
Output is correct |
58 |
Correct |
11 ms |
7252 KB |
Output is correct |
59 |
Correct |
74 ms |
7192 KB |
Output is correct |
60 |
Correct |
13 ms |
7260 KB |
Output is correct |
61 |
Correct |
13 ms |
7252 KB |
Output is correct |
62 |
Correct |
93 ms |
7376 KB |
Output is correct |
63 |
Correct |
88 ms |
7352 KB |
Output is correct |
64 |
Correct |
147 ms |
7252 KB |
Output is correct |
65 |
Correct |
140 ms |
7352 KB |
Output is correct |
66 |
Correct |
122 ms |
7388 KB |
Output is correct |
67 |
Correct |
108 ms |
7252 KB |
Output is correct |
68 |
Correct |
43 ms |
7296 KB |
Output is correct |
69 |
Correct |
32 ms |
7380 KB |
Output is correct |
70 |
Correct |
30 ms |
7292 KB |
Output is correct |
71 |
Correct |
33 ms |
7288 KB |
Output is correct |
72 |
Correct |
27 ms |
7380 KB |
Output is correct |
73 |
Correct |
29 ms |
7536 KB |
Output is correct |
74 |
Correct |
53 ms |
7728 KB |
Output is correct |
75 |
Correct |
54 ms |
7548 KB |
Output is correct |
76 |
Correct |
51 ms |
7628 KB |
Output is correct |
77 |
Correct |
52 ms |
7632 KB |
Output is correct |
78 |
Correct |
61 ms |
7600 KB |
Output is correct |
79 |
Correct |
46 ms |
7520 KB |
Output is correct |
80 |
Correct |
50 ms |
7516 KB |
Output is correct |
81 |
Correct |
58 ms |
7592 KB |
Output is correct |
82 |
Correct |
49 ms |
7508 KB |
Output is correct |
83 |
Correct |
72 ms |
7680 KB |
Output is correct |
84 |
Correct |
58 ms |
7476 KB |
Output is correct |
85 |
Correct |
75 ms |
7508 KB |
Output is correct |
86 |
Correct |
65 ms |
7508 KB |
Output is correct |
87 |
Correct |
65 ms |
7568 KB |
Output is correct |
88 |
Correct |
69 ms |
7500 KB |
Output is correct |
89 |
Correct |
66 ms |
7500 KB |
Output is correct |
90 |
Correct |
72 ms |
7508 KB |
Output is correct |
91 |
Correct |
63 ms |
7484 KB |
Output is correct |
92 |
Correct |
68 ms |
7428 KB |
Output is correct |