#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
using namespace std;
vector<vector<char>> grd(802, vector<char>(802, 'T'));
vector<vector<bool>> swm(802, vector<bool>(802, false));
vector<vector<int>> btime(802, vector<int>(802, 0));
int sttr, sttc;
int dstr, dstc;
int h, s;
vector<int> radd = { -1, 0, 1, 0 };
vector<int> cadd = { 0, -1, 0, 1 };
bool check(int tchk)
{
if (tchk >= btime[sttr][sttc]) return false;
queue<pair<int, pair<int, int>>> bfs;
bfs.push({ 0, {sttr, sttc} });
vector<vector<bool>> poss(h + 2, vector<bool>(h + 2, false));
poss[sttr][sttc] = true;
while (!bfs.empty() && !poss[dstr][dstc])
{
int r = bfs.front().second.first, c = bfs.front().second.second, dst = bfs.front().first;
bfs.pop();
for (int dir = 0; dir < 4; dir++) if (!poss[r + radd[dir]][c + cadd[dir]]) if (grd[r + radd[dir]][c + cadd[dir]] == 'G')
if ((dst + 1) / s < btime[r + radd[dir]][c + cadd[dir]] - tchk)
{
poss[r + radd[dir]][c + cadd[dir]] = true;
bfs.push({ dst + 1, {r + radd[dir], c + cadd[dir]} });
}
}
return poss[dstr][dstc];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> h >> s;
queue<pair<int, int>> swmpcs;
for (int i = 1; i <= h; i++) for (int j = 1; j <= h; j++)
{
cin >> grd[i][j];
if (grd[i][j] == 'M')
{
sttr = i;
sttc = j;
grd[i][j] = 'G';
}
else if (grd[i][j] == 'D')
{
dstr = i;
dstc = j;
grd[i][j] = 'T';
}
else if (grd[i][j] == 'H')
{
swmpcs.push({ i, j });
swm[i][j] = true;
grd[i][j] = 'T';
}
}
while (!swmpcs.empty())
{
int r = swmpcs.front().first, c = swmpcs.front().second;
swmpcs.pop();
for (int dir = 0; dir < 4; dir++) if ((!swm[r + radd[dir]][c + cadd[dir]]) && (grd[r + radd[dir]][c + cadd[dir]] == 'G'))
{
swm[r + radd[dir]][c + cadd[dir]] = true;
btime[r + radd[dir]][c + cadd[dir]] = btime[r][c] + 1;
swmpcs.push({ r + radd[dir], c + cadd[dir] });
}
}
btime[dstr][dstc] = 1e8;
grd[dstr][dstc] = 'G';
if (!check(0)) cout << -1;
else
{
int l = 0, r = 1e8;
while (l < r)
{
int mid = l + (r - l + 1) / 2;
if (check(mid)) l = mid;
else r = mid - 1;
}
cout << l;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3672 KB |
Output is correct |
2 |
Correct |
2 ms |
3676 KB |
Output is correct |
3 |
Correct |
2 ms |
3724 KB |
Output is correct |
4 |
Correct |
2 ms |
3676 KB |
Output is correct |
5 |
Correct |
3 ms |
3672 KB |
Output is correct |
6 |
Correct |
2 ms |
3636 KB |
Output is correct |
7 |
Correct |
68 ms |
4408 KB |
Output is correct |
8 |
Correct |
3 ms |
3676 KB |
Output is correct |
9 |
Correct |
2 ms |
3672 KB |
Output is correct |
10 |
Correct |
2 ms |
3676 KB |
Output is correct |
11 |
Correct |
2 ms |
3676 KB |
Output is correct |
12 |
Correct |
2 ms |
3676 KB |
Output is correct |
13 |
Correct |
2 ms |
3676 KB |
Output is correct |
14 |
Correct |
2 ms |
3676 KB |
Output is correct |
15 |
Correct |
2 ms |
3784 KB |
Output is correct |
16 |
Correct |
2 ms |
3772 KB |
Output is correct |
17 |
Correct |
2 ms |
3676 KB |
Output is correct |
18 |
Correct |
2 ms |
3676 KB |
Output is correct |
19 |
Correct |
2 ms |
3676 KB |
Output is correct |
20 |
Correct |
2 ms |
3784 KB |
Output is correct |
21 |
Correct |
2 ms |
3676 KB |
Output is correct |
22 |
Correct |
2 ms |
3676 KB |
Output is correct |
23 |
Correct |
2 ms |
3676 KB |
Output is correct |
24 |
Correct |
2 ms |
3676 KB |
Output is correct |
25 |
Correct |
2 ms |
3780 KB |
Output is correct |
26 |
Correct |
3 ms |
3676 KB |
Output is correct |
27 |
Correct |
2 ms |
3676 KB |
Output is correct |
28 |
Correct |
2 ms |
3676 KB |
Output is correct |
29 |
Correct |
2 ms |
3676 KB |
Output is correct |
30 |
Correct |
2 ms |
3676 KB |
Output is correct |
31 |
Correct |
2 ms |
3676 KB |
Output is correct |
32 |
Correct |
2 ms |
3672 KB |
Output is correct |
33 |
Correct |
5 ms |
3932 KB |
Output is correct |
34 |
Correct |
5 ms |
3904 KB |
Output is correct |
35 |
Correct |
15 ms |
3932 KB |
Output is correct |
36 |
Correct |
5 ms |
3932 KB |
Output is correct |
37 |
Correct |
5 ms |
3932 KB |
Output is correct |
38 |
Correct |
21 ms |
3996 KB |
Output is correct |
39 |
Correct |
6 ms |
3932 KB |
Output is correct |
40 |
Correct |
6 ms |
3900 KB |
Output is correct |
41 |
Correct |
28 ms |
4036 KB |
Output is correct |
42 |
Correct |
7 ms |
3932 KB |
Output is correct |
43 |
Correct |
7 ms |
3928 KB |
Output is correct |
44 |
Correct |
32 ms |
3928 KB |
Output is correct |
45 |
Correct |
8 ms |
3932 KB |
Output is correct |
46 |
Correct |
8 ms |
4024 KB |
Output is correct |
47 |
Correct |
37 ms |
3932 KB |
Output is correct |
48 |
Correct |
9 ms |
4188 KB |
Output is correct |
49 |
Correct |
9 ms |
4152 KB |
Output is correct |
50 |
Correct |
44 ms |
4224 KB |
Output is correct |
51 |
Correct |
9 ms |
4184 KB |
Output is correct |
52 |
Correct |
10 ms |
4188 KB |
Output is correct |
53 |
Correct |
54 ms |
4188 KB |
Output is correct |
54 |
Correct |
12 ms |
4188 KB |
Output is correct |
55 |
Correct |
12 ms |
4188 KB |
Output is correct |
56 |
Correct |
63 ms |
4344 KB |
Output is correct |
57 |
Correct |
13 ms |
4184 KB |
Output is correct |
58 |
Correct |
13 ms |
4188 KB |
Output is correct |
59 |
Correct |
70 ms |
4400 KB |
Output is correct |
60 |
Correct |
13 ms |
4296 KB |
Output is correct |
61 |
Correct |
13 ms |
4296 KB |
Output is correct |
62 |
Correct |
82 ms |
4184 KB |
Output is correct |
63 |
Correct |
85 ms |
4188 KB |
Output is correct |
64 |
Correct |
115 ms |
4184 KB |
Output is correct |
65 |
Correct |
123 ms |
4184 KB |
Output is correct |
66 |
Correct |
106 ms |
4396 KB |
Output is correct |
67 |
Correct |
32 ms |
4184 KB |
Output is correct |
68 |
Correct |
39 ms |
4188 KB |
Output is correct |
69 |
Correct |
35 ms |
4392 KB |
Output is correct |
70 |
Correct |
34 ms |
4188 KB |
Output is correct |
71 |
Correct |
32 ms |
4184 KB |
Output is correct |
72 |
Correct |
25 ms |
4184 KB |
Output is correct |
73 |
Correct |
28 ms |
4444 KB |
Output is correct |
74 |
Correct |
46 ms |
4444 KB |
Output is correct |
75 |
Correct |
48 ms |
4444 KB |
Output is correct |
76 |
Correct |
54 ms |
4444 KB |
Output is correct |
77 |
Correct |
50 ms |
4440 KB |
Output is correct |
78 |
Correct |
23 ms |
4444 KB |
Output is correct |
79 |
Correct |
45 ms |
4444 KB |
Output is correct |
80 |
Correct |
44 ms |
4440 KB |
Output is correct |
81 |
Correct |
56 ms |
4444 KB |
Output is correct |
82 |
Correct |
43 ms |
4552 KB |
Output is correct |
83 |
Correct |
53 ms |
4444 KB |
Output is correct |
84 |
Correct |
58 ms |
4492 KB |
Output is correct |
85 |
Correct |
55 ms |
4444 KB |
Output is correct |
86 |
Correct |
58 ms |
4440 KB |
Output is correct |
87 |
Correct |
54 ms |
4444 KB |
Output is correct |
88 |
Correct |
54 ms |
4440 KB |
Output is correct |
89 |
Correct |
60 ms |
4440 KB |
Output is correct |
90 |
Correct |
57 ms |
4440 KB |
Output is correct |
91 |
Correct |
59 ms |
4440 KB |
Output is correct |
92 |
Correct |
56 ms |
4444 KB |
Output is correct |