#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();
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) {
lli val = d[nn.F][nn.S] + 1;
if (dis[xx][yy] >= mid + (val + s - 1) / s) {
d[xx][yy] = val;
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 |
1 ms |
344 KB |
Output is correct |
6 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
7 |
Incorrect |
109 ms |
11416 KB |
Output isn't correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 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 |
1 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 |
344 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 |
1 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 |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Correct |
1 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 |
348 KB |
Output is correct |
33 |
Correct |
10 ms |
2648 KB |
Output is correct |
34 |
Correct |
10 ms |
2628 KB |
Output is correct |
35 |
Correct |
25 ms |
2552 KB |
Output is correct |
36 |
Correct |
15 ms |
3240 KB |
Output is correct |
37 |
Correct |
13 ms |
3244 KB |
Output is correct |
38 |
Correct |
31 ms |
3240 KB |
Output is correct |
39 |
Correct |
22 ms |
3972 KB |
Output is correct |
40 |
Correct |
18 ms |
3880 KB |
Output is correct |
41 |
Correct |
38 ms |
3972 KB |
Output is correct |
42 |
Correct |
19 ms |
4784 KB |
Output is correct |
43 |
Correct |
19 ms |
4780 KB |
Output is correct |
44 |
Correct |
54 ms |
4772 KB |
Output is correct |
45 |
Correct |
24 ms |
5676 KB |
Output is correct |
46 |
Correct |
23 ms |
5652 KB |
Output is correct |
47 |
Correct |
65 ms |
5640 KB |
Output is correct |
48 |
Correct |
29 ms |
6732 KB |
Output is correct |
49 |
Correct |
29 ms |
6624 KB |
Output is correct |
50 |
Correct |
74 ms |
6628 KB |
Output is correct |
51 |
Correct |
34 ms |
7876 KB |
Output is correct |
52 |
Correct |
34 ms |
7704 KB |
Output is correct |
53 |
Correct |
83 ms |
7712 KB |
Output is correct |
54 |
Correct |
40 ms |
8848 KB |
Output is correct |
55 |
Correct |
41 ms |
8744 KB |
Output is correct |
56 |
Correct |
108 ms |
8888 KB |
Output is correct |
57 |
Correct |
49 ms |
10036 KB |
Output is correct |
58 |
Correct |
48 ms |
10100 KB |
Output is correct |
59 |
Correct |
155 ms |
10040 KB |
Output is correct |
60 |
Correct |
50 ms |
11448 KB |
Output is correct |
61 |
Correct |
50 ms |
11332 KB |
Output is correct |
62 |
Correct |
163 ms |
11364 KB |
Output is correct |
63 |
Correct |
128 ms |
11308 KB |
Output is correct |
64 |
Correct |
185 ms |
11324 KB |
Output is correct |
65 |
Correct |
179 ms |
11356 KB |
Output is correct |
66 |
Incorrect |
134 ms |
11332 KB |
Output isn't correct |
67 |
Correct |
128 ms |
11612 KB |
Output is correct |
68 |
Correct |
76 ms |
11472 KB |
Output is correct |
69 |
Correct |
75 ms |
11476 KB |
Output is correct |
70 |
Correct |
60 ms |
11444 KB |
Output is correct |
71 |
Correct |
64 ms |
11488 KB |
Output is correct |
72 |
Incorrect |
66 ms |
11556 KB |
Output isn't correct |
73 |
Incorrect |
92 ms |
11412 KB |
Output isn't correct |
74 |
Correct |
85 ms |
11516 KB |
Output is correct |
75 |
Correct |
85 ms |
11352 KB |
Output is correct |
76 |
Correct |
87 ms |
11708 KB |
Output is correct |
77 |
Correct |
86 ms |
11416 KB |
Output is correct |
78 |
Correct |
92 ms |
11356 KB |
Output is correct |
79 |
Correct |
80 ms |
11324 KB |
Output is correct |
80 |
Correct |
83 ms |
11412 KB |
Output is correct |
81 |
Correct |
97 ms |
11472 KB |
Output is correct |
82 |
Correct |
81 ms |
11400 KB |
Output is correct |
83 |
Correct |
100 ms |
11376 KB |
Output is correct |
84 |
Correct |
99 ms |
11356 KB |
Output is correct |
85 |
Correct |
99 ms |
11564 KB |
Output is correct |
86 |
Correct |
99 ms |
11444 KB |
Output is correct |
87 |
Correct |
101 ms |
11388 KB |
Output is correct |
88 |
Correct |
103 ms |
11448 KB |
Output is correct |
89 |
Correct |
99 ms |
11364 KB |
Output is correct |
90 |
Correct |
100 ms |
11412 KB |
Output is correct |
91 |
Correct |
108 ms |
11408 KB |
Output is correct |
92 |
Correct |
110 ms |
11416 KB |
Output is correct |