#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using indexedset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, s;
cin >> n >> s;
vector<vector<char>> grid(n, vector<char>(n));
queue<pair<ll, pair<int, int>>> h;
pair<int, int> m;
pair<int, int> dest;
vector<pair<int, int>> dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
vector<vector<ll>> dist(n, vector<ll>(n, 1e9));
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cin >> grid[i][j];
if (grid[i][j] == 'H'){
h.push({0, {i, j}});
dist[i][j] = 0;
}
if (grid[i][j] == 'M'){
m = {i, j};
}
if (grid[i][j] == 'D'){
dest = {i, j};
}
}
}
while (!h.empty()){
int x = h.front().second.first;
int y = h.front().second.second;
ll d = h.front().first;
h.pop();
if (dist[x][y] != d) continue;
for (auto ne : dir){
int nx = x + ne.first;
int ny = y + ne.second;
if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
if ((grid[nx][ny] == 'G' || grid[nx][ny] == 'M') && dist[nx][ny] > (d+1)){
h.push({d+1, {nx, ny}});
dist[nx][ny] = d+1;
}
}
}
function<bool(int)> works = [&](ll t){
queue<pair<ll, pair<int, int>>> q;
vector<vector<ll>> di(n, vector<ll>(n, 1e9));
di[m.first][m.second] = 0;
q.push({0, m});
while (!q.empty()){
int x = q.front().second.first;
int y = q.front().second.second;
ll d = q.front().first;
q.pop();
if (di[x][y] != d) continue;
for (auto ne : dir){
int nx = x + ne.first;
int ny = y + ne.second;
if (nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
if ((grid[nx][ny] == 'G' || grid[nx][ny] == 'D') && di[nx][ny] > (d+1) && (d+1) < s*(-t+dist[nx][ny])){
q.push({d+1, {nx, ny}});
di[nx][ny] = d+1;
}
}
}
if (di[dest.first][dest.second] == 1e9){
return false;
}
return true;
};
ll l = -1, r = n*n;
while (l < r){
ll m = (l+r+1)/2;
if (works(m)){
l = m;
}else{
r = m-1;
}
}
cout << l << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
89 ms |
11268 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
7 ms |
2596 KB |
Output is correct |
34 |
Correct |
9 ms |
2528 KB |
Output is correct |
35 |
Correct |
21 ms |
2536 KB |
Output is correct |
36 |
Correct |
10 ms |
3432 KB |
Output is correct |
37 |
Correct |
11 ms |
3172 KB |
Output is correct |
38 |
Correct |
27 ms |
3172 KB |
Output is correct |
39 |
Correct |
12 ms |
3928 KB |
Output is correct |
40 |
Correct |
13 ms |
3872 KB |
Output is correct |
41 |
Correct |
38 ms |
3920 KB |
Output is correct |
42 |
Correct |
16 ms |
4704 KB |
Output is correct |
43 |
Correct |
17 ms |
4704 KB |
Output is correct |
44 |
Correct |
52 ms |
4740 KB |
Output is correct |
45 |
Correct |
19 ms |
5560 KB |
Output is correct |
46 |
Correct |
20 ms |
5612 KB |
Output is correct |
47 |
Correct |
53 ms |
5816 KB |
Output is correct |
48 |
Correct |
24 ms |
6612 KB |
Output is correct |
49 |
Correct |
26 ms |
6584 KB |
Output is correct |
50 |
Correct |
63 ms |
6556 KB |
Output is correct |
51 |
Correct |
29 ms |
7516 KB |
Output is correct |
52 |
Correct |
29 ms |
7516 KB |
Output is correct |
53 |
Correct |
82 ms |
7616 KB |
Output is correct |
54 |
Correct |
35 ms |
8760 KB |
Output is correct |
55 |
Correct |
43 ms |
8760 KB |
Output is correct |
56 |
Correct |
97 ms |
8760 KB |
Output is correct |
57 |
Correct |
43 ms |
10032 KB |
Output is correct |
58 |
Correct |
44 ms |
10080 KB |
Output is correct |
59 |
Correct |
115 ms |
9936 KB |
Output is correct |
60 |
Correct |
47 ms |
11364 KB |
Output is correct |
61 |
Correct |
48 ms |
11328 KB |
Output is correct |
62 |
Correct |
135 ms |
11244 KB |
Output is correct |
63 |
Correct |
116 ms |
11232 KB |
Output is correct |
64 |
Correct |
204 ms |
11204 KB |
Output is correct |
65 |
Correct |
167 ms |
11216 KB |
Output is correct |
66 |
Correct |
141 ms |
11224 KB |
Output is correct |
67 |
Correct |
124 ms |
11260 KB |
Output is correct |
68 |
Correct |
65 ms |
11288 KB |
Output is correct |
69 |
Correct |
66 ms |
11272 KB |
Output is correct |
70 |
Correct |
54 ms |
11252 KB |
Output is correct |
71 |
Correct |
62 ms |
11332 KB |
Output is correct |
72 |
Incorrect |
53 ms |
11400 KB |
Output isn't correct |
73 |
Incorrect |
66 ms |
11320 KB |
Output isn't correct |
74 |
Correct |
73 ms |
11260 KB |
Output is correct |
75 |
Correct |
84 ms |
11296 KB |
Output is correct |
76 |
Correct |
72 ms |
11308 KB |
Output is correct |
77 |
Correct |
80 ms |
11308 KB |
Output is correct |
78 |
Correct |
86 ms |
11316 KB |
Output is correct |
79 |
Correct |
62 ms |
11300 KB |
Output is correct |
80 |
Correct |
71 ms |
11292 KB |
Output is correct |
81 |
Correct |
77 ms |
11304 KB |
Output is correct |
82 |
Correct |
74 ms |
11328 KB |
Output is correct |
83 |
Correct |
84 ms |
11320 KB |
Output is correct |
84 |
Correct |
78 ms |
11292 KB |
Output is correct |
85 |
Correct |
86 ms |
11292 KB |
Output is correct |
86 |
Correct |
88 ms |
11308 KB |
Output is correct |
87 |
Correct |
86 ms |
11260 KB |
Output is correct |
88 |
Correct |
85 ms |
11336 KB |
Output is correct |
89 |
Correct |
92 ms |
11244 KB |
Output is correct |
90 |
Correct |
94 ms |
11288 KB |
Output is correct |
91 |
Correct |
89 ms |
11296 KB |
Output is correct |
92 |
Correct |
96 ms |
11276 KB |
Output is correct |