#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
#define pb push_back
#define f first
#define s second
const int MAXN = 1000;
const int INF = 1e9;
int unuse[MAXN][MAXN];
int reach[MAXN][MAXN];
const int dI[] = {-1, 0, 1, 0};
const int dJ[] = {0, -1, 0, 1};
int N, S;
pi st, goal;
bool reachGoal(int delay) {
if (delay == -1) return true;
for (int i = 0; i <= N + 1; i++) {
for (int j = 0; j <= N + 1; j++) {
reach[i][j] = INF;
}
}
reach[st.f][st.s] = delay * S;
queue<pi> q;
q.push(st);
while (q.size()) {
pi cur = q.front();
q.pop();
if (reach[cur.f][cur.s] < unuse[cur.f][cur.s] * S) {
for (int d = 0; d < 4; d++) {
int nI = cur.f + dI[d];
int nJ = cur.s + dJ[d];
if (reach[nI][nJ] == INF) {
reach[nI][nJ] = reach[cur.f][cur.s] + 1;
q.push({nI, nJ});
}
}
}
}
return reach[goal.f][goal.s] != INF;
}
int binSearch() {
int low = -1;
int high = 800 * 800;
while (low != high) {
int mid = (low + high + 1) / 2;
if (reachGoal(mid)) {
low = mid;
} else {
high = mid - 1;
}
}
return low;
}
int main() {
cin >> N >> S;
for (int i = 0; i <= N + 1; i++) {
for (int j = 0; j <= N + 1; j++) {
unuse[i][j] = -1;
}
}
for (int i = 0; i <= N + 1; i++) {
unuse[0][i] = 0;
unuse[N + 1][i] = 0;
unuse[i][0] = 0;
unuse[i][N + 1] = 0;
}
queue<pi> q;
for (int i = 1; i <= N; i++) {
string line;
cin >> line;
for (int j = 1; j <= N; j++) {
char cur = line[j - 1];
if (cur == 'T') {
unuse[i][j] = 0;
} else if (cur == 'H') {
unuse[i][j] = 0;
q.push({i, j});
} else if (cur == 'M') {
st = {i, j};
} else if (cur == 'D') {
goal = {i, j};
unuse[i][j] = INF;
} else {
assert(cur == 'G');
}
}
}
while (!q.empty()) {
pi cur = q.front();
q.pop();
for (int d = 0; d < 4; d++) {
int nI = cur.f + dI[d];
int nJ = cur.s + dJ[d];
if (unuse[nI][nJ] == -1) {
unuse[nI][nJ] = unuse[cur.f][cur.s] + 1;
q.push({nI, nJ});
}
}
}
int ans = binSearch();
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
368 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
126 ms |
7288 KB |
Output is correct |
8 |
Correct |
3 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
888 KB |
Output is correct |
13 |
Correct |
3 ms |
760 KB |
Output is correct |
14 |
Correct |
3 ms |
888 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
3 ms |
504 KB |
Output is correct |
17 |
Correct |
3 ms |
504 KB |
Output is correct |
18 |
Correct |
2 ms |
504 KB |
Output is correct |
19 |
Correct |
3 ms |
504 KB |
Output is correct |
20 |
Correct |
3 ms |
632 KB |
Output is correct |
21 |
Correct |
3 ms |
632 KB |
Output is correct |
22 |
Correct |
2 ms |
632 KB |
Output is correct |
23 |
Correct |
3 ms |
760 KB |
Output is correct |
24 |
Correct |
3 ms |
760 KB |
Output is correct |
25 |
Correct |
3 ms |
760 KB |
Output is correct |
26 |
Correct |
3 ms |
760 KB |
Output is correct |
27 |
Correct |
3 ms |
888 KB |
Output is correct |
28 |
Correct |
3 ms |
888 KB |
Output is correct |
29 |
Correct |
3 ms |
888 KB |
Output is correct |
30 |
Correct |
3 ms |
888 KB |
Output is correct |
31 |
Correct |
4 ms |
888 KB |
Output is correct |
32 |
Correct |
4 ms |
1016 KB |
Output is correct |
33 |
Correct |
16 ms |
3192 KB |
Output is correct |
34 |
Correct |
15 ms |
3192 KB |
Output is correct |
35 |
Correct |
34 ms |
3252 KB |
Output is correct |
36 |
Correct |
19 ms |
3624 KB |
Output is correct |
37 |
Correct |
20 ms |
3576 KB |
Output is correct |
38 |
Correct |
53 ms |
3768 KB |
Output is correct |
39 |
Correct |
26 ms |
4088 KB |
Output is correct |
40 |
Correct |
24 ms |
4088 KB |
Output is correct |
41 |
Correct |
68 ms |
4088 KB |
Output is correct |
42 |
Correct |
24 ms |
4512 KB |
Output is correct |
43 |
Correct |
29 ms |
4544 KB |
Output is correct |
44 |
Correct |
66 ms |
4512 KB |
Output is correct |
45 |
Correct |
34 ms |
5020 KB |
Output is correct |
46 |
Correct |
36 ms |
4984 KB |
Output is correct |
47 |
Correct |
103 ms |
4984 KB |
Output is correct |
48 |
Correct |
38 ms |
5368 KB |
Output is correct |
49 |
Correct |
61 ms |
5500 KB |
Output is correct |
50 |
Correct |
101 ms |
5468 KB |
Output is correct |
51 |
Correct |
41 ms |
5880 KB |
Output is correct |
52 |
Correct |
43 ms |
5864 KB |
Output is correct |
53 |
Correct |
130 ms |
5976 KB |
Output is correct |
54 |
Correct |
49 ms |
6392 KB |
Output is correct |
55 |
Correct |
53 ms |
6264 KB |
Output is correct |
56 |
Correct |
153 ms |
6392 KB |
Output is correct |
57 |
Correct |
54 ms |
6756 KB |
Output is correct |
58 |
Correct |
58 ms |
6844 KB |
Output is correct |
59 |
Correct |
175 ms |
6908 KB |
Output is correct |
60 |
Correct |
62 ms |
7288 KB |
Output is correct |
61 |
Correct |
68 ms |
7416 KB |
Output is correct |
62 |
Correct |
201 ms |
7288 KB |
Output is correct |
63 |
Correct |
154 ms |
7288 KB |
Output is correct |
64 |
Correct |
290 ms |
7416 KB |
Output is correct |
65 |
Correct |
223 ms |
7308 KB |
Output is correct |
66 |
Correct |
186 ms |
7288 KB |
Output is correct |
67 |
Correct |
181 ms |
7372 KB |
Output is correct |
68 |
Correct |
106 ms |
7408 KB |
Output is correct |
69 |
Correct |
103 ms |
7340 KB |
Output is correct |
70 |
Correct |
105 ms |
7288 KB |
Output is correct |
71 |
Correct |
69 ms |
7292 KB |
Output is correct |
72 |
Correct |
69 ms |
7208 KB |
Output is correct |
73 |
Correct |
73 ms |
7544 KB |
Output is correct |
74 |
Correct |
95 ms |
7628 KB |
Output is correct |
75 |
Correct |
111 ms |
7604 KB |
Output is correct |
76 |
Correct |
103 ms |
7648 KB |
Output is correct |
77 |
Correct |
106 ms |
7672 KB |
Output is correct |
78 |
Correct |
119 ms |
7416 KB |
Output is correct |
79 |
Correct |
104 ms |
7544 KB |
Output is correct |
80 |
Correct |
118 ms |
7608 KB |
Output is correct |
81 |
Correct |
107 ms |
7504 KB |
Output is correct |
82 |
Correct |
100 ms |
7540 KB |
Output is correct |
83 |
Correct |
120 ms |
7584 KB |
Output is correct |
84 |
Correct |
107 ms |
7416 KB |
Output is correct |
85 |
Correct |
113 ms |
7480 KB |
Output is correct |
86 |
Correct |
117 ms |
7568 KB |
Output is correct |
87 |
Correct |
131 ms |
7516 KB |
Output is correct |
88 |
Correct |
140 ms |
7548 KB |
Output is correct |
89 |
Correct |
137 ms |
7544 KB |
Output is correct |
90 |
Correct |
126 ms |
7516 KB |
Output is correct |
91 |
Correct |
119 ms |
7432 KB |
Output is correct |
92 |
Correct |
133 ms |
7452 KB |
Output is correct |