#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] != 'G' || 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] != 'G' || (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?)
# |
결과 |
실행 시간 |
메모리 |
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 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
7 |
Incorrect |
30 ms |
4304 KB |
Output isn't correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
33 |
Incorrect |
5 ms |
1996 KB |
Output isn't correct |
34 |
Incorrect |
5 ms |
1996 KB |
Output isn't correct |
35 |
Incorrect |
6 ms |
1996 KB |
Output isn't correct |
36 |
Incorrect |
6 ms |
2252 KB |
Output isn't correct |
37 |
Incorrect |
6 ms |
2252 KB |
Output isn't correct |
38 |
Incorrect |
8 ms |
2252 KB |
Output isn't correct |
39 |
Incorrect |
7 ms |
2508 KB |
Output isn't correct |
40 |
Incorrect |
7 ms |
2508 KB |
Output isn't correct |
41 |
Incorrect |
9 ms |
2508 KB |
Output isn't correct |
42 |
Incorrect |
9 ms |
2764 KB |
Output isn't correct |
43 |
Incorrect |
10 ms |
2736 KB |
Output isn't correct |
44 |
Incorrect |
11 ms |
2764 KB |
Output isn't correct |
45 |
Incorrect |
10 ms |
3000 KB |
Output isn't correct |
46 |
Incorrect |
10 ms |
3044 KB |
Output isn't correct |
47 |
Incorrect |
13 ms |
3056 KB |
Output isn't correct |
48 |
Incorrect |
12 ms |
3276 KB |
Output isn't correct |
49 |
Incorrect |
11 ms |
3276 KB |
Output isn't correct |
50 |
Incorrect |
15 ms |
3196 KB |
Output isn't correct |
51 |
Incorrect |
13 ms |
3532 KB |
Output isn't correct |
52 |
Incorrect |
13 ms |
3532 KB |
Output isn't correct |
53 |
Incorrect |
18 ms |
3532 KB |
Output isn't correct |
54 |
Incorrect |
15 ms |
3720 KB |
Output isn't correct |
55 |
Incorrect |
14 ms |
3768 KB |
Output isn't correct |
56 |
Incorrect |
20 ms |
3788 KB |
Output isn't correct |
57 |
Incorrect |
19 ms |
4040 KB |
Output isn't correct |
58 |
Incorrect |
17 ms |
3932 KB |
Output isn't correct |
59 |
Incorrect |
25 ms |
4044 KB |
Output isn't correct |
60 |
Incorrect |
19 ms |
4184 KB |
Output isn't correct |
61 |
Incorrect |
18 ms |
4172 KB |
Output isn't correct |
62 |
Incorrect |
26 ms |
4172 KB |
Output isn't correct |
63 |
Incorrect |
29 ms |
4280 KB |
Output isn't correct |
64 |
Incorrect |
29 ms |
4200 KB |
Output isn't correct |
65 |
Incorrect |
31 ms |
4268 KB |
Output isn't correct |
66 |
Incorrect |
29 ms |
4280 KB |
Output isn't correct |
67 |
Correct |
29 ms |
4248 KB |
Output is correct |
68 |
Incorrect |
39 ms |
4300 KB |
Output isn't correct |
69 |
Incorrect |
35 ms |
4308 KB |
Output isn't correct |
70 |
Incorrect |
34 ms |
4300 KB |
Output isn't correct |
71 |
Incorrect |
34 ms |
4268 KB |
Output isn't correct |
72 |
Incorrect |
40 ms |
4300 KB |
Output isn't correct |
73 |
Incorrect |
32 ms |
4548 KB |
Output isn't correct |
74 |
Incorrect |
33 ms |
4448 KB |
Output isn't correct |
75 |
Incorrect |
32 ms |
4532 KB |
Output isn't correct |
76 |
Incorrect |
31 ms |
4428 KB |
Output isn't correct |
77 |
Incorrect |
33 ms |
4492 KB |
Output isn't correct |
78 |
Correct |
30 ms |
4456 KB |
Output is correct |
79 |
Incorrect |
31 ms |
4476 KB |
Output isn't correct |
80 |
Incorrect |
31 ms |
4432 KB |
Output isn't correct |
81 |
Incorrect |
31 ms |
4436 KB |
Output isn't correct |
82 |
Incorrect |
31 ms |
4448 KB |
Output isn't correct |
83 |
Incorrect |
30 ms |
4480 KB |
Output isn't correct |
84 |
Incorrect |
31 ms |
4428 KB |
Output isn't correct |
85 |
Incorrect |
31 ms |
4360 KB |
Output isn't correct |
86 |
Incorrect |
30 ms |
4380 KB |
Output isn't correct |
87 |
Incorrect |
30 ms |
4428 KB |
Output isn't correct |
88 |
Incorrect |
31 ms |
4420 KB |
Output isn't correct |
89 |
Incorrect |
30 ms |
4316 KB |
Output isn't correct |
90 |
Incorrect |
31 ms |
4424 KB |
Output isn't correct |
91 |
Incorrect |
30 ms |
4420 KB |
Output isn't correct |
92 |
Incorrect |
35 ms |
4348 KB |
Output isn't correct |