#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[ny][nx] - 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?)
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 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 |
1 ms |
332 KB |
Output is correct |
6 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
7 |
Incorrect |
60 ms |
13472 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 |
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 |
13 ms |
5708 KB |
Output isn't correct |
36 |
Incorrect |
10 ms |
4172 KB |
Output isn't correct |
37 |
Correct |
10 ms |
6348 KB |
Output is correct |
38 |
Incorrect |
14 ms |
6732 KB |
Output isn't correct |
39 |
Incorrect |
13 ms |
4692 KB |
Output isn't correct |
40 |
Correct |
12 ms |
7244 KB |
Output is correct |
41 |
Incorrect |
17 ms |
7628 KB |
Output isn't correct |
42 |
Incorrect |
12 ms |
5196 KB |
Output isn't correct |
43 |
Correct |
14 ms |
8120 KB |
Output is correct |
44 |
Incorrect |
21 ms |
8696 KB |
Output isn't correct |
45 |
Incorrect |
16 ms |
5712 KB |
Output isn't correct |
46 |
Correct |
17 ms |
8964 KB |
Output is correct |
47 |
Incorrect |
25 ms |
9456 KB |
Output isn't correct |
48 |
Incorrect |
17 ms |
6284 KB |
Output isn't correct |
49 |
Correct |
20 ms |
9788 KB |
Output is correct |
50 |
Incorrect |
36 ms |
10264 KB |
Output isn't correct |
51 |
Incorrect |
21 ms |
6688 KB |
Output isn't correct |
52 |
Correct |
21 ms |
10572 KB |
Output is correct |
53 |
Incorrect |
36 ms |
11088 KB |
Output isn't correct |
54 |
Incorrect |
20 ms |
7236 KB |
Output isn't correct |
55 |
Correct |
25 ms |
11368 KB |
Output is correct |
56 |
Incorrect |
40 ms |
11980 KB |
Output isn't correct |
57 |
Incorrect |
23 ms |
7708 KB |
Output isn't correct |
58 |
Correct |
28 ms |
12144 KB |
Output is correct |
59 |
Incorrect |
58 ms |
12868 KB |
Output isn't correct |
60 |
Incorrect |
26 ms |
8140 KB |
Output isn't correct |
61 |
Correct |
40 ms |
12896 KB |
Output is correct |
62 |
Incorrect |
56 ms |
13648 KB |
Output isn't correct |
63 |
Correct |
56 ms |
13448 KB |
Output is correct |
64 |
Correct |
62 ms |
13748 KB |
Output is correct |
65 |
Correct |
59 ms |
13636 KB |
Output is correct |
66 |
Incorrect |
53 ms |
13616 KB |
Output isn't correct |
67 |
Correct |
46 ms |
12840 KB |
Output is correct |
68 |
Correct |
44 ms |
12112 KB |
Output is correct |
69 |
Correct |
44 ms |
12628 KB |
Output is correct |
70 |
Correct |
40 ms |
11964 KB |
Output is correct |
71 |
Correct |
42 ms |
11824 KB |
Output is correct |
72 |
Incorrect |
45 ms |
12744 KB |
Output isn't correct |
73 |
Incorrect |
50 ms |
12884 KB |
Output isn't correct |
74 |
Incorrect |
73 ms |
13876 KB |
Output isn't correct |
75 |
Incorrect |
51 ms |
12856 KB |
Output isn't correct |
76 |
Incorrect |
49 ms |
12536 KB |
Output isn't correct |
77 |
Incorrect |
66 ms |
13144 KB |
Output isn't correct |
78 |
Correct |
63 ms |
8948 KB |
Output is correct |
79 |
Incorrect |
74 ms |
13916 KB |
Output isn't correct |
80 |
Correct |
54 ms |
9008 KB |
Output is correct |
81 |
Correct |
55 ms |
8824 KB |
Output is correct |
82 |
Incorrect |
50 ms |
9156 KB |
Output isn't correct |
83 |
Correct |
48 ms |
8964 KB |
Output is correct |
84 |
Incorrect |
78 ms |
13892 KB |
Output isn't correct |
85 |
Incorrect |
50 ms |
9228 KB |
Output isn't correct |
86 |
Incorrect |
49 ms |
9136 KB |
Output isn't correct |
87 |
Incorrect |
52 ms |
9668 KB |
Output isn't correct |
88 |
Correct |
49 ms |
9792 KB |
Output is correct |
89 |
Incorrect |
83 ms |
13764 KB |
Output isn't correct |
90 |
Incorrect |
56 ms |
11996 KB |
Output isn't correct |
91 |
Incorrect |
63 ms |
13704 KB |
Output isn't correct |
92 |
Incorrect |
51 ms |
9796 KB |
Output isn't correct |