#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;
int n, s, d[M][M], dist[M][M];
char a[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] = s;
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] + s;
q.push({ny, nx});
}
}
d[en.F][en.S] = 2e9;
int l = 0, r = 1e6;
while(l < r){
memset(dist, 0, sizeof dist);
int mid = (l + r) / 2;
q.push(st);
dist[st.F][st.S] = mid * s + 1;
if(dist[st.F][st.S] - 1 >= d[st.F][st.S] - s) q.pop();
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] == 'T' || dist[ny][nx] || dist[y][x] >= d[ny][nx] - s || ny > n || nx > n || ny < 1 || nx < 1) continue;
dist[ny][nx] = dist[y][x] + 1;
q.push({ny, nx});
}
}
if(dist[en.F][en.S]) l = mid + 1;
else r = mid;
}
cout << l - 1 << endl;
return 0;
}
// Don't forget special cases. (n = 1?)
// Look for the constraints. (Runtime array? overflow?)
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
4300 KB |
Output is correct |
2 |
Correct |
5 ms |
4300 KB |
Output is correct |
3 |
Correct |
5 ms |
4300 KB |
Output is correct |
4 |
Correct |
5 ms |
4300 KB |
Output is correct |
5 |
Correct |
5 ms |
4300 KB |
Output is correct |
6 |
Correct |
5 ms |
4300 KB |
Output is correct |
7 |
Correct |
141 ms |
8304 KB |
Output is correct |
8 |
Correct |
5 ms |
4300 KB |
Output is correct |
9 |
Correct |
6 ms |
4300 KB |
Output is correct |
10 |
Correct |
6 ms |
4300 KB |
Output is correct |
11 |
Correct |
6 ms |
4352 KB |
Output is correct |
12 |
Correct |
5 ms |
4556 KB |
Output is correct |
13 |
Correct |
5 ms |
4428 KB |
Output is correct |
14 |
Correct |
7 ms |
4556 KB |
Output is correct |
15 |
Correct |
5 ms |
4300 KB |
Output is correct |
16 |
Correct |
6 ms |
4380 KB |
Output is correct |
17 |
Correct |
5 ms |
4300 KB |
Output is correct |
18 |
Correct |
5 ms |
4300 KB |
Output is correct |
19 |
Correct |
6 ms |
4300 KB |
Output is correct |
20 |
Correct |
5 ms |
4300 KB |
Output is correct |
21 |
Correct |
6 ms |
4428 KB |
Output is correct |
22 |
Correct |
6 ms |
4428 KB |
Output is correct |
23 |
Correct |
5 ms |
4428 KB |
Output is correct |
24 |
Correct |
5 ms |
4428 KB |
Output is correct |
25 |
Correct |
5 ms |
4572 KB |
Output is correct |
26 |
Correct |
5 ms |
4556 KB |
Output is correct |
27 |
Correct |
5 ms |
4556 KB |
Output is correct |
28 |
Correct |
5 ms |
4600 KB |
Output is correct |
29 |
Correct |
5 ms |
4556 KB |
Output is correct |
30 |
Correct |
5 ms |
4556 KB |
Output is correct |
31 |
Correct |
6 ms |
4556 KB |
Output is correct |
32 |
Correct |
5 ms |
4556 KB |
Output is correct |
33 |
Correct |
16 ms |
5996 KB |
Output is correct |
34 |
Correct |
16 ms |
5880 KB |
Output is correct |
35 |
Correct |
31 ms |
5964 KB |
Output is correct |
36 |
Correct |
18 ms |
6244 KB |
Output is correct |
37 |
Correct |
17 ms |
6220 KB |
Output is correct |
38 |
Correct |
40 ms |
6220 KB |
Output is correct |
39 |
Correct |
26 ms |
6496 KB |
Output is correct |
40 |
Correct |
26 ms |
6476 KB |
Output is correct |
41 |
Correct |
49 ms |
6496 KB |
Output is correct |
42 |
Correct |
25 ms |
6732 KB |
Output is correct |
43 |
Correct |
26 ms |
6624 KB |
Output is correct |
44 |
Correct |
62 ms |
6652 KB |
Output is correct |
45 |
Correct |
28 ms |
6928 KB |
Output is correct |
46 |
Correct |
29 ms |
6896 KB |
Output is correct |
47 |
Correct |
78 ms |
7020 KB |
Output is correct |
48 |
Correct |
38 ms |
7180 KB |
Output is correct |
49 |
Correct |
38 ms |
7156 KB |
Output is correct |
50 |
Correct |
95 ms |
7176 KB |
Output is correct |
51 |
Correct |
38 ms |
7404 KB |
Output is correct |
52 |
Correct |
42 ms |
7372 KB |
Output is correct |
53 |
Correct |
106 ms |
7400 KB |
Output is correct |
54 |
Correct |
44 ms |
7704 KB |
Output is correct |
55 |
Correct |
45 ms |
7636 KB |
Output is correct |
56 |
Correct |
125 ms |
7644 KB |
Output is correct |
57 |
Correct |
47 ms |
7904 KB |
Output is correct |
58 |
Correct |
54 ms |
7876 KB |
Output is correct |
59 |
Correct |
136 ms |
7956 KB |
Output is correct |
60 |
Correct |
54 ms |
8132 KB |
Output is correct |
61 |
Correct |
56 ms |
8136 KB |
Output is correct |
62 |
Correct |
151 ms |
8132 KB |
Output is correct |
63 |
Correct |
144 ms |
8204 KB |
Output is correct |
64 |
Correct |
209 ms |
8140 KB |
Output is correct |
65 |
Correct |
182 ms |
8212 KB |
Output is correct |
66 |
Correct |
170 ms |
8276 KB |
Output is correct |
67 |
Correct |
148 ms |
8136 KB |
Output is correct |
68 |
Correct |
90 ms |
8276 KB |
Output is correct |
69 |
Correct |
89 ms |
8132 KB |
Output is correct |
70 |
Correct |
74 ms |
8132 KB |
Output is correct |
71 |
Correct |
68 ms |
8196 KB |
Output is correct |
72 |
Correct |
71 ms |
8168 KB |
Output is correct |
73 |
Correct |
72 ms |
8388 KB |
Output is correct |
74 |
Correct |
98 ms |
8480 KB |
Output is correct |
75 |
Correct |
105 ms |
8568 KB |
Output is correct |
76 |
Correct |
98 ms |
8472 KB |
Output is correct |
77 |
Correct |
125 ms |
8460 KB |
Output is correct |
78 |
Correct |
110 ms |
8396 KB |
Output is correct |
79 |
Correct |
94 ms |
8388 KB |
Output is correct |
80 |
Correct |
97 ms |
8396 KB |
Output is correct |
81 |
Correct |
112 ms |
8352 KB |
Output is correct |
82 |
Correct |
103 ms |
8540 KB |
Output is correct |
83 |
Correct |
117 ms |
8464 KB |
Output is correct |
84 |
Correct |
106 ms |
8372 KB |
Output is correct |
85 |
Correct |
108 ms |
8388 KB |
Output is correct |
86 |
Correct |
125 ms |
8360 KB |
Output is correct |
87 |
Correct |
112 ms |
8388 KB |
Output is correct |
88 |
Correct |
120 ms |
8260 KB |
Output is correct |
89 |
Correct |
120 ms |
8324 KB |
Output is correct |
90 |
Correct |
153 ms |
8308 KB |
Output is correct |
91 |
Correct |
119 ms |
8352 KB |
Output is correct |
92 |
Correct |
121 ms |
8328 KB |
Output is correct |