#include <bits/stdc++.h>
#define forr(i, a, b) for (int i = (a); i <= (b); i++)
#define ford(i, a, b) for (int i = (a); i >= (b); i--)
#define forf(i, a, b) for (int i = (a); i < (b); i++)
#define fi first
#define se second
#define pb push_back
#define all(v) v.begin(), v.end()
#define ll long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
#define mask(i) (1LL << (i))
#define bit(x, i) (((x) >> (i)) & 1)
#define bp __builtin_popcountll
#define file "test"
using namespace std;
const int base = 31;
const ll mod = 1e9 + 7;
const ll oo = 1e18;
const int N = 805;
char a[N][N];
int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int M[N][N], H[N][N], d[N][N], dd[N][N], xm, ym, xd, yd, n, S;
bool check(int t){
queue<pii> q;
forr(i, 1, n) forr(j, 1, n) dd[i][j] = false;
if (H[xm][ym] <= t) return false;
q.push({xm, ym});
dd[xm][ym] = true;
while (!q.empty()){
pii u = q.front();
q.pop();
forr(i, 0, 3){
int nx = u.fi + dx[i], ny = u.se + dy[i];
if (1 <= nx && nx <= n && 1 <= ny && ny <= n && a[nx][ny] != 'T' && !dd[nx][ny]){
if ((M[u.fi][u.se] + 1) / S + t < H[nx][ny]){
dd[nx][ny] = true;
q.push({nx, ny});
}
}
}
}
forr(i, 0, 3){
int x = xd + dx[i], y = yd + dy[i];
if (1 <= x && x <= n && 1 <= y && y <= n && M[x][y]/S + t < H[x][y] && dd[x][y]) return true;
}
return false;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> S;
forr(i, 1, n) forr(j, 1, n) {
cin >> a[i][j];
if (a[i][j] == 'M') xm = i, ym = j;
if (a[i][j] == 'D') xd = i, yd = j;
}
queue<pii> q;
d[xm][ym] = 1;
q.push({xm, ym});
while (!q.empty()){
pii u = q.front();
q.pop();
forr(i, 0, 3){
int nx = u.fi + dx[i], ny = u.se + dy[i];
if (1 <= nx && nx <= n && 1 <= ny && ny <= n && a[nx][ny] != 'T' && !d[nx][ny]){
d[nx][ny] = d[u.fi][u.se] + 1;
q.push({nx, ny});
}
}
}
forr(i, 1, n) forr(j, 1, n) M[i][j] = d[i][j] - 1;
forr(i, 1, n) forr(j, 1, n) if (a[i][j] == 'H') q.push({i, j}), H[i][j] = 1;
while (!q.empty()){
pii u = q.front();
q.pop();
forr(i, 0, 3){
int nx = u.fi + dx[i], ny = u.se + dy[i];
if (1 <= nx && nx <= n && 1 <= ny && ny <= n && a[nx][ny] != 'T' && !H[nx][ny]){
H[nx][ny] = H[u.fi][u.se] + 1;
q.push({nx, ny});
}
}
}
forr(i, 1, n) forr(j, 1, n) if (H[i][j]) H[i][j]--;
int l = 0, r = n*n, ans = -1;
while (l <= r){
int mid = (l + r) >> 1;
if (check(mid)){
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
75 ms |
12060 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
632 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
0 ms |
720 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
856 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
1116 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
1 ms |
1372 KB |
Output is correct |
27 |
Correct |
1 ms |
1372 KB |
Output is correct |
28 |
Correct |
1 ms |
1372 KB |
Output is correct |
29 |
Correct |
1 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
1372 KB |
Output is correct |
31 |
Correct |
1 ms |
1372 KB |
Output is correct |
32 |
Correct |
1 ms |
1372 KB |
Output is correct |
33 |
Correct |
6 ms |
5208 KB |
Output is correct |
34 |
Correct |
6 ms |
5208 KB |
Output is correct |
35 |
Incorrect |
18 ms |
5236 KB |
Output isn't correct |
36 |
Correct |
8 ms |
5980 KB |
Output is correct |
37 |
Correct |
8 ms |
5952 KB |
Output is correct |
38 |
Incorrect |
22 ms |
5980 KB |
Output isn't correct |
39 |
Correct |
10 ms |
6624 KB |
Output is correct |
40 |
Correct |
10 ms |
6492 KB |
Output is correct |
41 |
Incorrect |
28 ms |
6700 KB |
Output isn't correct |
42 |
Correct |
11 ms |
7256 KB |
Output is correct |
43 |
Correct |
12 ms |
7512 KB |
Output is correct |
44 |
Incorrect |
43 ms |
7260 KB |
Output isn't correct |
45 |
Correct |
12 ms |
7904 KB |
Output is correct |
46 |
Correct |
13 ms |
8028 KB |
Output is correct |
47 |
Incorrect |
39 ms |
8028 KB |
Output isn't correct |
48 |
Correct |
14 ms |
8796 KB |
Output is correct |
49 |
Correct |
14 ms |
8808 KB |
Output is correct |
50 |
Incorrect |
53 ms |
8796 KB |
Output isn't correct |
51 |
Correct |
16 ms |
9560 KB |
Output is correct |
52 |
Correct |
16 ms |
9564 KB |
Output is correct |
53 |
Incorrect |
52 ms |
9588 KB |
Output isn't correct |
54 |
Correct |
19 ms |
10140 KB |
Output is correct |
55 |
Correct |
18 ms |
10332 KB |
Output is correct |
56 |
Incorrect |
65 ms |
10324 KB |
Output isn't correct |
57 |
Correct |
21 ms |
10836 KB |
Output is correct |
58 |
Correct |
21 ms |
10840 KB |
Output is correct |
59 |
Incorrect |
74 ms |
10908 KB |
Output isn't correct |
60 |
Correct |
23 ms |
11612 KB |
Output is correct |
61 |
Correct |
23 ms |
11748 KB |
Output is correct |
62 |
Incorrect |
84 ms |
11804 KB |
Output isn't correct |
63 |
Correct |
86 ms |
11612 KB |
Output is correct |
64 |
Correct |
155 ms |
11860 KB |
Output is correct |
65 |
Correct |
119 ms |
11600 KB |
Output is correct |
66 |
Correct |
97 ms |
11604 KB |
Output is correct |
67 |
Correct |
96 ms |
11800 KB |
Output is correct |
68 |
Correct |
52 ms |
11604 KB |
Output is correct |
69 |
Correct |
51 ms |
11604 KB |
Output is correct |
70 |
Correct |
42 ms |
11600 KB |
Output is correct |
71 |
Correct |
45 ms |
11600 KB |
Output is correct |
72 |
Correct |
46 ms |
11832 KB |
Output is correct |
73 |
Correct |
41 ms |
11928 KB |
Output is correct |
74 |
Correct |
48 ms |
11904 KB |
Output is correct |
75 |
Correct |
54 ms |
11860 KB |
Output is correct |
76 |
Correct |
49 ms |
11860 KB |
Output is correct |
77 |
Correct |
50 ms |
11864 KB |
Output is correct |
78 |
Correct |
58 ms |
11860 KB |
Output is correct |
79 |
Correct |
45 ms |
11856 KB |
Output is correct |
80 |
Correct |
53 ms |
12044 KB |
Output is correct |
81 |
Correct |
56 ms |
11856 KB |
Output is correct |
82 |
Correct |
50 ms |
11956 KB |
Output is correct |
83 |
Correct |
64 ms |
11856 KB |
Output is correct |
84 |
Correct |
57 ms |
12008 KB |
Output is correct |
85 |
Correct |
60 ms |
11856 KB |
Output is correct |
86 |
Correct |
59 ms |
11872 KB |
Output is correct |
87 |
Correct |
75 ms |
11788 KB |
Output is correct |
88 |
Correct |
72 ms |
11860 KB |
Output is correct |
89 |
Correct |
70 ms |
11768 KB |
Output is correct |
90 |
Correct |
64 ms |
11892 KB |
Output is correct |
91 |
Correct |
67 ms |
11956 KB |
Output is correct |
92 |
Correct |
80 ms |
11772 KB |
Output is correct |