#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] == 'H' || 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] == 'H' || 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 |
332 KB |
Output isn't correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
7 |
Incorrect |
69 ms |
13488 KB |
Output isn't correct |
8 |
Correct |
1 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 |
844 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 |
1164 KB |
Output is correct |
31 |
Incorrect |
1 ms |
932 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 |
10 ms |
5520 KB |
Output is correct |
35 |
Incorrect |
11 ms |
5708 KB |
Output isn't correct |
36 |
Incorrect |
8 ms |
4220 KB |
Output isn't correct |
37 |
Correct |
10 ms |
6348 KB |
Output is correct |
38 |
Incorrect |
19 ms |
6732 KB |
Output isn't correct |
39 |
Incorrect |
10 ms |
4676 KB |
Output isn't correct |
40 |
Correct |
12 ms |
7264 KB |
Output is correct |
41 |
Incorrect |
17 ms |
7668 KB |
Output isn't correct |
42 |
Incorrect |
12 ms |
5196 KB |
Output isn't correct |
43 |
Correct |
15 ms |
8140 KB |
Output is correct |
44 |
Incorrect |
20 ms |
8652 KB |
Output isn't correct |
45 |
Incorrect |
15 ms |
5836 KB |
Output isn't correct |
46 |
Correct |
17 ms |
9036 KB |
Output is correct |
47 |
Incorrect |
25 ms |
9432 KB |
Output isn't correct |
48 |
Incorrect |
18 ms |
6216 KB |
Output isn't correct |
49 |
Correct |
23 ms |
9732 KB |
Output is correct |
50 |
Incorrect |
30 ms |
10292 KB |
Output isn't correct |
51 |
Incorrect |
18 ms |
6700 KB |
Output isn't correct |
52 |
Correct |
22 ms |
10572 KB |
Output is correct |
53 |
Incorrect |
33 ms |
11140 KB |
Output isn't correct |
54 |
Incorrect |
20 ms |
7160 KB |
Output isn't correct |
55 |
Correct |
25 ms |
11412 KB |
Output is correct |
56 |
Incorrect |
39 ms |
11968 KB |
Output isn't correct |
57 |
Incorrect |
24 ms |
7676 KB |
Output isn't correct |
58 |
Correct |
28 ms |
12132 KB |
Output is correct |
59 |
Incorrect |
45 ms |
12740 KB |
Output isn't correct |
60 |
Incorrect |
26 ms |
8280 KB |
Output isn't correct |
61 |
Correct |
32 ms |
12876 KB |
Output is correct |
62 |
Incorrect |
52 ms |
13664 KB |
Output isn't correct |
63 |
Incorrect |
44 ms |
12832 KB |
Output isn't correct |
64 |
Correct |
58 ms |
13636 KB |
Output is correct |
65 |
Correct |
58 ms |
13588 KB |
Output is correct |
66 |
Incorrect |
64 ms |
13584 KB |
Output isn't correct |
67 |
Correct |
45 ms |
12868 KB |
Output is correct |
68 |
Correct |
42 ms |
12104 KB |
Output is correct |
69 |
Correct |
46 ms |
12612 KB |
Output is correct |
70 |
Correct |
41 ms |
11840 KB |
Output is correct |
71 |
Correct |
40 ms |
11836 KB |
Output is correct |
72 |
Incorrect |
46 ms |
12740 KB |
Output isn't correct |
73 |
Incorrect |
65 ms |
12828 KB |
Output isn't correct |
74 |
Incorrect |
68 ms |
13896 KB |
Output isn't correct |
75 |
Incorrect |
48 ms |
12852 KB |
Output isn't correct |
76 |
Incorrect |
48 ms |
12612 KB |
Output isn't correct |
77 |
Incorrect |
52 ms |
13112 KB |
Output isn't correct |
78 |
Correct |
60 ms |
8944 KB |
Output is correct |
79 |
Incorrect |
73 ms |
13848 KB |
Output isn't correct |
80 |
Correct |
50 ms |
9028 KB |
Output is correct |
81 |
Correct |
44 ms |
8812 KB |
Output is correct |
82 |
Incorrect |
47 ms |
9244 KB |
Output isn't correct |
83 |
Correct |
47 ms |
9028 KB |
Output is correct |
84 |
Incorrect |
68 ms |
13808 KB |
Output isn't correct |
85 |
Incorrect |
53 ms |
9196 KB |
Output isn't correct |
86 |
Incorrect |
48 ms |
9140 KB |
Output isn't correct |
87 |
Incorrect |
48 ms |
9668 KB |
Output isn't correct |
88 |
Correct |
64 ms |
9700 KB |
Output is correct |
89 |
Incorrect |
72 ms |
13848 KB |
Output isn't correct |
90 |
Incorrect |
55 ms |
11972 KB |
Output isn't correct |
91 |
Incorrect |
61 ms |
13760 KB |
Output isn't correct |
92 |
Incorrect |
50 ms |
9924 KB |
Output isn't correct |