#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] != 'G' || 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 |
Incorrect |
5 ms |
4300 KB |
Output isn't correct |
2 |
Incorrect |
6 ms |
4300 KB |
Output isn't correct |
3 |
Incorrect |
6 ms |
4300 KB |
Output isn't correct |
4 |
Incorrect |
5 ms |
4300 KB |
Output isn't correct |
5 |
Incorrect |
5 ms |
4300 KB |
Output isn't correct |
6 |
Incorrect |
5 ms |
4300 KB |
Output isn't correct |
7 |
Incorrect |
63 ms |
8260 KB |
Output isn't correct |
8 |
Correct |
6 ms |
4300 KB |
Output is correct |
9 |
Incorrect |
5 ms |
4300 KB |
Output isn't correct |
10 |
Incorrect |
5 ms |
4300 KB |
Output isn't correct |
11 |
Incorrect |
5 ms |
4344 KB |
Output isn't correct |
12 |
Incorrect |
5 ms |
4556 KB |
Output isn't correct |
13 |
Incorrect |
5 ms |
4428 KB |
Output isn't correct |
14 |
Correct |
5 ms |
4488 KB |
Output is correct |
15 |
Incorrect |
5 ms |
4300 KB |
Output isn't correct |
16 |
Incorrect |
5 ms |
4372 KB |
Output isn't correct |
17 |
Incorrect |
5 ms |
4300 KB |
Output isn't correct |
18 |
Incorrect |
5 ms |
4300 KB |
Output isn't correct |
19 |
Incorrect |
5 ms |
4316 KB |
Output isn't correct |
20 |
Incorrect |
5 ms |
4300 KB |
Output isn't correct |
21 |
Incorrect |
6 ms |
4428 KB |
Output isn't correct |
22 |
Incorrect |
7 ms |
4428 KB |
Output isn't correct |
23 |
Incorrect |
5 ms |
4428 KB |
Output isn't correct |
24 |
Incorrect |
7 ms |
4428 KB |
Output isn't correct |
25 |
Incorrect |
5 ms |
4556 KB |
Output isn't correct |
26 |
Incorrect |
6 ms |
4556 KB |
Output isn't correct |
27 |
Incorrect |
6 ms |
4556 KB |
Output isn't correct |
28 |
Incorrect |
6 ms |
4556 KB |
Output isn't correct |
29 |
Incorrect |
6 ms |
4556 KB |
Output isn't correct |
30 |
Incorrect |
6 ms |
4620 KB |
Output isn't correct |
31 |
Incorrect |
6 ms |
4556 KB |
Output isn't correct |
32 |
Incorrect |
6 ms |
4556 KB |
Output isn't correct |
33 |
Incorrect |
14 ms |
5908 KB |
Output isn't correct |
34 |
Incorrect |
14 ms |
5964 KB |
Output isn't correct |
35 |
Incorrect |
16 ms |
5936 KB |
Output isn't correct |
36 |
Incorrect |
17 ms |
6228 KB |
Output isn't correct |
37 |
Incorrect |
17 ms |
6216 KB |
Output isn't correct |
38 |
Incorrect |
19 ms |
6244 KB |
Output isn't correct |
39 |
Incorrect |
21 ms |
6476 KB |
Output isn't correct |
40 |
Incorrect |
20 ms |
6484 KB |
Output isn't correct |
41 |
Incorrect |
22 ms |
6492 KB |
Output isn't correct |
42 |
Incorrect |
24 ms |
6636 KB |
Output isn't correct |
43 |
Incorrect |
26 ms |
6688 KB |
Output isn't correct |
44 |
Incorrect |
26 ms |
6664 KB |
Output isn't correct |
45 |
Incorrect |
28 ms |
6944 KB |
Output isn't correct |
46 |
Incorrect |
27 ms |
6864 KB |
Output isn't correct |
47 |
Incorrect |
31 ms |
6980 KB |
Output isn't correct |
48 |
Incorrect |
31 ms |
7108 KB |
Output isn't correct |
49 |
Incorrect |
37 ms |
7220 KB |
Output isn't correct |
50 |
Incorrect |
35 ms |
7104 KB |
Output isn't correct |
51 |
Incorrect |
36 ms |
7428 KB |
Output isn't correct |
52 |
Incorrect |
43 ms |
7376 KB |
Output isn't correct |
53 |
Incorrect |
41 ms |
7448 KB |
Output isn't correct |
54 |
Incorrect |
41 ms |
7660 KB |
Output isn't correct |
55 |
Incorrect |
40 ms |
7624 KB |
Output isn't correct |
56 |
Incorrect |
46 ms |
7616 KB |
Output isn't correct |
57 |
Incorrect |
47 ms |
7844 KB |
Output isn't correct |
58 |
Incorrect |
52 ms |
7868 KB |
Output isn't correct |
59 |
Incorrect |
52 ms |
7872 KB |
Output isn't correct |
60 |
Incorrect |
52 ms |
8192 KB |
Output isn't correct |
61 |
Incorrect |
51 ms |
8080 KB |
Output isn't correct |
62 |
Incorrect |
58 ms |
8124 KB |
Output isn't correct |
63 |
Incorrect |
74 ms |
8088 KB |
Output isn't correct |
64 |
Incorrect |
64 ms |
8272 KB |
Output isn't correct |
65 |
Incorrect |
62 ms |
8196 KB |
Output isn't correct |
66 |
Incorrect |
63 ms |
8196 KB |
Output isn't correct |
67 |
Correct |
63 ms |
8200 KB |
Output is correct |
68 |
Incorrect |
64 ms |
8220 KB |
Output isn't correct |
69 |
Incorrect |
64 ms |
8136 KB |
Output isn't correct |
70 |
Incorrect |
64 ms |
8132 KB |
Output isn't correct |
71 |
Incorrect |
66 ms |
8180 KB |
Output isn't correct |
72 |
Incorrect |
65 ms |
8184 KB |
Output isn't correct |
73 |
Incorrect |
64 ms |
8360 KB |
Output isn't correct |
74 |
Incorrect |
63 ms |
8388 KB |
Output isn't correct |
75 |
Incorrect |
64 ms |
8368 KB |
Output isn't correct |
76 |
Incorrect |
66 ms |
8460 KB |
Output isn't correct |
77 |
Incorrect |
77 ms |
8388 KB |
Output isn't correct |
78 |
Correct |
65 ms |
8388 KB |
Output is correct |
79 |
Incorrect |
64 ms |
8528 KB |
Output isn't correct |
80 |
Incorrect |
63 ms |
8416 KB |
Output isn't correct |
81 |
Incorrect |
63 ms |
8408 KB |
Output isn't correct |
82 |
Incorrect |
76 ms |
8360 KB |
Output isn't correct |
83 |
Incorrect |
64 ms |
8336 KB |
Output isn't correct |
84 |
Incorrect |
66 ms |
8348 KB |
Output isn't correct |
85 |
Incorrect |
63 ms |
8348 KB |
Output isn't correct |
86 |
Incorrect |
75 ms |
8380 KB |
Output isn't correct |
87 |
Incorrect |
63 ms |
8304 KB |
Output isn't correct |
88 |
Incorrect |
65 ms |
8364 KB |
Output isn't correct |
89 |
Incorrect |
62 ms |
8288 KB |
Output isn't correct |
90 |
Incorrect |
67 ms |
8396 KB |
Output isn't correct |
91 |
Incorrect |
68 ms |
8348 KB |
Output isn't correct |
92 |
Incorrect |
74 ms |
8308 KB |
Output isn't correct |