#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ll long long
struct P{
ll x, y;
};
void dbg_out() { cerr << endl; }
template <typename H, typename... T>
void dbg_out(H h, T... t) { cerr << ' ' << h; dbg_out(t...); }
#define dbg(...) { cerr << #__VA_ARGS__ << ':'; dbg_out(__VA_ARGS__); }
const int N = 801;
char grid[N][N];
int n, s;
vector<vector<int>> bdist (N, vector<int>(N, N*N+1));
pair<int, int> bear;
pair<int, int> home;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int valid(int x, int y){
return x >= 0 && y >= 0 && x < n && y < n;
}
int f(int t){
vector<vector<int>> dist (n, vector<int>(n, n*n+1));
int x, y;
queue<pair<int, int>> q;
dist[bear.first][bear.second] = 0;
q.push(bear);
while (!q.empty()){
tie(x, y) = q.front(); q.pop();
if ((bdist[x][y]-t)*s <= dist[x][y]) continue;
for (int i=0; i<4; i++){
int a = x + dx[i], b = y + dy[i];
if (!valid(a, b) || grid[a][b] == 'T' || dist[a][b] != n*n+1) continue;
dist[a][b] = dist[x][y] + 1;
q.push({a, b});
}
}
return dist[home.first][home.second] != n*n+1;
}
signed main(){
fastio;
cin >> n >> s;
vector<pair<int, int>> bees;
for (int i=0; i<n; i++){
for (int j=0; j<n; j++){
cin >> grid[i][j];
if (grid[i][j] == 'H') bees.push_back({i, j});
if (grid[i][j] == 'M') bear = {i, j};
if (grid[i][j] == 'D') home = {i, j};
}
}
int x, y;
queue<pair<int, int>> q;
for (auto b: bees){
q.push(b);
bdist[b.first][b.second] = 0;
}
while (!q.empty()){
tie(x, y) = q.front(); q.pop();
for (int i=0; i<4; i++){
int a = x + dx[i], b = y + dy[i];
if (!valid(a, b) || grid[a][b] == 'T' || grid[a][b] == 'D' || bdist[a][b] != N*N+1) continue;
bdist[a][b] = bdist[x][y] + 1;
q.push({a, b});
}
}
int l=0, r=n*n, ans=-1;
while (l <= r){
int m = l + (r-l)/2;
if (f(m)){
ans = m;
l = m+1;
}
else r = m-1;
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2908 KB |
Output is correct |
2 |
Correct |
1 ms |
2908 KB |
Output is correct |
3 |
Correct |
2 ms |
2908 KB |
Output is correct |
4 |
Correct |
1 ms |
2908 KB |
Output is correct |
5 |
Correct |
1 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
2968 KB |
Output is correct |
7 |
Correct |
53 ms |
6844 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
1 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2908 KB |
Output is correct |
13 |
Correct |
1 ms |
2908 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 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 |
1 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 |
1 ms |
3012 KB |
Output is correct |
22 |
Correct |
1 ms |
2908 KB |
Output is correct |
23 |
Correct |
1 ms |
2908 KB |
Output is correct |
24 |
Correct |
1 ms |
2936 KB |
Output is correct |
25 |
Correct |
1 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 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 |
1 ms |
2908 KB |
Output is correct |
31 |
Correct |
1 ms |
2908 KB |
Output is correct |
32 |
Correct |
1 ms |
2908 KB |
Output is correct |
33 |
Correct |
6 ms |
3876 KB |
Output is correct |
34 |
Correct |
5 ms |
3876 KB |
Output is correct |
35 |
Correct |
13 ms |
3868 KB |
Output is correct |
36 |
Correct |
6 ms |
3932 KB |
Output is correct |
37 |
Correct |
7 ms |
3992 KB |
Output is correct |
38 |
Correct |
17 ms |
4176 KB |
Output is correct |
39 |
Correct |
8 ms |
4364 KB |
Output is correct |
40 |
Correct |
8 ms |
4364 KB |
Output is correct |
41 |
Correct |
23 ms |
4428 KB |
Output is correct |
42 |
Correct |
10 ms |
4684 KB |
Output is correct |
43 |
Correct |
12 ms |
4684 KB |
Output is correct |
44 |
Correct |
29 ms |
4660 KB |
Output is correct |
45 |
Correct |
11 ms |
4952 KB |
Output is correct |
46 |
Correct |
12 ms |
4904 KB |
Output is correct |
47 |
Correct |
33 ms |
4900 KB |
Output is correct |
48 |
Correct |
13 ms |
5316 KB |
Output is correct |
49 |
Correct |
14 ms |
5252 KB |
Output is correct |
50 |
Correct |
37 ms |
5272 KB |
Output is correct |
51 |
Correct |
16 ms |
5628 KB |
Output is correct |
52 |
Correct |
16 ms |
5576 KB |
Output is correct |
53 |
Correct |
49 ms |
5544 KB |
Output is correct |
54 |
Correct |
18 ms |
6020 KB |
Output is correct |
55 |
Correct |
18 ms |
6016 KB |
Output is correct |
56 |
Correct |
55 ms |
6004 KB |
Output is correct |
57 |
Correct |
20 ms |
6444 KB |
Output is correct |
58 |
Correct |
20 ms |
6432 KB |
Output is correct |
59 |
Correct |
62 ms |
6392 KB |
Output is correct |
60 |
Correct |
24 ms |
6824 KB |
Output is correct |
61 |
Correct |
23 ms |
6836 KB |
Output is correct |
62 |
Correct |
71 ms |
6816 KB |
Output is correct |
63 |
Correct |
68 ms |
6884 KB |
Output is correct |
64 |
Correct |
118 ms |
6784 KB |
Output is correct |
65 |
Correct |
92 ms |
6828 KB |
Output is correct |
66 |
Correct |
78 ms |
6836 KB |
Output is correct |
67 |
Correct |
77 ms |
6836 KB |
Output is correct |
68 |
Correct |
39 ms |
6848 KB |
Output is correct |
69 |
Correct |
38 ms |
6872 KB |
Output is correct |
70 |
Correct |
32 ms |
6848 KB |
Output is correct |
71 |
Correct |
36 ms |
6868 KB |
Output is correct |
72 |
Correct |
29 ms |
6844 KB |
Output is correct |
73 |
Correct |
29 ms |
6892 KB |
Output is correct |
74 |
Correct |
39 ms |
6804 KB |
Output is correct |
75 |
Correct |
45 ms |
6892 KB |
Output is correct |
76 |
Correct |
43 ms |
6892 KB |
Output is correct |
77 |
Correct |
45 ms |
6852 KB |
Output is correct |
78 |
Correct |
49 ms |
6900 KB |
Output is correct |
79 |
Correct |
36 ms |
6852 KB |
Output is correct |
80 |
Correct |
41 ms |
6852 KB |
Output is correct |
81 |
Correct |
44 ms |
6888 KB |
Output is correct |
82 |
Correct |
47 ms |
6872 KB |
Output is correct |
83 |
Correct |
52 ms |
6860 KB |
Output is correct |
84 |
Correct |
47 ms |
6848 KB |
Output is correct |
85 |
Correct |
50 ms |
6880 KB |
Output is correct |
86 |
Correct |
49 ms |
6880 KB |
Output is correct |
87 |
Correct |
52 ms |
6928 KB |
Output is correct |
88 |
Correct |
54 ms |
6848 KB |
Output is correct |
89 |
Correct |
51 ms |
6812 KB |
Output is correct |
90 |
Correct |
53 ms |
6816 KB |
Output is correct |
91 |
Correct |
52 ms |
6836 KB |
Output is correct |
92 |
Correct |
55 ms |
6844 KB |
Output is correct |