#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) {
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 = 10;
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 |
1 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 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
49 ms |
11296 KB |
Output isn't correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Incorrect |
0 ms |
604 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
11 |
Incorrect |
0 ms |
348 KB |
Output isn't 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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
18 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
19 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
20 |
Incorrect |
0 ms |
460 KB |
Output isn't correct |
21 |
Incorrect |
0 ms |
456 KB |
Output isn't correct |
22 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
24 |
Incorrect |
0 ms |
460 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
456 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
560 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
33 |
Incorrect |
6 ms |
2572 KB |
Output isn't correct |
34 |
Incorrect |
7 ms |
2576 KB |
Output isn't correct |
35 |
Correct |
11 ms |
2632 KB |
Output is correct |
36 |
Incorrect |
7 ms |
3184 KB |
Output isn't correct |
37 |
Incorrect |
10 ms |
3432 KB |
Output isn't correct |
38 |
Correct |
15 ms |
3276 KB |
Output is correct |
39 |
Incorrect |
9 ms |
4244 KB |
Output isn't correct |
40 |
Incorrect |
11 ms |
3992 KB |
Output isn't correct |
41 |
Correct |
18 ms |
4052 KB |
Output is correct |
42 |
Incorrect |
11 ms |
4844 KB |
Output isn't correct |
43 |
Incorrect |
14 ms |
4844 KB |
Output isn't correct |
44 |
Correct |
27 ms |
4832 KB |
Output is correct |
45 |
Incorrect |
15 ms |
5720 KB |
Output isn't correct |
46 |
Incorrect |
23 ms |
5764 KB |
Output isn't correct |
47 |
Correct |
35 ms |
5812 KB |
Output is correct |
48 |
Incorrect |
17 ms |
6804 KB |
Output isn't correct |
49 |
Incorrect |
19 ms |
6748 KB |
Output isn't correct |
50 |
Correct |
33 ms |
6748 KB |
Output is correct |
51 |
Incorrect |
19 ms |
7896 KB |
Output isn't correct |
52 |
Incorrect |
23 ms |
7772 KB |
Output isn't correct |
53 |
Correct |
34 ms |
7916 KB |
Output is correct |
54 |
Incorrect |
22 ms |
8916 KB |
Output isn't correct |
55 |
Incorrect |
28 ms |
9052 KB |
Output isn't correct |
56 |
Correct |
49 ms |
9140 KB |
Output is correct |
57 |
Incorrect |
25 ms |
10188 KB |
Output isn't correct |
58 |
Incorrect |
30 ms |
10208 KB |
Output isn't correct |
59 |
Correct |
46 ms |
10176 KB |
Output is correct |
60 |
Incorrect |
36 ms |
11600 KB |
Output isn't correct |
61 |
Incorrect |
34 ms |
11604 KB |
Output isn't correct |
62 |
Correct |
56 ms |
11664 KB |
Output is correct |
63 |
Incorrect |
61 ms |
11600 KB |
Output isn't correct |
64 |
Incorrect |
85 ms |
11560 KB |
Output isn't correct |
65 |
Incorrect |
80 ms |
11604 KB |
Output isn't correct |
66 |
Incorrect |
72 ms |
11612 KB |
Output isn't correct |
67 |
Correct |
50 ms |
11800 KB |
Output is correct |
68 |
Incorrect |
41 ms |
11600 KB |
Output isn't correct |
69 |
Incorrect |
49 ms |
11604 KB |
Output isn't correct |
70 |
Incorrect |
38 ms |
11600 KB |
Output isn't correct |
71 |
Incorrect |
30 ms |
11612 KB |
Output isn't correct |
72 |
Incorrect |
44 ms |
11600 KB |
Output isn't correct |
73 |
Incorrect |
49 ms |
11708 KB |
Output isn't correct |
74 |
Incorrect |
52 ms |
11680 KB |
Output isn't correct |
75 |
Incorrect |
52 ms |
11720 KB |
Output isn't correct |
76 |
Incorrect |
44 ms |
11636 KB |
Output isn't correct |
77 |
Incorrect |
50 ms |
11676 KB |
Output isn't correct |
78 |
Correct |
41 ms |
11668 KB |
Output is correct |
79 |
Incorrect |
68 ms |
11912 KB |
Output isn't correct |
80 |
Incorrect |
49 ms |
11576 KB |
Output isn't correct |
81 |
Incorrect |
42 ms |
11532 KB |
Output isn't correct |
82 |
Incorrect |
47 ms |
11604 KB |
Output isn't correct |
83 |
Correct |
50 ms |
11904 KB |
Output is correct |
84 |
Incorrect |
65 ms |
11480 KB |
Output isn't correct |
85 |
Incorrect |
52 ms |
11644 KB |
Output isn't correct |
86 |
Incorrect |
46 ms |
11688 KB |
Output isn't correct |
87 |
Incorrect |
58 ms |
11696 KB |
Output isn't correct |
88 |
Correct |
56 ms |
11640 KB |
Output is correct |
89 |
Incorrect |
84 ms |
11608 KB |
Output isn't correct |
90 |
Incorrect |
47 ms |
11600 KB |
Output isn't correct |
91 |
Incorrect |
51 ms |
11612 KB |
Output isn't correct |
92 |
Incorrect |
59 ms |
11708 KB |
Output isn't correct |