#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define EB emplace_back
#define all(x) std::begin(x), std::end(x)
#define sz(x) (int)(x).size()
#define rep(i, a, b) for(long long i = a; i < (b); ++i)
#define endl '\n'
#define debarr(a, n) cerr << #a << " : ";for(int i = 0; i < n; i++) cerr << a[i] << " "; cerr << endl;
#define debmat(mat, row, col) cerr << #mat << " :\n";for(int i = 0; i < row; i++) {for(int j = 0; j < col; j++) cerr << mat[i][j] << " ";cerr << endl;}
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class S, class T>ostream& operator <<(ostream& os, const pair<S, T>& p) {return os << "(" << p.first << ", " << p.second << ")";}
template <class T>ostream& operator <<(ostream& os, const vector<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const unordered_set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const unordered_map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const set<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T>ostream& operator <<(ostream& os, const multiset<T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class S, class T>ostream& operator <<(ostream& os, const map<S, T>& p) {os << "[ "; for (auto& it : p) os << it << " "; return os << "]";}
template <class T> void dbs(string str, T t) {cerr << str << " : " << t << "\n";}
template <class T, class... S> void dbs(string str, T t, S... s) {int idx = str.find(','); cerr << str.substr(0, idx) << " : " << t << ","; dbs(str.substr(idx + 1), s...);}
template <class T> void prc(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) {if (i != a) cerr << ", "; cerr << *i;} cerr << "]\n";}
typedef long long lli;
typedef pair<lli, lli> ii; typedef vector<lli> vi;
typedef vector<ii> vii; typedef vector<vi> graph;
bool ckmax(auto &a, auto const& b) {return b > a ? a = b, 1 : 0;}
bool ckmin(auto &a, auto const& b) {return b < a ? a = b, 1 : 0;}
int const MOD = 1000000007;
void pre() {
}
void solve() {
lli n, s;
cin >> n >> s;
vector<string> arr(n);
rep(i, 0, n) {
cin >> arr[i];
}
ii mecho, home;
rep(i, 0, n)rep(j, 0, n) {
if (arr[i][j] == 'M') {
mecho = {i, j};
}
else if (arr[i][j] == 'D') {
home = {i, j};
}
}
// checks function
auto checks = [&](lli xx, lli yy)->bool{
if (xx >= 0 && xx < n && yy >= 0 && yy < n && (arr[xx][yy] == 'G' || arr[xx][yy] == 'D'))return true;
return false;
};
int const dx[4] = { -1, 1, 0, 0};
int const dy[4] = {0, 0, -1, 1};
graph dis(n, vi(n, 1e9));
queue<ii> q;
rep(i, 0, n)rep(j, 0, n)if (arr[i][j] == 'H') {
q.push({i, j});
dis[i][j] = 0;
}
while (!q.empty()) {
auto nn = q.front(); q.pop();
rep(dir, 0, 4) {
lli xx = nn.F + dx[dir];
lli yy = nn.S + dy[dir];
if (checks(xx, yy)) {
if (dis[xx][yy] > dis[nn.F][nn.S] + 1) {
dis[xx][yy] = dis[nn.F][nn.S] + 1;
q.push({xx, yy});
}
}
}
}
// check function
auto check = [&](lli mid)->bool{
graph d(n, vi(n, 1e9));
d[mecho.F][mecho.S] = 0;
q.push(mecho);
while (!q.empty()) {
auto nn = q.front(); q.pop();
if (dis[nn.F][nn.S] < mid + (d[nn.F][nn.S] + s - 1) / s) {
continue;
}
rep(dir, 0, 4) {
lli xx = nn.F + dx[dir];
lli yy = nn.S + dy[dir];
if (xx >= 0 && xx < n && yy >= 0 && yy < n && (arr[xx][yy] == 'G' || arr[xx][yy] == 'D')) {
if (d[xx][yy] > d[nn.F][nn.S] + 1) {
d[xx][yy] = d[nn.F][nn.S] + 1;
q.push({xx, yy});
}
}
}
}
return mid + (d[home.F][home.S] + s - 1) / s < dis[home.F][home.S];
};
lli low = 0;
lli high = 1e9;
lli ans = -1;
while (low <= high) {
lli mid = (high + low) / 2;
if (check(mid)) {
ans = mid;
low = mid + 1;
} else high = mid - 1;
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
pre();
int T = 1;
while (T--)
solve();
return 0;
}
Compilation message
mecho.cpp:27:12: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
27 | bool ckmax(auto &a, auto const& b) {return b > a ? a = b, 1 : 0;}
| ^~~~
mecho.cpp:27:21: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
27 | bool ckmax(auto &a, auto const& b) {return b > a ? a = b, 1 : 0;}
| ^~~~
mecho.cpp:28:12: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
28 | bool ckmin(auto &a, auto const& b) {return b < a ? a = b, 1 : 0;}
| ^~~~
mecho.cpp:28:21: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
28 | bool ckmin(auto &a, auto const& b) {return b < a ? a = b, 1 : 0;}
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
104 ms |
11372 KB |
Output isn't correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 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 |
348 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 |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
408 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
11 ms |
2536 KB |
Output is correct |
34 |
Correct |
11 ms |
2536 KB |
Output is correct |
35 |
Correct |
25 ms |
2548 KB |
Output is correct |
36 |
Correct |
13 ms |
3284 KB |
Output is correct |
37 |
Correct |
12 ms |
3244 KB |
Output is correct |
38 |
Correct |
32 ms |
3168 KB |
Output is correct |
39 |
Correct |
16 ms |
3968 KB |
Output is correct |
40 |
Correct |
16 ms |
3968 KB |
Output is correct |
41 |
Correct |
41 ms |
4000 KB |
Output is correct |
42 |
Correct |
19 ms |
4780 KB |
Output is correct |
43 |
Correct |
19 ms |
4780 KB |
Output is correct |
44 |
Correct |
56 ms |
4824 KB |
Output is correct |
45 |
Correct |
28 ms |
5860 KB |
Output is correct |
46 |
Correct |
24 ms |
5676 KB |
Output is correct |
47 |
Correct |
64 ms |
5620 KB |
Output is correct |
48 |
Correct |
30 ms |
6904 KB |
Output is correct |
49 |
Correct |
29 ms |
6620 KB |
Output is correct |
50 |
Correct |
77 ms |
6632 KB |
Output is correct |
51 |
Correct |
34 ms |
7748 KB |
Output is correct |
52 |
Correct |
34 ms |
7756 KB |
Output is correct |
53 |
Correct |
85 ms |
7660 KB |
Output is correct |
54 |
Correct |
51 ms |
8844 KB |
Output is correct |
55 |
Correct |
41 ms |
8900 KB |
Output is correct |
56 |
Correct |
108 ms |
8876 KB |
Output is correct |
57 |
Correct |
48 ms |
10088 KB |
Output is correct |
58 |
Correct |
43 ms |
10076 KB |
Output is correct |
59 |
Correct |
159 ms |
10060 KB |
Output is correct |
60 |
Correct |
51 ms |
11412 KB |
Output is correct |
61 |
Correct |
49 ms |
11332 KB |
Output is correct |
62 |
Correct |
145 ms |
11600 KB |
Output is correct |
63 |
Correct |
124 ms |
11340 KB |
Output is correct |
64 |
Correct |
199 ms |
11304 KB |
Output is correct |
65 |
Correct |
186 ms |
11376 KB |
Output is correct |
66 |
Incorrect |
141 ms |
11336 KB |
Output isn't correct |
67 |
Correct |
138 ms |
11356 KB |
Output is correct |
68 |
Correct |
80 ms |
11656 KB |
Output is correct |
69 |
Correct |
74 ms |
11436 KB |
Output is correct |
70 |
Correct |
65 ms |
11508 KB |
Output is correct |
71 |
Correct |
69 ms |
11416 KB |
Output is correct |
72 |
Incorrect |
66 ms |
11476 KB |
Output isn't correct |
73 |
Incorrect |
88 ms |
11444 KB |
Output isn't correct |
74 |
Correct |
85 ms |
11408 KB |
Output is correct |
75 |
Correct |
91 ms |
11472 KB |
Output is correct |
76 |
Correct |
82 ms |
11380 KB |
Output is correct |
77 |
Correct |
86 ms |
11344 KB |
Output is correct |
78 |
Correct |
96 ms |
11404 KB |
Output is correct |
79 |
Correct |
78 ms |
11488 KB |
Output is correct |
80 |
Correct |
85 ms |
11400 KB |
Output is correct |
81 |
Correct |
93 ms |
11420 KB |
Output is correct |
82 |
Correct |
97 ms |
11656 KB |
Output is correct |
83 |
Correct |
100 ms |
11372 KB |
Output is correct |
84 |
Correct |
92 ms |
11420 KB |
Output is correct |
85 |
Correct |
95 ms |
11368 KB |
Output is correct |
86 |
Correct |
118 ms |
11380 KB |
Output is correct |
87 |
Correct |
109 ms |
11636 KB |
Output is correct |
88 |
Correct |
104 ms |
11364 KB |
Output is correct |
89 |
Correct |
104 ms |
11388 KB |
Output is correct |
90 |
Correct |
120 ms |
11408 KB |
Output is correct |
91 |
Correct |
128 ms |
11388 KB |
Output is correct |
92 |
Correct |
107 ms |
11388 KB |
Output is correct |