#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,long long>;
using ld = long double;
#define int long long
#define el cout << '\n'
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FOD(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
template <class T1, class T2>bool chmax(T1 &a, T2 b){if (a < b) {a = b; return true;}return false;}
template <class T1, class T2>bool chmin(T1 &a, T2 b){if (a > b) {a = b; return true;}return false;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7;
void solve(){
int n, k;
cin >> n >> k;
string s[n];
REP(i, n){
cin >> s[i];
}
pii start, des;
vector<vector<int>> dis(n, vector<int> (n ,1e9));
queue<pii> q;
REP(i, n){
REP(j, n){
if (s[i][j] == 'M'){
start = {i, j};
}
if (s[i][j] == 'D'){
des = {i, j};
}
if (s[i][j] == 'H'){
q.push({i, j});
dis[i][j] = 0;
}
}
}
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
while(q.size()){
auto [x, y] = q.front();
q.pop();
for (int i = 0; i < 4; i++){
int a = x + dx[i], b = y + dy[i];
if (a < 0 || a >= n || b < 0 || b >= n) continue;
if (s[a][b] == 'T' || s[a][b] == 'D') continue;
if (dis[x][y] + 1 < dis[a][b]){
dis[a][b] = dis[x][y] + 1;
q.push({a, b});
}
}
}
// REP(i, n){
// REP(j, n){
// cerr << dis[i][j] << " ";
// }
// cerr << '\n';
// }
// cerr << '\n';
auto check = [&](int m){
queue<pii> que;
que.push(start);
if (m * k >= dis[start.fi][start.se]) return false;
vector<vector<int>> dis2(n, vector<int> (n, 1e9));
dis2[start.fi][start.se] = 0;
while(que.size()){
auto [x, y] = que.front();
que.pop();
if (x == des.fi && y == des.se) break;
for (int i = 0; i < 4; i++){
int a = x + dx[i], b = y + dy[i];
if (a < 0 || a >= n || b < 0 || b >= n) continue;
if (s[a][b] == 'T') continue;
int cur = dis2[x][y];
if ((cur + 1) / k < dis[a][b] - m && cur + 1 < dis2[a][b]){
dis2[a][b] = cur + 1;
que.push({a, b});
}
}
}
// REP(i, n){
// REP(j, n){
// cerr << dis2[i][j] << " ";
// }
// cerr << '\n';
// }
return dis2[des.fi][des.se] != 1e9;
};
if (!check(0)){
cout << -1;
return;
}
int l = -1, r = n * n + 1;
while(r - l > 1){
int mid = (l + r) / 2;
if (check(mid)) l = mid;
else r = mid;
}
cout << l;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tests = 1;
//cin >> tests;
for (int _ = 1; _ <= tests; _++){
cerr << "- Case #" << _ << ": \n";
solve();
el;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 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 |
Incorrect |
48 ms |
11868 KB |
Output isn't correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
33 |
Correct |
6 ms |
2712 KB |
Output is correct |
34 |
Incorrect |
3 ms |
2664 KB |
Output isn't correct |
35 |
Incorrect |
6 ms |
2652 KB |
Output isn't correct |
36 |
Correct |
6 ms |
3348 KB |
Output is correct |
37 |
Incorrect |
4 ms |
3580 KB |
Output isn't correct |
38 |
Incorrect |
8 ms |
3304 KB |
Output isn't correct |
39 |
Correct |
8 ms |
4120 KB |
Output is correct |
40 |
Incorrect |
5 ms |
4112 KB |
Output isn't correct |
41 |
Incorrect |
9 ms |
3992 KB |
Output isn't correct |
42 |
Correct |
12 ms |
4964 KB |
Output is correct |
43 |
Incorrect |
6 ms |
4896 KB |
Output isn't correct |
44 |
Incorrect |
12 ms |
4956 KB |
Output isn't correct |
45 |
Correct |
15 ms |
5904 KB |
Output is correct |
46 |
Incorrect |
9 ms |
5884 KB |
Output isn't correct |
47 |
Incorrect |
14 ms |
5836 KB |
Output isn't correct |
48 |
Correct |
17 ms |
6980 KB |
Output is correct |
49 |
Incorrect |
9 ms |
6880 KB |
Output isn't correct |
50 |
Incorrect |
16 ms |
6848 KB |
Output isn't correct |
51 |
Correct |
30 ms |
8092 KB |
Output is correct |
52 |
Incorrect |
11 ms |
8016 KB |
Output isn't correct |
53 |
Incorrect |
20 ms |
7908 KB |
Output isn't correct |
54 |
Correct |
25 ms |
9272 KB |
Output is correct |
55 |
Incorrect |
14 ms |
9228 KB |
Output isn't correct |
56 |
Incorrect |
22 ms |
9216 KB |
Output isn't correct |
57 |
Correct |
31 ms |
10592 KB |
Output is correct |
58 |
Incorrect |
18 ms |
10488 KB |
Output isn't correct |
59 |
Incorrect |
31 ms |
10468 KB |
Output isn't correct |
60 |
Correct |
33 ms |
12176 KB |
Output is correct |
61 |
Incorrect |
20 ms |
11848 KB |
Output isn't correct |
62 |
Incorrect |
34 ms |
11868 KB |
Output isn't correct |
63 |
Correct |
109 ms |
11868 KB |
Output is correct |
64 |
Incorrect |
68 ms |
11868 KB |
Output isn't correct |
65 |
Correct |
148 ms |
11844 KB |
Output is correct |
66 |
Correct |
141 ms |
11864 KB |
Output is correct |
67 |
Correct |
27 ms |
11704 KB |
Output is correct |
68 |
Correct |
51 ms |
11912 KB |
Output is correct |
69 |
Incorrect |
36 ms |
11868 KB |
Output isn't correct |
70 |
Correct |
34 ms |
11732 KB |
Output is correct |
71 |
Correct |
43 ms |
11896 KB |
Output is correct |
72 |
Incorrect |
27 ms |
11864 KB |
Output isn't correct |
73 |
Incorrect |
37 ms |
11856 KB |
Output isn't correct |
74 |
Incorrect |
48 ms |
11816 KB |
Output isn't correct |
75 |
Incorrect |
52 ms |
11840 KB |
Output isn't correct |
76 |
Incorrect |
74 ms |
11840 KB |
Output isn't correct |
77 |
Incorrect |
48 ms |
11856 KB |
Output isn't correct |
78 |
Correct |
31 ms |
11864 KB |
Output is correct |
79 |
Incorrect |
54 ms |
11912 KB |
Output isn't correct |
80 |
Incorrect |
53 ms |
11880 KB |
Output isn't correct |
81 |
Correct |
67 ms |
11892 KB |
Output is correct |
82 |
Incorrect |
68 ms |
11876 KB |
Output isn't correct |
83 |
Correct |
74 ms |
11912 KB |
Output is correct |
84 |
Incorrect |
61 ms |
11868 KB |
Output isn't correct |
85 |
Incorrect |
59 ms |
11940 KB |
Output isn't correct |
86 |
Correct |
64 ms |
11832 KB |
Output is correct |
87 |
Incorrect |
52 ms |
11896 KB |
Output isn't correct |
88 |
Correct |
68 ms |
11892 KB |
Output is correct |
89 |
Incorrect |
64 ms |
11896 KB |
Output isn't correct |
90 |
Incorrect |
47 ms |
11916 KB |
Output isn't correct |
91 |
Incorrect |
61 ms |
11856 KB |
Output isn't correct |
92 |
Correct |
78 ms |
11868 KB |
Output is correct |