#include <iostream>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
#define int long long
#define pi pair<int, int>
#define f first
#define s second
const int N = 805;
const int INF = 1e10;
vector<vector<char>> grid(N, vector<char>(N));
vector<vector<int>> vis(N, vector<int>(N, INF));
vector<vector<int>> v(N, vector<int>(N, INF));
vector<pi> moves = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
vector<pi> bees;
pi mecho;
pi loc;
int n, m;
int steps;
bool ok(int i, int j, int val)
{
return (i >= 0 && i < n && j >= 0 && j < m && vis[i][j] > val && grid[i][j] != 'T');
}
bool ok2(int i, int j, int val)
{
return (i >= 0 && i < n && j >= 0 && j < m && v[i][j] > val && grid[i][j] != 'T');
}
void bee_bfs()
{
queue<pi> q;
for (auto p : bees)
{
vis[p.f][p.s] = 0;
q.push(p);
}
while (!q.empty())
{
auto p = q.front();
q.pop();
for (auto mv : moves)
{
int val = vis[p.f][p.s] + 1;
if (ok(p.f + mv.f, p.s + mv.s, val))
{
vis[p.f + mv.f][p.s + mv.s] = val;
q.push({p.f + mv.f, p.s + mv.s});
}
}
}
}
void mecho_bfs()
{
queue<pi> q;
v[mecho.f][mecho.s] = 0;
q.push(mecho);
while (!q.empty())
{
auto p = q.front();
q.pop();
for (auto mv : moves)
{
int val = v[p.f][p.s] + 1;
if (ok2(p.f + mv.f, p.s + mv.s, val))
{
v[p.f + mv.f][p.s + mv.s] = val;
q.push({p.f + mv.f, p.s + mv.s});
}
}
}
}
bool issoke(int t)
{
return (((v[loc.f][loc.s]) / steps) + t) < (vis[loc.f][loc.s]);
}
signed main()
{
cin >> n >> steps;
m = n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> grid[i][j];
if (grid[i][j] == 'M')
{
mecho.f = i;
mecho.s = j;
}
else if (grid[i][j] == 'H')
{
bees.push_back({i, j});
}
else if (grid[i][j] == 'D')
{
loc.f = i;
loc.s = j;
}
}
}
bee_bfs();
mecho_bfs();
if (!issoke(0))
{
cout << -1 << '\n';
return 0;
}
int l = 0;
int r = 1;
while (issoke(r))
{
r *= 2;
}
while (l + 1 < r)
{
int m = (l + r) / 2;
if (issoke(m))
{
l = m;
}
else
{
r = m;
}
}
cout << l - 1 << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
11296 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
11100 KB |
Output isn't correct |
3 |
Incorrect |
6 ms |
11100 KB |
Output isn't correct |
4 |
Incorrect |
6 ms |
11100 KB |
Output isn't correct |
5 |
Incorrect |
6 ms |
11352 KB |
Output isn't correct |
6 |
Incorrect |
6 ms |
11100 KB |
Output isn't correct |
7 |
Incorrect |
65 ms |
11324 KB |
Output isn't correct |
8 |
Correct |
5 ms |
11096 KB |
Output is correct |
9 |
Incorrect |
6 ms |
11100 KB |
Output isn't correct |
10 |
Incorrect |
6 ms |
11076 KB |
Output isn't correct |
11 |
Incorrect |
5 ms |
11096 KB |
Output isn't correct |
12 |
Incorrect |
6 ms |
11096 KB |
Output isn't correct |
13 |
Incorrect |
6 ms |
11128 KB |
Output isn't correct |
14 |
Correct |
6 ms |
11096 KB |
Output is correct |
15 |
Incorrect |
6 ms |
11100 KB |
Output isn't correct |
16 |
Correct |
6 ms |
11100 KB |
Output is correct |
17 |
Incorrect |
6 ms |
11100 KB |
Output isn't correct |
18 |
Correct |
6 ms |
11100 KB |
Output is correct |
19 |
Incorrect |
6 ms |
11100 KB |
Output isn't correct |
20 |
Correct |
6 ms |
11100 KB |
Output is correct |
21 |
Incorrect |
6 ms |
11100 KB |
Output isn't correct |
22 |
Correct |
5 ms |
11296 KB |
Output is correct |
23 |
Incorrect |
6 ms |
11100 KB |
Output isn't correct |
24 |
Correct |
7 ms |
11064 KB |
Output is correct |
25 |
Incorrect |
6 ms |
11100 KB |
Output isn't correct |
26 |
Correct |
6 ms |
11096 KB |
Output is correct |
27 |
Incorrect |
6 ms |
11232 KB |
Output isn't correct |
28 |
Correct |
6 ms |
11096 KB |
Output is correct |
29 |
Incorrect |
6 ms |
11096 KB |
Output isn't correct |
30 |
Correct |
6 ms |
11104 KB |
Output is correct |
31 |
Incorrect |
7 ms |
11100 KB |
Output isn't correct |
32 |
Correct |
6 ms |
11100 KB |
Output is correct |
33 |
Incorrect |
12 ms |
11184 KB |
Output isn't correct |
34 |
Correct |
12 ms |
11332 KB |
Output is correct |
35 |
Incorrect |
15 ms |
11352 KB |
Output isn't correct |
36 |
Incorrect |
14 ms |
11356 KB |
Output isn't correct |
37 |
Correct |
14 ms |
11356 KB |
Output is correct |
38 |
Incorrect |
18 ms |
11352 KB |
Output isn't correct |
39 |
Incorrect |
17 ms |
11356 KB |
Output isn't correct |
40 |
Correct |
16 ms |
11444 KB |
Output is correct |
41 |
Incorrect |
20 ms |
11356 KB |
Output isn't correct |
42 |
Incorrect |
18 ms |
11356 KB |
Output isn't correct |
43 |
Correct |
18 ms |
11488 KB |
Output is correct |
44 |
Incorrect |
25 ms |
11356 KB |
Output isn't correct |
45 |
Incorrect |
22 ms |
11352 KB |
Output isn't correct |
46 |
Correct |
20 ms |
11356 KB |
Output is correct |
47 |
Incorrect |
26 ms |
11356 KB |
Output isn't correct |
48 |
Incorrect |
24 ms |
11468 KB |
Output isn't correct |
49 |
Correct |
24 ms |
11608 KB |
Output is correct |
50 |
Incorrect |
32 ms |
11352 KB |
Output isn't correct |
51 |
Incorrect |
28 ms |
11352 KB |
Output isn't correct |
52 |
Correct |
29 ms |
11352 KB |
Output is correct |
53 |
Incorrect |
37 ms |
11512 KB |
Output isn't correct |
54 |
Incorrect |
31 ms |
11356 KB |
Output isn't correct |
55 |
Correct |
29 ms |
11612 KB |
Output is correct |
56 |
Incorrect |
44 ms |
11604 KB |
Output isn't correct |
57 |
Incorrect |
36 ms |
11356 KB |
Output isn't correct |
58 |
Correct |
33 ms |
11600 KB |
Output is correct |
59 |
Incorrect |
46 ms |
11644 KB |
Output isn't correct |
60 |
Incorrect |
38 ms |
11604 KB |
Output isn't correct |
61 |
Correct |
37 ms |
11604 KB |
Output is correct |
62 |
Incorrect |
59 ms |
11604 KB |
Output isn't correct |
63 |
Incorrect |
54 ms |
11608 KB |
Output isn't correct |
64 |
Incorrect |
60 ms |
11488 KB |
Output isn't correct |
65 |
Incorrect |
57 ms |
11612 KB |
Output isn't correct |
66 |
Incorrect |
65 ms |
11600 KB |
Output isn't correct |
67 |
Incorrect |
56 ms |
11612 KB |
Output isn't correct |
68 |
Incorrect |
56 ms |
11776 KB |
Output isn't correct |
69 |
Incorrect |
56 ms |
11724 KB |
Output isn't correct |
70 |
Incorrect |
56 ms |
11608 KB |
Output isn't correct |
71 |
Incorrect |
56 ms |
11608 KB |
Output isn't correct |
72 |
Incorrect |
56 ms |
11764 KB |
Output isn't correct |
73 |
Incorrect |
54 ms |
12112 KB |
Output isn't correct |
74 |
Incorrect |
63 ms |
12632 KB |
Output isn't correct |
75 |
Correct |
55 ms |
12116 KB |
Output is correct |
76 |
Incorrect |
53 ms |
12112 KB |
Output isn't correct |
77 |
Correct |
53 ms |
12112 KB |
Output is correct |
78 |
Correct |
54 ms |
12124 KB |
Output is correct |
79 |
Correct |
53 ms |
12112 KB |
Output is correct |
80 |
Correct |
54 ms |
12112 KB |
Output is correct |
81 |
Correct |
53 ms |
12112 KB |
Output is correct |
82 |
Correct |
53 ms |
12124 KB |
Output is correct |
83 |
Correct |
55 ms |
12124 KB |
Output is correct |
84 |
Incorrect |
60 ms |
11720 KB |
Output isn't correct |
85 |
Correct |
56 ms |
12144 KB |
Output is correct |
86 |
Correct |
56 ms |
12092 KB |
Output is correct |
87 |
Correct |
68 ms |
12112 KB |
Output is correct |
88 |
Correct |
58 ms |
12020 KB |
Output is correct |
89 |
Incorrect |
57 ms |
11860 KB |
Output isn't correct |
90 |
Correct |
60 ms |
12004 KB |
Output is correct |
91 |
Correct |
55 ms |
11856 KB |
Output is correct |
92 |
Correct |
56 ms |
11868 KB |
Output is correct |