#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <utility>
#include <cmath>
#include <numeric>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 1007;
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
queue < pair <int,int> > q;
pair <int,int> st, en, p[M][M];
char a[M][M];
int n, s, d[M][M], dist[M][M];
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> s;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
cin >> a[i][j];
if(a[i][j] == 'H') q.push({i, j}), d[i][j] = 1;
if(a[i][j] == 'D') en = {i, j};
if(a[i][j] == 'M') st = {i, j};
}
}
while(!q.empty()){
int x = q.front().S;
int y = q.front().F;
q.pop();
for(int i = 0; i < 4; ++i){
int nx = x + dx[i];
int ny = y + dy[i];
if(a[ny][nx] == 'T' || d[ny][nx] || ny < 1 || nx < 1 || ny > n || nx > n) continue;
d[ny][nx] = d[y][x] + 1;
q.push({ny, nx});
}
}
// for(int i = 1; i <= n; ++i){
// for(int j = 1; j <= n; ++j){
// cout << d[i][j] << " ";
// }
// cout << endl;
// }
q.push(st);
dist[st.F][st.S] = 1;
while(!q.empty()){
int x = q.front().S;
int y = q.front().F;
q.pop();
for(int i = 0; i < 4; ++i){
int nx = x + dx[i];
int ny = y + dy[i];
if(a[ny][nx] == 'T' || (dist[y][x]) / s >= d[y][x] - 1 || dist[ny][nx] || ny < 1 || nx < 1 || ny > n || nx > n) continue;
p[ny][nx] = {y, x};
dist[ny][nx] = dist[y][x] + 1;
q.push({ny, nx});
}
}
// cout << endl;
// for(int i = 1; i <= n; ++i){
// for(int j = 1; j <= n; ++j) cout << dist[i][j] << " ";
// cout << endl;
// }
if(!dist[en.F][en.S]) return cout << -1 << endl, 0;
int ans = 1e9;
int i = en.F, j = en.S;
while(i != st.F || j != st.S){
ans = min(ans, (d[i][j] - 1) - ((dist[i][j] - 2) / s + 1));
// cout << i << " " << j << " " << (d[i][j] - 1) - ((dist[i][j] - 2) / s + 1) << " " << d[i][j] - 1 << " " << (dist[i][j] - 1) / s << endl;
int ni = p[i][j].F, nj = p[i][j].S;
i = ni;
j = nj;
}
cout << ans << endl;
return 0;
}
// Don't forget special cases. (n = 1?)
// Look for the constraints. (Runtime array? overflow?)
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
432 KB |
Output isn't correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
7 |
Incorrect |
55 ms |
13492 KB |
Output isn't correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
14 |
Correct |
1 ms |
844 KB |
Output is correct |
15 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
20 |
Correct |
1 ms |
716 KB |
Output is correct |
21 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
22 |
Correct |
1 ms |
896 KB |
Output is correct |
23 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
24 |
Correct |
1 ms |
972 KB |
Output is correct |
25 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
26 |
Correct |
1 ms |
1100 KB |
Output is correct |
27 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
28 |
Correct |
1 ms |
1100 KB |
Output is correct |
29 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
30 |
Correct |
1 ms |
1228 KB |
Output is correct |
31 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
32 |
Correct |
1 ms |
1228 KB |
Output is correct |
33 |
Incorrect |
7 ms |
3660 KB |
Output isn't correct |
34 |
Correct |
8 ms |
5452 KB |
Output is correct |
35 |
Incorrect |
10 ms |
5716 KB |
Output isn't correct |
36 |
Incorrect |
9 ms |
4172 KB |
Output isn't correct |
37 |
Correct |
10 ms |
6400 KB |
Output is correct |
38 |
Incorrect |
14 ms |
6736 KB |
Output isn't correct |
39 |
Incorrect |
11 ms |
4684 KB |
Output isn't correct |
40 |
Correct |
11 ms |
7324 KB |
Output is correct |
41 |
Incorrect |
21 ms |
7628 KB |
Output isn't correct |
42 |
Incorrect |
11 ms |
5196 KB |
Output isn't correct |
43 |
Correct |
14 ms |
8140 KB |
Output is correct |
44 |
Incorrect |
20 ms |
8652 KB |
Output isn't correct |
45 |
Incorrect |
14 ms |
5708 KB |
Output isn't correct |
46 |
Correct |
17 ms |
8920 KB |
Output is correct |
47 |
Incorrect |
24 ms |
9420 KB |
Output isn't correct |
48 |
Incorrect |
16 ms |
6236 KB |
Output isn't correct |
49 |
Correct |
19 ms |
9804 KB |
Output is correct |
50 |
Incorrect |
30 ms |
10316 KB |
Output isn't correct |
51 |
Incorrect |
18 ms |
6772 KB |
Output isn't correct |
52 |
Correct |
21 ms |
10532 KB |
Output is correct |
53 |
Incorrect |
35 ms |
11140 KB |
Output isn't correct |
54 |
Incorrect |
22 ms |
7192 KB |
Output isn't correct |
55 |
Correct |
24 ms |
11332 KB |
Output is correct |
56 |
Incorrect |
39 ms |
11936 KB |
Output isn't correct |
57 |
Incorrect |
23 ms |
7764 KB |
Output isn't correct |
58 |
Correct |
28 ms |
12148 KB |
Output is correct |
59 |
Incorrect |
44 ms |
12796 KB |
Output isn't correct |
60 |
Incorrect |
31 ms |
8200 KB |
Output isn't correct |
61 |
Correct |
30 ms |
12904 KB |
Output is correct |
62 |
Incorrect |
50 ms |
13584 KB |
Output isn't correct |
63 |
Incorrect |
46 ms |
12836 KB |
Output isn't correct |
64 |
Correct |
57 ms |
13644 KB |
Output is correct |
65 |
Correct |
56 ms |
13636 KB |
Output is correct |
66 |
Incorrect |
54 ms |
13572 KB |
Output isn't correct |
67 |
Correct |
45 ms |
12872 KB |
Output is correct |
68 |
Correct |
42 ms |
12128 KB |
Output is correct |
69 |
Correct |
43 ms |
12616 KB |
Output is correct |
70 |
Correct |
46 ms |
11972 KB |
Output is correct |
71 |
Correct |
39 ms |
11852 KB |
Output is correct |
72 |
Incorrect |
43 ms |
12792 KB |
Output isn't correct |
73 |
Incorrect |
46 ms |
12940 KB |
Output isn't correct |
74 |
Incorrect |
66 ms |
13892 KB |
Output isn't correct |
75 |
Incorrect |
61 ms |
12976 KB |
Output isn't correct |
76 |
Incorrect |
46 ms |
12576 KB |
Output isn't correct |
77 |
Incorrect |
48 ms |
13108 KB |
Output isn't correct |
78 |
Correct |
44 ms |
9028 KB |
Output is correct |
79 |
Incorrect |
68 ms |
13832 KB |
Output isn't correct |
80 |
Correct |
44 ms |
8908 KB |
Output is correct |
81 |
Correct |
43 ms |
8852 KB |
Output is correct |
82 |
Incorrect |
46 ms |
9264 KB |
Output isn't correct |
83 |
Correct |
45 ms |
8924 KB |
Output is correct |
84 |
Incorrect |
67 ms |
13868 KB |
Output isn't correct |
85 |
Incorrect |
47 ms |
9348 KB |
Output isn't correct |
86 |
Incorrect |
49 ms |
9096 KB |
Output isn't correct |
87 |
Incorrect |
47 ms |
9720 KB |
Output isn't correct |
88 |
Correct |
47 ms |
9808 KB |
Output is correct |
89 |
Incorrect |
67 ms |
13744 KB |
Output isn't correct |
90 |
Incorrect |
52 ms |
12016 KB |
Output isn't correct |
91 |
Incorrect |
59 ms |
13636 KB |
Output isn't correct |
92 |
Incorrect |
54 ms |
9936 KB |
Output isn't correct |