#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> > tq;
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') tq.push({i, j}), d[i][j] = s;
if(a[i][j] == 'D') en = {i, j};
if(a[i][j] == 'M') st = {i, j};
}
}
while(!tq.empty()){
int x = tq.front().S;
int y = tq.front().F;
tq.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;
tq.push({ny, nx});
}
}
d[en.F][en.S] = 1e9;
// for(int i = 1; i <= n; ++i){
// for(int j = 1; j <= n; ++j) cout << d[i][j] << " ";
// cout << endl;
// }
// cout << endl;
queue < pair < pair <int,int>, int > > q;
int l = 0, r = 1e4;
while(l < r){
memset(dist, 0, sizeof dist);
int mid = (l + r) / 2;
bool check = 0;
q.push({st, 1e9});
dist[st.F][st.S] = 1;
while(!q.empty()){
int x = q.front().F.S;
int y = q.front().F.F;
int cost = q.front().S;
q.pop();
// if(mid == 1) cout << x << " " << y << " " << cost << " " << dist[y][x] << endl;
if(y == en.F && x == en.S){
check = 1;
continue;
}
for(int i = 0; i < 4; ++i){
int nx = x + dx[i];
int ny = y + dy[i];
int ncost = min(cost, (d[ny][nx] - s - dist[y][x]) / s);
if(a[ny][nx] == 'H' || a[ny][nx] == 'T' || ncost < mid || dist[ny][nx] || ny > n || nx > n || ny < 1 || nx < 1) continue;
dist[ny][nx] = dist[y][x] + 1;
q.push({{ny, nx}, ncost});
}
}
// if(mid == 4) for(int i = 1; i <= n; ++i){
// for(int j = 1; j <= n; ++j) cout << dist[i][j] << " ";
// cout << endl;
// }
if(check) 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 |
4 ms |
4300 KB |
Output isn't correct |
3 |
Incorrect |
4 ms |
4300 KB |
Output isn't correct |
4 |
Incorrect |
4 ms |
4300 KB |
Output isn't correct |
5 |
Correct |
4 ms |
4300 KB |
Output is correct |
6 |
Correct |
4 ms |
4300 KB |
Output is correct |
7 |
Correct |
186 ms |
9000 KB |
Output is correct |
8 |
Incorrect |
4 ms |
4300 KB |
Output isn't correct |
9 |
Correct |
5 ms |
4428 KB |
Output is correct |
10 |
Correct |
4 ms |
4300 KB |
Output is correct |
11 |
Correct |
5 ms |
4300 KB |
Output is correct |
12 |
Incorrect |
5 ms |
4520 KB |
Output isn't correct |
13 |
Incorrect |
5 ms |
4428 KB |
Output isn't correct |
14 |
Correct |
5 ms |
4556 KB |
Output is correct |
15 |
Incorrect |
4 ms |
4300 KB |
Output isn't correct |
16 |
Correct |
4 ms |
4300 KB |
Output is correct |
17 |
Incorrect |
4 ms |
4392 KB |
Output isn't correct |
18 |
Correct |
4 ms |
4368 KB |
Output is correct |
19 |
Incorrect |
4 ms |
4428 KB |
Output isn't correct |
20 |
Correct |
5 ms |
4396 KB |
Output is correct |
21 |
Incorrect |
5 ms |
4428 KB |
Output isn't correct |
22 |
Correct |
5 ms |
4520 KB |
Output is correct |
23 |
Incorrect |
5 ms |
4428 KB |
Output isn't correct |
24 |
Correct |
5 ms |
4428 KB |
Output is correct |
25 |
Incorrect |
5 ms |
4556 KB |
Output isn't correct |
26 |
Correct |
5 ms |
4556 KB |
Output is correct |
27 |
Incorrect |
5 ms |
4556 KB |
Output isn't correct |
28 |
Correct |
5 ms |
4684 KB |
Output is correct |
29 |
Incorrect |
5 ms |
4556 KB |
Output isn't correct |
30 |
Correct |
5 ms |
4556 KB |
Output is correct |
31 |
Incorrect |
5 ms |
4556 KB |
Output isn't correct |
32 |
Correct |
6 ms |
4556 KB |
Output is correct |
33 |
Incorrect |
29 ms |
6120 KB |
Output isn't correct |
34 |
Incorrect |
34 ms |
6100 KB |
Output isn't correct |
35 |
Correct |
47 ms |
6016 KB |
Output is correct |
36 |
Incorrect |
36 ms |
6348 KB |
Output isn't correct |
37 |
Incorrect |
48 ms |
6384 KB |
Output isn't correct |
38 |
Correct |
65 ms |
6400 KB |
Output is correct |
39 |
Incorrect |
47 ms |
6604 KB |
Output isn't correct |
40 |
Incorrect |
75 ms |
6692 KB |
Output isn't correct |
41 |
Correct |
80 ms |
6688 KB |
Output is correct |
42 |
Incorrect |
57 ms |
6864 KB |
Output isn't correct |
43 |
Incorrect |
77 ms |
7020 KB |
Output isn't correct |
44 |
Correct |
95 ms |
6988 KB |
Output is correct |
45 |
Incorrect |
73 ms |
7260 KB |
Output isn't correct |
46 |
Incorrect |
116 ms |
7264 KB |
Output isn't correct |
47 |
Correct |
114 ms |
7212 KB |
Output is correct |
48 |
Incorrect |
81 ms |
7576 KB |
Output isn't correct |
49 |
Incorrect |
109 ms |
7572 KB |
Output isn't correct |
50 |
Correct |
133 ms |
7580 KB |
Output is correct |
51 |
Incorrect |
96 ms |
7812 KB |
Output isn't correct |
52 |
Incorrect |
143 ms |
7876 KB |
Output isn't correct |
53 |
Correct |
169 ms |
7984 KB |
Output is correct |
54 |
Incorrect |
105 ms |
8176 KB |
Output isn't correct |
55 |
Incorrect |
165 ms |
8080 KB |
Output isn't correct |
56 |
Correct |
198 ms |
8264 KB |
Output is correct |
57 |
Incorrect |
128 ms |
8500 KB |
Output isn't correct |
58 |
Incorrect |
201 ms |
8388 KB |
Output isn't correct |
59 |
Correct |
225 ms |
8472 KB |
Output is correct |
60 |
Incorrect |
157 ms |
8824 KB |
Output isn't correct |
61 |
Incorrect |
203 ms |
8776 KB |
Output isn't correct |
62 |
Correct |
266 ms |
8836 KB |
Output is correct |
63 |
Correct |
179 ms |
8832 KB |
Output is correct |
64 |
Correct |
253 ms |
8828 KB |
Output is correct |
65 |
Correct |
238 ms |
8772 KB |
Output is correct |
66 |
Incorrect |
192 ms |
8900 KB |
Output isn't correct |
67 |
Correct |
182 ms |
8864 KB |
Output is correct |
68 |
Correct |
90 ms |
8860 KB |
Output is correct |
69 |
Correct |
81 ms |
8812 KB |
Output is correct |
70 |
Correct |
78 ms |
8780 KB |
Output is correct |
71 |
Correct |
73 ms |
8772 KB |
Output is correct |
72 |
Incorrect |
67 ms |
8944 KB |
Output isn't correct |
73 |
Incorrect |
74 ms |
9008 KB |
Output isn't correct |
74 |
Correct |
113 ms |
9104 KB |
Output is correct |
75 |
Correct |
138 ms |
9004 KB |
Output is correct |
76 |
Correct |
132 ms |
9028 KB |
Output is correct |
77 |
Correct |
152 ms |
9092 KB |
Output is correct |
78 |
Correct |
161 ms |
9016 KB |
Output is correct |
79 |
Correct |
100 ms |
8960 KB |
Output is correct |
80 |
Correct |
126 ms |
9016 KB |
Output is correct |
81 |
Correct |
146 ms |
9072 KB |
Output is correct |
82 |
Correct |
121 ms |
8944 KB |
Output is correct |
83 |
Correct |
167 ms |
9028 KB |
Output is correct |
84 |
Correct |
131 ms |
9032 KB |
Output is correct |
85 |
Correct |
138 ms |
9024 KB |
Output is correct |
86 |
Correct |
141 ms |
9036 KB |
Output is correct |
87 |
Correct |
142 ms |
9004 KB |
Output is correct |
88 |
Correct |
165 ms |
9076 KB |
Output is correct |
89 |
Correct |
157 ms |
9136 KB |
Output is correct |
90 |
Correct |
172 ms |
8936 KB |
Output is correct |
91 |
Correct |
168 ms |
8980 KB |
Output is correct |
92 |
Correct |
181 ms |
8980 KB |
Output is correct |