#include <bits/stdc++.h>
#define int long long
using namespace std;
char arr[801][801];
int n, s;
int stx, sty, ex, ey;
bool in(int x, int y)
{
return (min(x, y) >= 1 && max(x, y) <= n);
}
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int dBee[801][801];
int D[801][801];
bool check(int mid)
{
memset(D, 0x3f, sizeof D);
D[stx][sty] = mid * s;
queue<pair<int, int>> q;
q.emplace(stx, sty);
while (!q.empty())
{
int ux, uy;
tie(ux, uy) = q.front();
q.pop();
if (D[ux][uy] >= dBee[ux][uy]) continue;
for (int i = 0; i < 4; i++)
{
int vx = ux + dx[i];
int vy = uy + dy[i];
if (in(vx, vy) && arr[vx][vy] != 'T' && D[vx][vy] > D[ux][uy] + 1)
{
D[vx][vy] = D[ux][uy] + 1;
q.emplace(vx, vy);
}
}
}
if (D[ex][ey] < (int)1e18) return true;
else return false;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("TEST.IN", "r", stdin);
//freopen("TEST.OUT", "w", stdout);
cin >> n >> s;
queue<pair<int, int>> q;
memset(dBee, 0x3f, sizeof dBee);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> arr[i][j];
if (arr[i][j] == 'H')
{
q.emplace(i, j);
dBee[i][j] = 0;
}
if (arr[i][j] == 'M')
{
tie(stx, sty) = make_pair(i, j);
}
if (arr[i][j] == 'D')
{
tie(ex, ey) = make_pair(i, j);
}
}
}
while (!q.empty())
{
int ux, uy;
tie(ux, uy) = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int vx = ux + dx[i];
int vy = uy + dy[i];
if (in(vx, vy) && arr[vx][vy] != 'D' && dBee[vx][vy] > dBee[ux][uy] + s)
{
dBee[vx][vy] = dBee[ux][uy] + s;
q.emplace(vx, vy);
}
}
}
int l = 0;
int r = 1e9;
int ans = -1;
while (l <= r)
{
int mid = (l + r) / 2;
if (check(mid))
{
ans = mid;
l = mid + 1;
}
else r = mid - 1;
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
10844 KB |
Output is correct |
2 |
Correct |
4 ms |
10844 KB |
Output is correct |
3 |
Correct |
4 ms |
10844 KB |
Output is correct |
4 |
Incorrect |
6 ms |
10844 KB |
Output isn't correct |
5 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
6 |
Correct |
4 ms |
10844 KB |
Output is correct |
7 |
Correct |
62 ms |
11096 KB |
Output is correct |
8 |
Correct |
5 ms |
10840 KB |
Output is correct |
9 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
10 |
Correct |
4 ms |
10844 KB |
Output is correct |
11 |
Correct |
4 ms |
10840 KB |
Output is correct |
12 |
Incorrect |
4 ms |
10840 KB |
Output isn't correct |
13 |
Correct |
4 ms |
10840 KB |
Output is correct |
14 |
Correct |
4 ms |
10840 KB |
Output is correct |
15 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
16 |
Incorrect |
4 ms |
11004 KB |
Output isn't correct |
17 |
Incorrect |
5 ms |
10840 KB |
Output isn't correct |
18 |
Incorrect |
5 ms |
10844 KB |
Output isn't correct |
19 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
20 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
21 |
Incorrect |
5 ms |
10896 KB |
Output isn't correct |
22 |
Incorrect |
5 ms |
10844 KB |
Output isn't correct |
23 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
24 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
25 |
Incorrect |
5 ms |
10844 KB |
Output isn't correct |
26 |
Incorrect |
5 ms |
10844 KB |
Output isn't correct |
27 |
Incorrect |
5 ms |
10844 KB |
Output isn't correct |
28 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
29 |
Incorrect |
5 ms |
10844 KB |
Output isn't correct |
30 |
Incorrect |
4 ms |
10844 KB |
Output isn't correct |
31 |
Incorrect |
5 ms |
10844 KB |
Output isn't correct |
32 |
Incorrect |
4 ms |
10896 KB |
Output isn't correct |
33 |
Incorrect |
7 ms |
10844 KB |
Output isn't correct |
34 |
Incorrect |
8 ms |
10844 KB |
Output isn't correct |
35 |
Correct |
19 ms |
10844 KB |
Output is correct |
36 |
Incorrect |
10 ms |
10900 KB |
Output isn't correct |
37 |
Incorrect |
9 ms |
10844 KB |
Output isn't correct |
38 |
Correct |
25 ms |
10728 KB |
Output is correct |
39 |
Incorrect |
10 ms |
10844 KB |
Output isn't correct |
40 |
Incorrect |
11 ms |
10724 KB |
Output isn't correct |
41 |
Correct |
29 ms |
10840 KB |
Output is correct |
42 |
Incorrect |
11 ms |
10844 KB |
Output isn't correct |
43 |
Incorrect |
11 ms |
10716 KB |
Output isn't correct |
44 |
Correct |
36 ms |
10892 KB |
Output is correct |
45 |
Incorrect |
16 ms |
10844 KB |
Output isn't correct |
46 |
Incorrect |
14 ms |
10716 KB |
Output isn't correct |
47 |
Correct |
55 ms |
10772 KB |
Output is correct |
48 |
Incorrect |
15 ms |
10844 KB |
Output isn't correct |
49 |
Incorrect |
14 ms |
10844 KB |
Output isn't correct |
50 |
Correct |
52 ms |
10976 KB |
Output is correct |
51 |
Incorrect |
15 ms |
10844 KB |
Output isn't correct |
52 |
Incorrect |
17 ms |
10840 KB |
Output isn't correct |
53 |
Correct |
58 ms |
11008 KB |
Output is correct |
54 |
Incorrect |
16 ms |
11068 KB |
Output isn't correct |
55 |
Incorrect |
20 ms |
10844 KB |
Output isn't correct |
56 |
Correct |
84 ms |
11048 KB |
Output is correct |
57 |
Incorrect |
18 ms |
11100 KB |
Output isn't correct |
58 |
Incorrect |
20 ms |
10936 KB |
Output isn't correct |
59 |
Correct |
75 ms |
11096 KB |
Output is correct |
60 |
Incorrect |
22 ms |
10960 KB |
Output isn't correct |
61 |
Incorrect |
22 ms |
11100 KB |
Output isn't correct |
62 |
Correct |
114 ms |
11132 KB |
Output is correct |
63 |
Incorrect |
21 ms |
11104 KB |
Output isn't correct |
64 |
Incorrect |
97 ms |
11128 KB |
Output isn't correct |
65 |
Incorrect |
76 ms |
11128 KB |
Output isn't correct |
66 |
Incorrect |
21 ms |
11100 KB |
Output isn't correct |
67 |
Correct |
37 ms |
11136 KB |
Output is correct |
68 |
Incorrect |
25 ms |
11356 KB |
Output isn't correct |
69 |
Incorrect |
41 ms |
11268 KB |
Output isn't correct |
70 |
Incorrect |
30 ms |
11272 KB |
Output isn't correct |
71 |
Incorrect |
24 ms |
11348 KB |
Output isn't correct |
72 |
Incorrect |
24 ms |
11352 KB |
Output isn't correct |
73 |
Correct |
30 ms |
11612 KB |
Output is correct |
74 |
Correct |
47 ms |
11608 KB |
Output is correct |
75 |
Correct |
63 ms |
11860 KB |
Output is correct |
76 |
Correct |
51 ms |
11608 KB |
Output is correct |
77 |
Correct |
54 ms |
11672 KB |
Output is correct |
78 |
Correct |
54 ms |
11616 KB |
Output is correct |
79 |
Correct |
46 ms |
11612 KB |
Output is correct |
80 |
Correct |
45 ms |
11612 KB |
Output is correct |
81 |
Correct |
51 ms |
11612 KB |
Output is correct |
82 |
Correct |
43 ms |
11616 KB |
Output is correct |
83 |
Correct |
60 ms |
11356 KB |
Output is correct |
84 |
Correct |
52 ms |
11356 KB |
Output is correct |
85 |
Correct |
51 ms |
11536 KB |
Output is correct |
86 |
Correct |
62 ms |
11540 KB |
Output is correct |
87 |
Correct |
57 ms |
11352 KB |
Output is correct |
88 |
Correct |
59 ms |
11356 KB |
Output is correct |
89 |
Correct |
60 ms |
11432 KB |
Output is correct |
90 |
Correct |
64 ms |
11440 KB |
Output is correct |
91 |
Correct |
63 ms |
11440 KB |
Output is correct |
92 |
Correct |
59 ms |
11352 KB |
Output is correct |