# include <bits/stdc++.h>
# define ll long long
# define fi first
# define se second
# define pb push_back
# define pf push_front
# define For(i, a, b) for( int i = a; i < b; i++ )
# define in insert
# define all(a) a.begin(),a.end()
# define pi pair < int, int >
# define DEBUG
# define readfile(file) freopen ( (file + ".in").c_str(), "r", stdin)
# define writefile(file) freopen ( (file + ".out").c_str(), "w", stdout)
# define speed ios_base::sync_with_stdio(false);cin.tie(NULL)
# define LARGE 1000
# define SET(file) readfile(file);writefile(file);
using namespace std;
int ax[4] = { 1, -1, 0, 0 };
int ay[4] = { 0, 0, 1, -1 };
char mainMap[LARGE][LARGE];
bool visited[LARGE][LARGE];
int beeDistance[LARGE][LARGE];
int n, s;
int dx, dy;
int mx, my;
bool valid( int i, int j )
{
if ( i >= 0 && i < n && j >= 0 && j < n && mainMap[i][j] != 'T' )
return 1;
return 0;
}
bool test( int delay )
{
if ( delay * s >= beeDistance[mx][my] )
return false;
memset(visited, 0, sizeof(visited));
queue < pair < int, pi > > q;
q.push({delay * s, {mx, my}});
visited[mx][my] = true;
while (!q.empty())
{
int distance = q.front().fi;
int x = q.front().se.fi;
int y = q.front().se.se;
q.pop();
if ( mainMap[x][y] == 'D' )
return true;
For ( i, 0, 4)
{
int nx = x + ax[i];
int ny = y + ay[i];
if (nx < 0 || nx >= n || ny < 0 || ny >= n || mainMap[nx][ny] == 'T' || (distance + 1) >= beeDistance[nx][ny] || visited[nx][ny])
continue;
q.push({distance + 1, {nx, ny}});
visited[nx][ny] = true;
}
}
return false;
}
int main()
{
/// Author: _Dilshod_
speed;
cin >> n >> s;
queue < pi > bq;
memset(beeDistance, -1, sizeof(beeDistance));
For ( i, 0, n )
{
For ( j, 0, n )
{
cin >> mainMap[i][j];
if ( mainMap[i][j] == 'D' )
{
dx = i;
dy = j;
} else if ( mainMap[i][j] == 'H' )
{
bq.push({i, j});
beeDistance[i][j] = 0;
} else if ( mainMap[i][j] == 'M' )
{
mx = i;
my = j;
mainMap[i][j] = 'G';
}
}
}
while( !bq.empty() )
{
int x = bq.front().fi;
int y = bq.front().se;
bq.pop();
For ( i, 0, 4 )
{
int nx = x + ax[i];
int ny = y + ay[i];
if (nx < 0 || nx >= n || ny < 0 || ny >= n || mainMap[nx][ny] != 'G' || beeDistance[nx][ny] != -1)
continue;
beeDistance[nx][ny] = beeDistance[x][y] + s;
bq.push({nx, ny});
}
}
beeDistance[dx][dy] = n * n * s;
int l = -1, r = 2 * n * n, m;
while (r - l > 1 )
{
m = (l + r) >> 1;
if ( test(m) ) l = m;
else r = m;
}
cout << l;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5240 KB |
Output is correct |
2 |
Correct |
6 ms |
5240 KB |
Output is correct |
3 |
Correct |
6 ms |
5240 KB |
Output is correct |
4 |
Correct |
6 ms |
5240 KB |
Output is correct |
5 |
Correct |
6 ms |
5240 KB |
Output is correct |
6 |
Correct |
6 ms |
5240 KB |
Output is correct |
7 |
Correct |
112 ms |
6136 KB |
Output is correct |
8 |
Correct |
6 ms |
5240 KB |
Output is correct |
9 |
Correct |
6 ms |
5240 KB |
Output is correct |
10 |
Correct |
6 ms |
5240 KB |
Output is correct |
11 |
Correct |
6 ms |
5240 KB |
Output is correct |
12 |
Correct |
6 ms |
5368 KB |
Output is correct |
13 |
Correct |
6 ms |
5240 KB |
Output is correct |
14 |
Correct |
7 ms |
5372 KB |
Output is correct |
15 |
Correct |
6 ms |
5212 KB |
Output is correct |
16 |
Correct |
6 ms |
5240 KB |
Output is correct |
17 |
Correct |
7 ms |
5240 KB |
Output is correct |
18 |
Correct |
7 ms |
5240 KB |
Output is correct |
19 |
Correct |
6 ms |
5240 KB |
Output is correct |
20 |
Correct |
6 ms |
5240 KB |
Output is correct |
21 |
Correct |
6 ms |
5308 KB |
Output is correct |
22 |
Correct |
6 ms |
5240 KB |
Output is correct |
23 |
Correct |
6 ms |
5288 KB |
Output is correct |
24 |
Correct |
8 ms |
5240 KB |
Output is correct |
25 |
Correct |
6 ms |
5240 KB |
Output is correct |
26 |
Correct |
7 ms |
5368 KB |
Output is correct |
27 |
Correct |
6 ms |
5240 KB |
Output is correct |
28 |
Correct |
7 ms |
5240 KB |
Output is correct |
29 |
Correct |
7 ms |
5368 KB |
Output is correct |
30 |
Correct |
7 ms |
5368 KB |
Output is correct |
31 |
Correct |
7 ms |
5240 KB |
Output is correct |
32 |
Correct |
7 ms |
5244 KB |
Output is correct |
33 |
Correct |
12 ms |
5496 KB |
Output is correct |
34 |
Correct |
12 ms |
5624 KB |
Output is correct |
35 |
Correct |
29 ms |
5624 KB |
Output is correct |
36 |
Correct |
11 ms |
5624 KB |
Output is correct |
37 |
Correct |
12 ms |
5624 KB |
Output is correct |
38 |
Correct |
37 ms |
5624 KB |
Output is correct |
39 |
Correct |
13 ms |
5752 KB |
Output is correct |
40 |
Correct |
13 ms |
5752 KB |
Output is correct |
41 |
Correct |
47 ms |
5728 KB |
Output is correct |
42 |
Correct |
14 ms |
5752 KB |
Output is correct |
43 |
Correct |
14 ms |
5752 KB |
Output is correct |
44 |
Correct |
59 ms |
5752 KB |
Output is correct |
45 |
Correct |
15 ms |
5752 KB |
Output is correct |
46 |
Correct |
15 ms |
5752 KB |
Output is correct |
47 |
Correct |
70 ms |
5752 KB |
Output is correct |
48 |
Correct |
17 ms |
5752 KB |
Output is correct |
49 |
Correct |
17 ms |
5752 KB |
Output is correct |
50 |
Correct |
83 ms |
5880 KB |
Output is correct |
51 |
Correct |
19 ms |
5876 KB |
Output is correct |
52 |
Correct |
20 ms |
5880 KB |
Output is correct |
53 |
Correct |
107 ms |
6008 KB |
Output is correct |
54 |
Correct |
21 ms |
5880 KB |
Output is correct |
55 |
Correct |
21 ms |
5880 KB |
Output is correct |
56 |
Correct |
125 ms |
5980 KB |
Output is correct |
57 |
Correct |
23 ms |
6008 KB |
Output is correct |
58 |
Correct |
23 ms |
5880 KB |
Output is correct |
59 |
Correct |
138 ms |
6136 KB |
Output is correct |
60 |
Correct |
25 ms |
6008 KB |
Output is correct |
61 |
Correct |
25 ms |
6008 KB |
Output is correct |
62 |
Correct |
161 ms |
6140 KB |
Output is correct |
63 |
Correct |
127 ms |
6008 KB |
Output is correct |
64 |
Correct |
266 ms |
6136 KB |
Output is correct |
65 |
Correct |
185 ms |
6124 KB |
Output is correct |
66 |
Correct |
151 ms |
6064 KB |
Output is correct |
67 |
Correct |
149 ms |
6060 KB |
Output is correct |
68 |
Correct |
60 ms |
6008 KB |
Output is correct |
69 |
Correct |
59 ms |
6008 KB |
Output is correct |
70 |
Correct |
49 ms |
6040 KB |
Output is correct |
71 |
Correct |
54 ms |
6008 KB |
Output is correct |
72 |
Correct |
46 ms |
6096 KB |
Output is correct |
73 |
Correct |
45 ms |
6264 KB |
Output is correct |
74 |
Correct |
76 ms |
6260 KB |
Output is correct |
75 |
Correct |
89 ms |
6264 KB |
Output is correct |
76 |
Correct |
82 ms |
6356 KB |
Output is correct |
77 |
Correct |
80 ms |
6264 KB |
Output is correct |
78 |
Correct |
102 ms |
6264 KB |
Output is correct |
79 |
Correct |
66 ms |
6236 KB |
Output is correct |
80 |
Correct |
78 ms |
6264 KB |
Output is correct |
81 |
Correct |
91 ms |
6264 KB |
Output is correct |
82 |
Correct |
79 ms |
6392 KB |
Output is correct |
83 |
Correct |
101 ms |
6264 KB |
Output is correct |
84 |
Correct |
97 ms |
6272 KB |
Output is correct |
85 |
Correct |
104 ms |
6268 KB |
Output is correct |
86 |
Correct |
99 ms |
6264 KB |
Output is correct |
87 |
Correct |
104 ms |
6264 KB |
Output is correct |
88 |
Correct |
110 ms |
6116 KB |
Output is correct |
89 |
Correct |
111 ms |
6136 KB |
Output is correct |
90 |
Correct |
109 ms |
6184 KB |
Output is correct |
91 |
Correct |
110 ms |
6316 KB |
Output is correct |
92 |
Correct |
121 ms |
6136 KB |
Output is correct |