#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,long long>;
using ld = long double;
#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);
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 = 2000;
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 |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 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 |
97 ms |
6236 KB |
Output is 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 |
0 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
344 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 |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Incorrect |
3 ms |
1572 KB |
Output isn't correct |
34 |
Incorrect |
4 ms |
1576 KB |
Output isn't correct |
35 |
Correct |
20 ms |
1580 KB |
Output is correct |
36 |
Incorrect |
4 ms |
1880 KB |
Output isn't correct |
37 |
Incorrect |
4 ms |
1884 KB |
Output isn't correct |
38 |
Correct |
25 ms |
1880 KB |
Output is correct |
39 |
Incorrect |
5 ms |
2292 KB |
Output isn't correct |
40 |
Incorrect |
5 ms |
2288 KB |
Output isn't correct |
41 |
Correct |
35 ms |
2308 KB |
Output is correct |
42 |
Incorrect |
8 ms |
2744 KB |
Output isn't correct |
43 |
Incorrect |
7 ms |
2700 KB |
Output isn't correct |
44 |
Correct |
43 ms |
2736 KB |
Output is correct |
45 |
Incorrect |
8 ms |
3196 KB |
Output isn't correct |
46 |
Incorrect |
8 ms |
3164 KB |
Output isn't correct |
47 |
Correct |
52 ms |
3196 KB |
Output is correct |
48 |
Incorrect |
9 ms |
3736 KB |
Output isn't correct |
49 |
Incorrect |
10 ms |
3716 KB |
Output isn't correct |
50 |
Correct |
66 ms |
3700 KB |
Output is correct |
51 |
Incorrect |
12 ms |
4184 KB |
Output isn't correct |
52 |
Incorrect |
10 ms |
4288 KB |
Output isn't correct |
53 |
Correct |
73 ms |
4276 KB |
Output is correct |
54 |
Incorrect |
13 ms |
4896 KB |
Output isn't correct |
55 |
Incorrect |
13 ms |
4884 KB |
Output isn't correct |
56 |
Correct |
92 ms |
4896 KB |
Output is correct |
57 |
Incorrect |
14 ms |
5544 KB |
Output isn't correct |
58 |
Incorrect |
15 ms |
5528 KB |
Output isn't correct |
59 |
Correct |
99 ms |
5504 KB |
Output is correct |
60 |
Incorrect |
16 ms |
6236 KB |
Output isn't correct |
61 |
Incorrect |
15 ms |
6260 KB |
Output isn't correct |
62 |
Correct |
112 ms |
6232 KB |
Output is correct |
63 |
Correct |
103 ms |
6188 KB |
Output is correct |
64 |
Correct |
172 ms |
6220 KB |
Output is correct |
65 |
Correct |
154 ms |
6232 KB |
Output is correct |
66 |
Correct |
131 ms |
6236 KB |
Output is correct |
67 |
Correct |
26 ms |
6236 KB |
Output is correct |
68 |
Correct |
46 ms |
6244 KB |
Output is correct |
69 |
Correct |
46 ms |
6264 KB |
Output is correct |
70 |
Correct |
42 ms |
6244 KB |
Output is correct |
71 |
Correct |
35 ms |
6232 KB |
Output is correct |
72 |
Incorrect |
33 ms |
6228 KB |
Output isn't correct |
73 |
Incorrect |
39 ms |
6276 KB |
Output isn't correct |
74 |
Correct |
71 ms |
6240 KB |
Output is correct |
75 |
Correct |
72 ms |
6232 KB |
Output is correct |
76 |
Correct |
65 ms |
6248 KB |
Output is correct |
77 |
Correct |
68 ms |
6232 KB |
Output is correct |
78 |
Correct |
25 ms |
6232 KB |
Output is correct |
79 |
Correct |
68 ms |
6244 KB |
Output is correct |
80 |
Correct |
65 ms |
6256 KB |
Output is correct |
81 |
Correct |
73 ms |
6260 KB |
Output is correct |
82 |
Correct |
68 ms |
6240 KB |
Output is correct |
83 |
Correct |
85 ms |
6236 KB |
Output is correct |
84 |
Correct |
98 ms |
6264 KB |
Output is correct |
85 |
Correct |
76 ms |
6248 KB |
Output is correct |
86 |
Correct |
83 ms |
6228 KB |
Output is correct |
87 |
Correct |
82 ms |
6232 KB |
Output is correct |
88 |
Correct |
87 ms |
6252 KB |
Output is correct |
89 |
Correct |
102 ms |
6216 KB |
Output is correct |
90 |
Correct |
94 ms |
6236 KB |
Output is correct |
91 |
Correct |
93 ms |
6236 KB |
Output is correct |
92 |
Correct |
90 ms |
6248 KB |
Output is correct |