#include <iostream>
#include <queue>
#define MAX 802
using namespace std;
struct bee
{
short int x;
short int y;
int t;
int r;
};
int N, S, sx, sy, ex, ey, B[MAX][MAX], vis[MAX][MAX], l, r, mid, res, cur;
bool xd;
char A[MAX][MAX];
queue <bee> qiu;
bee temp;
void solve ()
{
while (!qiu.empty())
{
temp = qiu.front();
qiu.pop();
temp.t++;
if (B[temp.x - 1][temp.y] == -1 && A[temp.x - 1][temp.y] != 'T' && A[temp.x - 1][temp.y] != 'D')
{
B[temp.x - 1][temp.y] = temp.t;
temp.x--;
qiu.push(temp);
temp.x++;
}
if (B[temp.x + 1][temp.y] == -1 && A[temp.x + 1][temp.y] != 'T' && A[temp.x + 1][temp.y] != 'D')
{
B[temp.x + 1][temp.y] = temp.t;
temp.x++;
qiu.push(temp);
temp.x--;
}
if (B[temp.x][temp.y - 1] == -1 && A[temp.x][temp.y - 1] != 'T' && A[temp.x][temp.y - 1] != 'D')
{
B[temp.x][temp.y - 1] = temp.t;
temp.y--;
qiu.push(temp);
temp.y++;
}
if (B[temp.x][temp.y + 1] == -1 && A[temp.x][temp.y + 1] != 'T' && A[temp.x][temp.y + 1] != 'D')
{
B[temp.x][temp.y + 1] = temp.t;
temp.y++;
qiu.push(temp);
temp.y--;
}
}
}
bool solve2(int x, int y, int t, int r)
{
if (r == -1)
return 1;
temp.x = x;
temp.y = y;
temp.t = t;
temp.r = r;
qiu.push(temp);
while (!qiu.empty())
{
temp = qiu.front();
qiu.pop();
//cout << temp.x << " " << temp.y << " " << temp.t << " " << temp.r << endl;
if (temp.x == ex && temp.y == ey)
return 1;
if (temp.r + (temp.t / S) >= B[temp.x][temp.y])
continue;
temp.t++;
if (vis[temp.x + 1][temp.y] < cur && A[temp.x + 1][temp.y] != 'T')
{
vis[temp.x + 1][temp.y] = cur;
temp.x++;
qiu.push(temp);
temp.x--;
}
if (vis[temp.x - 1][temp.y] < cur && A[temp.x - 1][temp.y] != 'T')
{
vis[temp.x - 1][temp.y] = cur;
temp.x--;
qiu.push(temp);
temp.x++;
}
if (vis[temp.x][temp.y + 1] < cur && A[temp.x][temp.y + 1] != 'T')
{
vis[temp.x][temp.y + 1] = cur;
temp.y++;
qiu.push(temp);
temp.y--;
}
if (vis[temp.x][temp.y - 1] < cur && A[temp.x][temp.y - 1] != 'T')
{
vis[temp.x][temp.y - 1] = cur;
temp.y--;
qiu.push(temp);
temp.y++;
}
}
return 0;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> S;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
{
cin >> A[i][j];
vis[i][j] = -1;
B[i][j] = -1;
if (A[i][j] == 'M')
{
sx = i;
sy = j;
}
if (A[i][j] == 'D')
{
ex = i;
ey = j;
}
if (A[i][j] == 'H')
{
temp.x = i;
temp.y = j;
temp.t = 0;
B[i][j] = 0;
qiu.push(temp);
}
}
solve();
l = -1;
r = 650000;
while (l <= r)
{
mid = (l + r) / 2;
cur++;
vis[sx][sy] = cur;
while (!qiu.empty())
qiu.pop();
xd = solve2(sx, sy, 0, mid);
if (xd == 1)
{
res = mid;
l = mid + 1;
}
else
r = mid - 1;
}
cout << res;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
77 ms |
6784 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
768 KB |
Output is correct |
13 |
Correct |
1 ms |
768 KB |
Output is correct |
14 |
Correct |
1 ms |
768 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
1 ms |
640 KB |
Output is correct |
22 |
Correct |
1 ms |
640 KB |
Output is correct |
23 |
Correct |
1 ms |
768 KB |
Output is correct |
24 |
Correct |
1 ms |
768 KB |
Output is correct |
25 |
Correct |
1 ms |
768 KB |
Output is correct |
26 |
Correct |
1 ms |
768 KB |
Output is correct |
27 |
Correct |
1 ms |
768 KB |
Output is correct |
28 |
Correct |
1 ms |
768 KB |
Output is correct |
29 |
Correct |
1 ms |
896 KB |
Output is correct |
30 |
Correct |
1 ms |
896 KB |
Output is correct |
31 |
Correct |
1 ms |
896 KB |
Output is correct |
32 |
Correct |
1 ms |
896 KB |
Output is correct |
33 |
Correct |
5 ms |
2944 KB |
Output is correct |
34 |
Correct |
5 ms |
2976 KB |
Output is correct |
35 |
Correct |
17 ms |
2944 KB |
Output is correct |
36 |
Correct |
6 ms |
3328 KB |
Output is correct |
37 |
Correct |
6 ms |
3328 KB |
Output is correct |
38 |
Correct |
23 ms |
3328 KB |
Output is correct |
39 |
Correct |
8 ms |
3712 KB |
Output is correct |
40 |
Correct |
7 ms |
3760 KB |
Output is correct |
41 |
Correct |
28 ms |
3712 KB |
Output is correct |
42 |
Correct |
9 ms |
4096 KB |
Output is correct |
43 |
Correct |
8 ms |
4096 KB |
Output is correct |
44 |
Correct |
37 ms |
4096 KB |
Output is correct |
45 |
Correct |
11 ms |
4480 KB |
Output is correct |
46 |
Correct |
10 ms |
4480 KB |
Output is correct |
47 |
Correct |
43 ms |
4480 KB |
Output is correct |
48 |
Correct |
13 ms |
5024 KB |
Output is correct |
49 |
Correct |
11 ms |
4992 KB |
Output is correct |
50 |
Correct |
54 ms |
5112 KB |
Output is correct |
51 |
Correct |
14 ms |
5376 KB |
Output is correct |
52 |
Correct |
15 ms |
5504 KB |
Output is correct |
53 |
Correct |
71 ms |
5376 KB |
Output is correct |
54 |
Correct |
17 ms |
5760 KB |
Output is correct |
55 |
Correct |
15 ms |
5760 KB |
Output is correct |
56 |
Correct |
83 ms |
5760 KB |
Output is correct |
57 |
Correct |
18 ms |
6144 KB |
Output is correct |
58 |
Correct |
19 ms |
6144 KB |
Output is correct |
59 |
Correct |
94 ms |
6264 KB |
Output is correct |
60 |
Correct |
20 ms |
6656 KB |
Output is correct |
61 |
Correct |
19 ms |
6656 KB |
Output is correct |
62 |
Correct |
108 ms |
6656 KB |
Output is correct |
63 |
Correct |
100 ms |
6656 KB |
Output is correct |
64 |
Correct |
170 ms |
6776 KB |
Output is correct |
65 |
Correct |
151 ms |
6656 KB |
Output is correct |
66 |
Correct |
128 ms |
6684 KB |
Output is correct |
67 |
Correct |
102 ms |
6776 KB |
Output is correct |
68 |
Correct |
51 ms |
6832 KB |
Output is correct |
69 |
Correct |
50 ms |
6656 KB |
Output is correct |
70 |
Correct |
39 ms |
6656 KB |
Output is correct |
71 |
Correct |
34 ms |
6656 KB |
Output is correct |
72 |
Correct |
31 ms |
6856 KB |
Output is correct |
73 |
Correct |
36 ms |
7040 KB |
Output is correct |
74 |
Correct |
51 ms |
7040 KB |
Output is correct |
75 |
Correct |
60 ms |
7032 KB |
Output is correct |
76 |
Correct |
51 ms |
7040 KB |
Output is correct |
77 |
Correct |
58 ms |
7040 KB |
Output is correct |
78 |
Correct |
57 ms |
7040 KB |
Output is correct |
79 |
Correct |
43 ms |
7040 KB |
Output is correct |
80 |
Correct |
51 ms |
7040 KB |
Output is correct |
81 |
Correct |
57 ms |
7040 KB |
Output is correct |
82 |
Correct |
51 ms |
7040 KB |
Output is correct |
83 |
Correct |
65 ms |
6912 KB |
Output is correct |
84 |
Correct |
59 ms |
6912 KB |
Output is correct |
85 |
Correct |
67 ms |
6912 KB |
Output is correct |
86 |
Correct |
74 ms |
7024 KB |
Output is correct |
87 |
Correct |
66 ms |
6976 KB |
Output is correct |
88 |
Correct |
67 ms |
6912 KB |
Output is correct |
89 |
Correct |
67 ms |
6912 KB |
Output is correct |
90 |
Correct |
71 ms |
6912 KB |
Output is correct |
91 |
Correct |
75 ms |
6912 KB |
Output is correct |
92 |
Correct |
86 ms |
6912 KB |
Output is correct |