#include <bits/stdc++.h>
using namespace std;
const int NMAX = 8e2;
const int VISUAL = 4;
int dl[] = { -1, 0, 1, 0 };
int dc[] = { 0, 1, 0, -1 };
int n, step; int bee_dist[2 + NMAX][2 + NMAX];
char forest[2 + NMAX][2 + NMAX]; int dist[2 + NMAX][2 + NMAX];
pair<int, int> honey, house;
vector<pair<int, int>> bees;
void bee_spread () {
queue<pair<int, int>> q;
for ( auto pos : bees ) {
q.push ( pos );
bee_dist[pos.first][pos.second] = 1;
}
while ( !q.empty () ) {
pair<int, int> start = q.front (); q.pop ();
int line = start.first, column = start.second;
for ( int dir = 0; dir < VISUAL; dir ++ ) {
int new_line = line + dl[dir];
int new_col = column + dc[dir];
if ( forest[new_line][new_col] != 'T' && bee_dist[new_line][new_col] == 0 ) {
if ( forest[new_line][new_col] != 'D' ) {
q.push ( {new_line, new_col} );
bee_dist[new_line][new_col] = bee_dist[line][column] + 1;
}
}
}
}
}
bool validation ( int time, pair<int, int> start ) {
if ( time >= bee_dist[start.first][start.second] - 1 )
return false;
queue<pair<int, int>> q; q.push ( start );
dist[start.first][start.second] = 1;
while ( !q.empty () ) {
start = q.front (); q.pop ();
int line = start.first, column = start.second;
for ( int dir = 0; dir < VISUAL; dir ++ ) {
int new_line = line + dl[dir];
int new_col = column + dc[dir];
if ( new_line == house.first && new_col == house.second )
return true;
if ( (forest[new_line][new_col] == 'D' || forest[new_line][new_col] == 'G') && dist[new_line][new_col] == 0 ) {
dist[new_line][new_col] = dist[line][column] + 1;
int need = ( dist[new_line][new_col] - 1 ) / step;
if ( need + time < bee_dist[new_line][new_col] - 1 )
q.push ( {new_line, new_col} );
}
}
}
return false;
}
void reset_dist () {
for ( int i = 1; i <= n; i ++ )
for ( int j = 1; j <= n; j ++ )
dist[i][j] = 0;
}
void solve () {
int left = 0, right = 2 * n * n; int answer = -1;
while ( left <= right ) {
int time = left + (right - left) / 2;
int status = validation ( time, honey );
if ( status == true ) {
answer = time;
left = time + 1;
}
else
right = time - 1;
reset_dist ();
}
cout << answer;
}
int main()
{
cin >> n >> step; cin.get ();
for ( int line = 1; line <= n; line ++ ) {
for ( int column = 1; column <= n; column ++ ) {
forest[line][column] = cin.get ();
if ( forest[line][column] == 'M' )
honey = {line, column};
else if ( forest[line][column] == 'D' )
house = {line, column};
else if ( forest[line][column] == 'H' )
bees.push_back ( {line, column} );
}
cin.get ();
}
for ( int indx = 0; indx < n + 2; indx ++ ) {
forest[indx][0] = forest[indx][n + 1] = 'T';
forest[0][indx] = forest[n + 1][indx] = 'T';
}
bee_spread ();
solve ();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
83 ms |
6060 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
852 KB |
Output is correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Correct |
8 ms |
2692 KB |
Output is correct |
34 |
Correct |
8 ms |
2740 KB |
Output is correct |
35 |
Correct |
21 ms |
2772 KB |
Output is correct |
36 |
Correct |
10 ms |
3028 KB |
Output is correct |
37 |
Correct |
10 ms |
3028 KB |
Output is correct |
38 |
Correct |
27 ms |
3028 KB |
Output is correct |
39 |
Correct |
12 ms |
3412 KB |
Output is correct |
40 |
Correct |
12 ms |
3496 KB |
Output is correct |
41 |
Correct |
34 ms |
3476 KB |
Output is correct |
42 |
Correct |
14 ms |
3796 KB |
Output is correct |
43 |
Correct |
15 ms |
3796 KB |
Output is correct |
44 |
Correct |
41 ms |
3836 KB |
Output is correct |
45 |
Correct |
19 ms |
4264 KB |
Output is correct |
46 |
Correct |
17 ms |
4172 KB |
Output is correct |
47 |
Correct |
59 ms |
4192 KB |
Output is correct |
48 |
Correct |
22 ms |
4428 KB |
Output is correct |
49 |
Correct |
20 ms |
4476 KB |
Output is correct |
50 |
Correct |
62 ms |
4544 KB |
Output is correct |
51 |
Correct |
24 ms |
4852 KB |
Output is correct |
52 |
Correct |
23 ms |
4864 KB |
Output is correct |
53 |
Correct |
69 ms |
4844 KB |
Output is correct |
54 |
Correct |
30 ms |
5144 KB |
Output is correct |
55 |
Correct |
28 ms |
5232 KB |
Output is correct |
56 |
Correct |
90 ms |
5204 KB |
Output is correct |
57 |
Correct |
32 ms |
5588 KB |
Output is correct |
58 |
Correct |
34 ms |
5588 KB |
Output is correct |
59 |
Correct |
94 ms |
5604 KB |
Output is correct |
60 |
Correct |
35 ms |
5964 KB |
Output is correct |
61 |
Correct |
34 ms |
5896 KB |
Output is correct |
62 |
Correct |
124 ms |
5952 KB |
Output is correct |
63 |
Correct |
110 ms |
5876 KB |
Output is correct |
64 |
Correct |
191 ms |
5852 KB |
Output is correct |
65 |
Correct |
156 ms |
5948 KB |
Output is correct |
66 |
Correct |
122 ms |
5956 KB |
Output is correct |
67 |
Correct |
122 ms |
5948 KB |
Output is correct |
68 |
Correct |
65 ms |
5952 KB |
Output is correct |
69 |
Correct |
63 ms |
5924 KB |
Output is correct |
70 |
Correct |
52 ms |
5868 KB |
Output is correct |
71 |
Correct |
55 ms |
5944 KB |
Output is correct |
72 |
Correct |
44 ms |
5872 KB |
Output is correct |
73 |
Correct |
44 ms |
6244 KB |
Output is correct |
74 |
Correct |
61 ms |
6256 KB |
Output is correct |
75 |
Correct |
79 ms |
6252 KB |
Output is correct |
76 |
Correct |
68 ms |
6192 KB |
Output is correct |
77 |
Correct |
75 ms |
6256 KB |
Output is correct |
78 |
Correct |
78 ms |
6188 KB |
Output is correct |
79 |
Correct |
55 ms |
6152 KB |
Output is correct |
80 |
Correct |
65 ms |
6128 KB |
Output is correct |
81 |
Correct |
69 ms |
6208 KB |
Output is correct |
82 |
Correct |
64 ms |
6104 KB |
Output is correct |
83 |
Correct |
96 ms |
6208 KB |
Output is correct |
84 |
Correct |
76 ms |
6108 KB |
Output is correct |
85 |
Correct |
81 ms |
6140 KB |
Output is correct |
86 |
Correct |
76 ms |
6092 KB |
Output is correct |
87 |
Correct |
77 ms |
6168 KB |
Output is correct |
88 |
Correct |
81 ms |
6120 KB |
Output is correct |
89 |
Correct |
87 ms |
6012 KB |
Output is correct |
90 |
Correct |
91 ms |
6116 KB |
Output is correct |
91 |
Correct |
87 ms |
6092 KB |
Output is correct |
92 |
Correct |
85 ms |
6116 KB |
Output is correct |