#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' || 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?)
# |
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 |
1 ms |
332 KB |
Output is correct |
6 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
7 |
Incorrect |
134 ms |
11068 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 |
844 KB |
Output isn't correct |
13 |
Correct |
1 ms |
844 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Incorrect |
1 ms |
588 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 |
588 KB |
Output is correct |
21 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
22 |
Correct |
1 ms |
716 KB |
Output is correct |
23 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
24 |
Correct |
2 ms |
844 KB |
Output is correct |
25 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
26 |
Correct |
1 ms |
972 KB |
Output is correct |
27 |
Incorrect |
1 ms |
972 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 |
2 ms |
1028 KB |
Output isn't correct |
32 |
Correct |
2 ms |
1228 KB |
Output is correct |
33 |
Incorrect |
13 ms |
4088 KB |
Output isn't correct |
34 |
Correct |
14 ms |
4812 KB |
Output is correct |
35 |
Incorrect |
23 ms |
5148 KB |
Output isn't correct |
36 |
Incorrect |
20 ms |
4764 KB |
Output isn't correct |
37 |
Correct |
21 ms |
5376 KB |
Output is correct |
38 |
Incorrect |
25 ms |
5744 KB |
Output isn't correct |
39 |
Incorrect |
20 ms |
5152 KB |
Output isn't correct |
40 |
Correct |
21 ms |
6012 KB |
Output is correct |
41 |
Incorrect |
31 ms |
6528 KB |
Output isn't correct |
42 |
Incorrect |
24 ms |
5700 KB |
Output isn't correct |
43 |
Correct |
26 ms |
6720 KB |
Output is correct |
44 |
Incorrect |
38 ms |
7108 KB |
Output isn't correct |
45 |
Incorrect |
28 ms |
6164 KB |
Output isn't correct |
46 |
Correct |
37 ms |
7256 KB |
Output is correct |
47 |
Incorrect |
47 ms |
7808 KB |
Output isn't correct |
48 |
Incorrect |
34 ms |
6724 KB |
Output isn't correct |
49 |
Correct |
35 ms |
7984 KB |
Output is correct |
50 |
Incorrect |
54 ms |
8516 KB |
Output isn't correct |
51 |
Incorrect |
39 ms |
7348 KB |
Output isn't correct |
52 |
Correct |
45 ms |
8592 KB |
Output is correct |
53 |
Incorrect |
61 ms |
9212 KB |
Output isn't correct |
54 |
Incorrect |
48 ms |
8004 KB |
Output isn't correct |
55 |
Correct |
49 ms |
9200 KB |
Output is correct |
56 |
Incorrect |
69 ms |
9928 KB |
Output isn't correct |
57 |
Incorrect |
51 ms |
8372 KB |
Output isn't correct |
58 |
Correct |
55 ms |
9896 KB |
Output is correct |
59 |
Incorrect |
81 ms |
10564 KB |
Output isn't correct |
60 |
Incorrect |
63 ms |
8904 KB |
Output isn't correct |
61 |
Correct |
62 ms |
10436 KB |
Output is correct |
62 |
Incorrect |
92 ms |
11240 KB |
Output isn't correct |
63 |
Correct |
105 ms |
11260 KB |
Output is correct |
64 |
Correct |
113 ms |
11332 KB |
Output is correct |
65 |
Correct |
111 ms |
11400 KB |
Output is correct |
66 |
Incorrect |
105 ms |
11268 KB |
Output isn't correct |
67 |
Correct |
74 ms |
7368 KB |
Output is correct |
68 |
Correct |
91 ms |
11292 KB |
Output is correct |
69 |
Correct |
86 ms |
11256 KB |
Output is correct |
70 |
Correct |
82 ms |
11332 KB |
Output is correct |
71 |
Correct |
77 ms |
11296 KB |
Output is correct |
72 |
Correct |
81 ms |
10644 KB |
Output is correct |
73 |
Correct |
77 ms |
11588 KB |
Output is correct |
74 |
Correct |
118 ms |
11524 KB |
Output is correct |
75 |
Correct |
120 ms |
11568 KB |
Output is correct |
76 |
Correct |
104 ms |
11588 KB |
Output is correct |
77 |
Correct |
106 ms |
11588 KB |
Output is correct |
78 |
Correct |
71 ms |
6020 KB |
Output is correct |
79 |
Correct |
108 ms |
10984 KB |
Output is correct |
80 |
Correct |
100 ms |
9416 KB |
Output is correct |
81 |
Correct |
110 ms |
9364 KB |
Output is correct |
82 |
Correct |
100 ms |
9484 KB |
Output is correct |
83 |
Correct |
124 ms |
9552 KB |
Output is correct |
84 |
Correct |
131 ms |
11076 KB |
Output is correct |
85 |
Correct |
105 ms |
9564 KB |
Output is correct |
86 |
Correct |
112 ms |
9492 KB |
Output is correct |
87 |
Correct |
122 ms |
9676 KB |
Output is correct |
88 |
Correct |
124 ms |
9796 KB |
Output is correct |
89 |
Correct |
130 ms |
11064 KB |
Output is correct |
90 |
Correct |
133 ms |
10700 KB |
Output is correct |
91 |
Correct |
128 ms |
11004 KB |
Output is correct |
92 |
Correct |
124 ms |
9804 KB |
Output is correct |