#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;
char a[M][M];
int n, s, d[M][M], dist[M][M], res[M][M];
bool vist[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' || a[ny][nx] == 'D' || 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;
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;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
res[i][j] = -1;
if(d[i][j] && dist[i][j]) res[i][j] = (d[i][j] - 1) - ((dist[i][j] - 2) / s + 1);
// cout << res[i][j] << " ";
}
// cout << endl;
}
priority_queue < pair < int, pair <int,int> > > pq;
pq.push({res[st.F][st.S], st});
vist[st.F][st.S] = 1;
while(!pq.empty()){
int x = pq.top().S.S;
int y = pq.top().S.F;
int cost = pq.top().F;
pq.pop();
// cout << x << " " << y << " " << cost << endl;
if(x == en.S && y == en.F) return cout << cost << endl, 0;
for(int i = 0; i < 4; ++i){
int nx = x + dx[i];
int ny = y + dy[i];
int ncost = min(cost, res[ny][nx]);
if(vist[ny][nx] || dist[y][x] >= dist[ny][nx] || ncost == -1 || ny < 1 || nx < 1 || ny > n || nx > n) continue;
vist[ny][nx] = 1;
pq.push({ncost, {ny, nx}});
}
}
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 |
0 ms |
332 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
7 |
Incorrect |
82 ms |
7428 KB |
Output isn't correct |
8 |
Correct |
1 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 |
844 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
460 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 |
588 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
32 |
Incorrect |
2 ms |
972 KB |
Output isn't correct |
33 |
Incorrect |
13 ms |
2612 KB |
Output isn't correct |
34 |
Incorrect |
13 ms |
3388 KB |
Output isn't correct |
35 |
Incorrect |
15 ms |
3360 KB |
Output isn't correct |
36 |
Incorrect |
15 ms |
3020 KB |
Output isn't correct |
37 |
Incorrect |
19 ms |
3864 KB |
Output isn't correct |
38 |
Incorrect |
24 ms |
3788 KB |
Output isn't correct |
39 |
Incorrect |
18 ms |
3308 KB |
Output isn't correct |
40 |
Incorrect |
19 ms |
4300 KB |
Output isn't correct |
41 |
Incorrect |
26 ms |
4192 KB |
Output isn't correct |
42 |
Incorrect |
22 ms |
3684 KB |
Output isn't correct |
43 |
Incorrect |
24 ms |
4620 KB |
Output isn't correct |
44 |
Incorrect |
30 ms |
4644 KB |
Output isn't correct |
45 |
Incorrect |
31 ms |
3996 KB |
Output isn't correct |
46 |
Incorrect |
28 ms |
5060 KB |
Output isn't correct |
47 |
Incorrect |
35 ms |
5080 KB |
Output isn't correct |
48 |
Incorrect |
31 ms |
4344 KB |
Output isn't correct |
49 |
Incorrect |
33 ms |
5568 KB |
Output isn't correct |
50 |
Incorrect |
42 ms |
5576 KB |
Output isn't correct |
51 |
Incorrect |
36 ms |
4676 KB |
Output isn't correct |
52 |
Incorrect |
38 ms |
6032 KB |
Output isn't correct |
53 |
Incorrect |
49 ms |
6052 KB |
Output isn't correct |
54 |
Incorrect |
43 ms |
5060 KB |
Output isn't correct |
55 |
Incorrect |
46 ms |
6484 KB |
Output isn't correct |
56 |
Incorrect |
57 ms |
6440 KB |
Output isn't correct |
57 |
Incorrect |
48 ms |
5456 KB |
Output isn't correct |
58 |
Incorrect |
50 ms |
6896 KB |
Output isn't correct |
59 |
Incorrect |
63 ms |
6940 KB |
Output isn't correct |
60 |
Incorrect |
55 ms |
5796 KB |
Output isn't correct |
61 |
Incorrect |
60 ms |
7364 KB |
Output isn't correct |
62 |
Incorrect |
73 ms |
7364 KB |
Output isn't correct |
63 |
Incorrect |
81 ms |
7304 KB |
Output isn't correct |
64 |
Incorrect |
87 ms |
7376 KB |
Output isn't correct |
65 |
Incorrect |
79 ms |
7288 KB |
Output isn't correct |
66 |
Incorrect |
80 ms |
7364 KB |
Output isn't correct |
67 |
Correct |
72 ms |
7360 KB |
Output is correct |
68 |
Incorrect |
70 ms |
7328 KB |
Output isn't correct |
69 |
Incorrect |
72 ms |
7308 KB |
Output isn't correct |
70 |
Incorrect |
69 ms |
7472 KB |
Output isn't correct |
71 |
Incorrect |
66 ms |
7412 KB |
Output isn't correct |
72 |
Incorrect |
73 ms |
7396 KB |
Output isn't correct |
73 |
Incorrect |
75 ms |
7576 KB |
Output isn't correct |
74 |
Incorrect |
101 ms |
7620 KB |
Output isn't correct |
75 |
Incorrect |
77 ms |
7560 KB |
Output isn't correct |
76 |
Incorrect |
76 ms |
7620 KB |
Output isn't correct |
77 |
Incorrect |
78 ms |
7556 KB |
Output isn't correct |
78 |
Correct |
74 ms |
5996 KB |
Output is correct |
79 |
Incorrect |
96 ms |
7560 KB |
Output isn't correct |
80 |
Incorrect |
77 ms |
5952 KB |
Output isn't correct |
81 |
Incorrect |
76 ms |
5888 KB |
Output isn't correct |
82 |
Incorrect |
80 ms |
5956 KB |
Output isn't correct |
83 |
Incorrect |
75 ms |
5924 KB |
Output isn't correct |
84 |
Incorrect |
94 ms |
7668 KB |
Output isn't correct |
85 |
Incorrect |
78 ms |
5968 KB |
Output isn't correct |
86 |
Incorrect |
75 ms |
6056 KB |
Output isn't correct |
87 |
Incorrect |
75 ms |
6212 KB |
Output isn't correct |
88 |
Incorrect |
78 ms |
6256 KB |
Output isn't correct |
89 |
Incorrect |
105 ms |
7528 KB |
Output isn't correct |
90 |
Incorrect |
81 ms |
7128 KB |
Output isn't correct |
91 |
Incorrect |
86 ms |
7492 KB |
Output isn't correct |
92 |
Incorrect |
76 ms |
6240 KB |
Output isn't correct |