#include <bits/stdc++.h>
using namespace std;
#define N 805
#define fi first
#define se second
typedef pair<int, int> ii;
int n, d[N][N], d1[N][N], xs, ys, xe, ye, s;
vector<ii> o;
char a[N][N];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
bool in(int u, int v){
return u >= 1 && u <= n && v >= 1 && v <= n && a[u][v] != 'T';
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
queue<ii> q;
cin >> n >> s;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
d1[i][j] = d[i][j] = 1e9;
}
}
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
cin >> a[i][j];
if (a[i][j] == 'H') {
d1[i][j] = 0;
q.push({i, j});
}
if (a[i][j] == 'D'){
xe = i;
ye = j;
}
if (a[i][j] == 'M'){
xs = i;
ys = j;
}
}
}
while (!q.empty()){
int u = q.front().fi;
int v = q.front().se;
q.pop();
for (int i = 0; i <= 3; i++){
int newu = u + dx[i];
int newv = v + dy[i];
if (!in(newu, newv)) continue;
if (newu == xe && newv == ye) continue;
if (d1[newu][newv] > d1[u][v] + 1){
d1[newu][newv] = d1[u][v] + 1;
q.push({newu, newv});
}
}
}
int l = 0, r = n * n, rt = -1;
while (l <= r){
int mid = (l + r) >> 1;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++) d[i][j] = 1e9;
}
d[xs][ys] = 0;
q.push({xs, ys});
while (!q.empty()){
int u = q.front().fi;
int v = q.front().se;
q.pop();
for (int i = 0; i <= 3; i++){
int newu = u + dx[i];
int newv = v + dy[i];
if (!in(newu, newv)) continue;
int newd = mid + (d[u][v] + 1) / s;
if (d1[newu][newv] <= newd) continue;
if (d[newu][newv] > d[u][v] + 1){
d[newu][newv] = d[u][v] + 1;
q.push({newu, newv});
}
}
}
if (d1[xs][ys] <= mid) d[xe][ye] = 1e9;
if (d[xe][ye] != 1e9) {rt = mid; l = mid + 1;}
else r = mid - 1;
}
cout << rt;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
74 ms |
6236 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4564 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
4 ms |
4700 KB |
Output is correct |
34 |
Correct |
4 ms |
4744 KB |
Output is correct |
35 |
Correct |
17 ms |
4700 KB |
Output is correct |
36 |
Correct |
5 ms |
4700 KB |
Output is correct |
37 |
Correct |
8 ms |
4896 KB |
Output is correct |
38 |
Correct |
23 ms |
4856 KB |
Output is correct |
39 |
Correct |
7 ms |
4956 KB |
Output is correct |
40 |
Correct |
7 ms |
4956 KB |
Output is correct |
41 |
Correct |
32 ms |
5056 KB |
Output is correct |
42 |
Correct |
8 ms |
4956 KB |
Output is correct |
43 |
Correct |
8 ms |
4956 KB |
Output is correct |
44 |
Correct |
38 ms |
4956 KB |
Output is correct |
45 |
Correct |
10 ms |
5212 KB |
Output is correct |
46 |
Correct |
10 ms |
5364 KB |
Output is correct |
47 |
Correct |
42 ms |
5212 KB |
Output is correct |
48 |
Correct |
11 ms |
5468 KB |
Output is correct |
49 |
Correct |
12 ms |
5468 KB |
Output is correct |
50 |
Correct |
51 ms |
5508 KB |
Output is correct |
51 |
Correct |
13 ms |
5468 KB |
Output is correct |
52 |
Correct |
14 ms |
5676 KB |
Output is correct |
53 |
Correct |
65 ms |
5680 KB |
Output is correct |
54 |
Correct |
15 ms |
5724 KB |
Output is correct |
55 |
Correct |
16 ms |
5724 KB |
Output is correct |
56 |
Correct |
75 ms |
5724 KB |
Output is correct |
57 |
Correct |
18 ms |
5980 KB |
Output is correct |
58 |
Correct |
18 ms |
5976 KB |
Output is correct |
59 |
Correct |
87 ms |
5980 KB |
Output is correct |
60 |
Correct |
20 ms |
5980 KB |
Output is correct |
61 |
Correct |
20 ms |
5980 KB |
Output is correct |
62 |
Correct |
98 ms |
6136 KB |
Output is correct |
63 |
Correct |
80 ms |
5980 KB |
Output is correct |
64 |
Correct |
150 ms |
5976 KB |
Output is correct |
65 |
Correct |
112 ms |
5976 KB |
Output is correct |
66 |
Correct |
91 ms |
6140 KB |
Output is correct |
67 |
Correct |
87 ms |
6100 KB |
Output is correct |
68 |
Correct |
41 ms |
5980 KB |
Output is correct |
69 |
Correct |
48 ms |
5980 KB |
Output is correct |
70 |
Correct |
31 ms |
5976 KB |
Output is correct |
71 |
Correct |
35 ms |
5976 KB |
Output is correct |
72 |
Correct |
28 ms |
6184 KB |
Output is correct |
73 |
Correct |
35 ms |
6236 KB |
Output is correct |
74 |
Correct |
50 ms |
6236 KB |
Output is correct |
75 |
Correct |
62 ms |
6232 KB |
Output is correct |
76 |
Correct |
54 ms |
6236 KB |
Output is correct |
77 |
Correct |
53 ms |
6232 KB |
Output is correct |
78 |
Correct |
67 ms |
6236 KB |
Output is correct |
79 |
Correct |
41 ms |
6236 KB |
Output is correct |
80 |
Correct |
63 ms |
6388 KB |
Output is correct |
81 |
Correct |
60 ms |
6232 KB |
Output is correct |
82 |
Correct |
53 ms |
6236 KB |
Output is correct |
83 |
Correct |
70 ms |
6236 KB |
Output is correct |
84 |
Correct |
58 ms |
6232 KB |
Output is correct |
85 |
Correct |
65 ms |
6232 KB |
Output is correct |
86 |
Correct |
64 ms |
6232 KB |
Output is correct |
87 |
Correct |
63 ms |
6108 KB |
Output is correct |
88 |
Correct |
68 ms |
6296 KB |
Output is correct |
89 |
Correct |
66 ms |
6296 KB |
Output is correct |
90 |
Correct |
71 ms |
6232 KB |
Output is correct |
91 |
Correct |
69 ms |
6236 KB |
Output is correct |
92 |
Correct |
75 ms |
6236 KB |
Output is correct |