#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N = 805;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
int n, s;
char c[N][N];
pair<int, int> hive, house, bear;
int mecho[N][N], bee[N][N];
bool check(int time)
{
memset(mecho, -1, sizeof(mecho));
mecho[bear.fi][bear.se] = 0;
if(bee[bear.fi][bear.se] <= time) return false;
queue<pair<int, int>> p;
p.push(bear);
while(p.size())
{
int u = p.front().fi;
int v = p.front().se;
p.pop();
for(int e = 0; e < 4; e ++)
{
int nu = u + dx[e];
int nv = v + dy[e];
if(nu < 1 || nu > n) continue;
if(nv < 1 || nv > n) continue;
if(mecho[nu][nv] == -1 && c[nu][nv] != 'T')
{
if((mecho[u][v] + 1)/s + time < bee[nu][nv])
{
mecho[nu][nv] = mecho[u][v] + 1;
p.push({nu, nv});
}
}
}
}
for(int e = 0; e < 4; e ++)
{
if(house.fi + dx[e] < 1 || house.fi + dx[e] > n) continue;
if(house.se + dy[e] < 1 || house.se + dy[e] > n) continue;
if(c[house.fi + dx[e]][house.se + dy[e]] == 'T') continue;
if(c[house.fi + dx[e]][house.se + dy[e]] == 'H') continue;
if(mecho[house.fi + dx[e]][house.se + dy[e]] != -1) return true;
}
return false;
}
int main()
{
ios::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 >> c[i][j];
if(c[i][j] == 'M')
bear = {i, j};
if(c[i][j] == 'D')
house = {i, j};
}
memset(bee, -1, sizeof(bee));
queue<pair<int, int>> q;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++){
if(c[i][j] == 'H')
{
bee[i][j] = 0;
q.push({i, j});
}
}
while(q.size())
{
int u = q.front().fi;
int v = q.front().se;
q.pop();
for(int e = 0; e < 4; e ++)
{
int nu = u + dx[e];
int nv = v + dy[e];
if(nu < 1 || nu > n) continue;
if(nv < 1 || nv > n) continue;
if(bee[nu][nv] == -1 && c[nu][nv] != 'T')
{
bee[nu][nv] = bee[u][v] + 1;
q.push({nu, nv});
}
}
}
int l = 0, r = 1e9, ans = -1;
while(l <= r)
{
int mid = (l + r) / 2;
if(check(mid))
{
ans = mid;
l = mid + 1;
}
else
r = mid - 1;
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5356 KB |
Output is correct |
2 |
Correct |
3 ms |
5468 KB |
Output is correct |
3 |
Correct |
4 ms |
5468 KB |
Output is correct |
4 |
Correct |
4 ms |
5328 KB |
Output is correct |
5 |
Correct |
4 ms |
5468 KB |
Output is correct |
6 |
Correct |
4 ms |
5464 KB |
Output is correct |
7 |
Correct |
54 ms |
6736 KB |
Output is correct |
8 |
Correct |
4 ms |
5468 KB |
Output is correct |
9 |
Correct |
4 ms |
5468 KB |
Output is correct |
10 |
Correct |
4 ms |
5468 KB |
Output is correct |
11 |
Correct |
5 ms |
5332 KB |
Output is correct |
12 |
Incorrect |
4 ms |
5468 KB |
Output isn't correct |
13 |
Correct |
4 ms |
5580 KB |
Output is correct |
14 |
Correct |
4 ms |
5468 KB |
Output is correct |
15 |
Correct |
4 ms |
5464 KB |
Output is correct |
16 |
Correct |
3 ms |
5468 KB |
Output is correct |
17 |
Correct |
4 ms |
5464 KB |
Output is correct |
18 |
Correct |
4 ms |
5468 KB |
Output is correct |
19 |
Correct |
4 ms |
5316 KB |
Output is correct |
20 |
Correct |
4 ms |
5468 KB |
Output is correct |
21 |
Correct |
3 ms |
5468 KB |
Output is correct |
22 |
Correct |
4 ms |
5464 KB |
Output is correct |
23 |
Correct |
4 ms |
5468 KB |
Output is correct |
24 |
Correct |
4 ms |
5468 KB |
Output is correct |
25 |
Correct |
4 ms |
5340 KB |
Output is correct |
26 |
Correct |
4 ms |
5468 KB |
Output is correct |
27 |
Correct |
5 ms |
5464 KB |
Output is correct |
28 |
Correct |
4 ms |
5376 KB |
Output is correct |
29 |
Correct |
4 ms |
5468 KB |
Output is correct |
30 |
Correct |
4 ms |
5496 KB |
Output is correct |
31 |
Correct |
4 ms |
5468 KB |
Output is correct |
32 |
Correct |
4 ms |
5468 KB |
Output is correct |
33 |
Correct |
7 ms |
5740 KB |
Output is correct |
34 |
Correct |
6 ms |
5736 KB |
Output is correct |
35 |
Correct |
16 ms |
5708 KB |
Output is correct |
36 |
Correct |
7 ms |
5980 KB |
Output is correct |
37 |
Correct |
7 ms |
5980 KB |
Output is correct |
38 |
Correct |
20 ms |
5980 KB |
Output is correct |
39 |
Correct |
7 ms |
6092 KB |
Output is correct |
40 |
Correct |
8 ms |
5976 KB |
Output is correct |
41 |
Correct |
24 ms |
6092 KB |
Output is correct |
42 |
Correct |
8 ms |
6172 KB |
Output is correct |
43 |
Correct |
7 ms |
5980 KB |
Output is correct |
44 |
Correct |
31 ms |
6168 KB |
Output is correct |
45 |
Correct |
9 ms |
6232 KB |
Output is correct |
46 |
Correct |
13 ms |
6036 KB |
Output is correct |
47 |
Correct |
38 ms |
6236 KB |
Output is correct |
48 |
Correct |
11 ms |
6232 KB |
Output is correct |
49 |
Correct |
15 ms |
6236 KB |
Output is correct |
50 |
Correct |
53 ms |
6348 KB |
Output is correct |
51 |
Correct |
12 ms |
6236 KB |
Output is correct |
52 |
Correct |
12 ms |
6236 KB |
Output is correct |
53 |
Correct |
49 ms |
6232 KB |
Output is correct |
54 |
Correct |
14 ms |
6488 KB |
Output is correct |
55 |
Correct |
19 ms |
6340 KB |
Output is correct |
56 |
Correct |
63 ms |
6548 KB |
Output is correct |
57 |
Correct |
13 ms |
6620 KB |
Output is correct |
58 |
Correct |
13 ms |
6488 KB |
Output is correct |
59 |
Correct |
65 ms |
6476 KB |
Output is correct |
60 |
Correct |
18 ms |
6748 KB |
Output is correct |
61 |
Correct |
15 ms |
6576 KB |
Output is correct |
62 |
Correct |
74 ms |
6736 KB |
Output is correct |
63 |
Correct |
71 ms |
6776 KB |
Output is correct |
64 |
Correct |
123 ms |
6744 KB |
Output is correct |
65 |
Correct |
127 ms |
6776 KB |
Output is correct |
66 |
Correct |
82 ms |
6776 KB |
Output is correct |
67 |
Correct |
76 ms |
6748 KB |
Output is correct |
68 |
Correct |
36 ms |
6748 KB |
Output is correct |
69 |
Correct |
37 ms |
6748 KB |
Output is correct |
70 |
Correct |
25 ms |
6748 KB |
Output is correct |
71 |
Correct |
27 ms |
6592 KB |
Output is correct |
72 |
Correct |
24 ms |
6744 KB |
Output is correct |
73 |
Correct |
27 ms |
7000 KB |
Output is correct |
74 |
Correct |
41 ms |
6848 KB |
Output is correct |
75 |
Correct |
43 ms |
6844 KB |
Output is correct |
76 |
Correct |
41 ms |
7000 KB |
Output is correct |
77 |
Correct |
41 ms |
7000 KB |
Output is correct |
78 |
Correct |
44 ms |
7028 KB |
Output is correct |
79 |
Correct |
37 ms |
7000 KB |
Output is correct |
80 |
Correct |
43 ms |
6992 KB |
Output is correct |
81 |
Correct |
56 ms |
6876 KB |
Output is correct |
82 |
Correct |
49 ms |
7004 KB |
Output is correct |
83 |
Correct |
49 ms |
6740 KB |
Output is correct |
84 |
Correct |
45 ms |
7004 KB |
Output is correct |
85 |
Correct |
44 ms |
6740 KB |
Output is correct |
86 |
Correct |
49 ms |
6780 KB |
Output is correct |
87 |
Correct |
59 ms |
6900 KB |
Output is correct |
88 |
Correct |
52 ms |
6748 KB |
Output is correct |
89 |
Correct |
52 ms |
6724 KB |
Output is correct |
90 |
Correct |
52 ms |
6892 KB |
Output is correct |
91 |
Correct |
54 ms |
6744 KB |
Output is correct |
92 |
Correct |
50 ms |
6748 KB |
Output is correct |