#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
using namespace std;
const int oo = 1e9 + 9;
const int MAX = 801;
struct cord{
int x = 0, y = 0;
cord(int x1, int y1){
x = x1;
y = y1;
}
};
pii dir[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int n, s;
cord start(0, 0), dest(0, 0);
char grid[MAX][MAX];
int beeTime[MAX][MAX];
bool inBounds(cord c){
return c.x >= 0 && c.x < n && c.y >= 0 && c.y < n;
}
void bfs1(vector<cord>& source){
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
beeTime[i][j] = oo;
}
}
queue<cord> q;
for(cord a:source){
q.push(a);
beeTime[a.x][a.y] = 0;
}
while(!q.empty()){
cord c = q.front();
q.pop();
for(auto &d:dir){
cord c1 = cord(c.x + d.first, c.y + d.second);
if(!inBounds(c1) || grid[c1.x][c1.y] == 'T' || grid[c1.x][c1.y] == 'D') continue;
if(beeTime[c1.x][c1.y] > beeTime[c.x][c.y] + 1){
beeTime[c1.x][c1.y] = beeTime[c.x][c.y] + 1;
q.push(c1);
}
}
}
}
bool visited[MAX][MAX];
bool check(int startTime){
memset(visited, 0, sizeof(visited));
visited[start.x][start.y] = 1;
queue<pair<cord, pii>> q;
q.push({start, {startTime, s}});
while(!q.empty()){
cord c = q.front().first;
pii t = q.front().second;
q.pop();
if(grid[c.x][c.y] == 'D') return true;
if(beeTime[c.x][c.y] <= t.first) continue;
for(auto d:dir){
cord c1 = cord(c.x + d.first, c.y + d.second);
if(!inBounds(c1) || grid[c1.x][c1.y] == 'T' || visited[c1.x][c1.y] == 1) continue;
visited[c1.x][c1.y] = 1;
if(t.second == 1){
q.push({c1, {t.first + 1, s}});
}
else{
q.push({c1, {t.first, t.second - 1}});
}
}
}
return false;
}
int main(){
cin >> n >> s;
vector<cord> bees;
getchar();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
char c = getchar();
grid[i][j] = c;
if(c == 'H'){
bees.push_back(cord(i, j));
}
else if(c == 'M'){
start = cord(i, j);
}
else if(c == 'D'){
dest = cord(i, j);
}
}
getchar();
}
bfs1(bees);
int l = 0, r = n * n;
int ans = -1;
while(l <= r){
int mid = (l + r) / 2;
if(check(mid)){
ans = mid;
l = mid + 1;
}
else{
r = mid - 1;
}
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
66 ms |
4792 KB |
Output is correct |
8 |
Correct |
1 ms |
944 KB |
Output is correct |
9 |
Correct |
1 ms |
944 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
944 KB |
Output is correct |
12 |
Correct |
2 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1108 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
952 KB |
Output is correct |
19 |
Correct |
1 ms |
948 KB |
Output is correct |
20 |
Correct |
1 ms |
944 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
1 ms |
1108 KB |
Output is correct |
24 |
Correct |
2 ms |
1108 KB |
Output is correct |
25 |
Correct |
1 ms |
1108 KB |
Output is correct |
26 |
Correct |
1 ms |
1108 KB |
Output is correct |
27 |
Correct |
1 ms |
1108 KB |
Output is correct |
28 |
Correct |
2 ms |
1108 KB |
Output is correct |
29 |
Correct |
1 ms |
1108 KB |
Output is correct |
30 |
Correct |
1 ms |
1108 KB |
Output is correct |
31 |
Correct |
1 ms |
1236 KB |
Output is correct |
32 |
Correct |
1 ms |
1236 KB |
Output is correct |
33 |
Correct |
4 ms |
2388 KB |
Output is correct |
34 |
Correct |
3 ms |
2388 KB |
Output is correct |
35 |
Correct |
16 ms |
2432 KB |
Output is correct |
36 |
Correct |
5 ms |
2644 KB |
Output is correct |
37 |
Correct |
4 ms |
2660 KB |
Output is correct |
38 |
Correct |
22 ms |
2644 KB |
Output is correct |
39 |
Correct |
5 ms |
2872 KB |
Output is correct |
40 |
Correct |
5 ms |
2880 KB |
Output is correct |
41 |
Correct |
31 ms |
2900 KB |
Output is correct |
42 |
Correct |
7 ms |
3156 KB |
Output is correct |
43 |
Correct |
5 ms |
3028 KB |
Output is correct |
44 |
Correct |
38 ms |
3148 KB |
Output is correct |
45 |
Correct |
7 ms |
3284 KB |
Output is correct |
46 |
Correct |
7 ms |
3384 KB |
Output is correct |
47 |
Correct |
41 ms |
3380 KB |
Output is correct |
48 |
Correct |
7 ms |
3516 KB |
Output is correct |
49 |
Correct |
8 ms |
3540 KB |
Output is correct |
50 |
Correct |
49 ms |
3628 KB |
Output is correct |
51 |
Correct |
9 ms |
3788 KB |
Output is correct |
52 |
Correct |
8 ms |
3860 KB |
Output is correct |
53 |
Correct |
63 ms |
3796 KB |
Output is correct |
54 |
Correct |
10 ms |
4164 KB |
Output is correct |
55 |
Correct |
10 ms |
4144 KB |
Output is correct |
56 |
Correct |
72 ms |
4272 KB |
Output is correct |
57 |
Correct |
11 ms |
4308 KB |
Output is correct |
58 |
Correct |
11 ms |
4416 KB |
Output is correct |
59 |
Correct |
81 ms |
4320 KB |
Output is correct |
60 |
Correct |
12 ms |
4668 KB |
Output is correct |
61 |
Correct |
11 ms |
4676 KB |
Output is correct |
62 |
Correct |
94 ms |
4736 KB |
Output is correct |
63 |
Correct |
89 ms |
4684 KB |
Output is correct |
64 |
Correct |
168 ms |
4704 KB |
Output is correct |
65 |
Correct |
126 ms |
4584 KB |
Output is correct |
66 |
Correct |
101 ms |
4692 KB |
Output is correct |
67 |
Correct |
101 ms |
4680 KB |
Output is correct |
68 |
Correct |
38 ms |
4704 KB |
Output is correct |
69 |
Correct |
37 ms |
4612 KB |
Output is correct |
70 |
Correct |
31 ms |
4684 KB |
Output is correct |
71 |
Correct |
34 ms |
4704 KB |
Output is correct |
72 |
Correct |
23 ms |
4616 KB |
Output is correct |
73 |
Correct |
25 ms |
4940 KB |
Output is correct |
74 |
Correct |
44 ms |
4872 KB |
Output is correct |
75 |
Correct |
56 ms |
4976 KB |
Output is correct |
76 |
Correct |
47 ms |
4948 KB |
Output is correct |
77 |
Correct |
47 ms |
4892 KB |
Output is correct |
78 |
Correct |
61 ms |
5068 KB |
Output is correct |
79 |
Correct |
37 ms |
4940 KB |
Output is correct |
80 |
Correct |
47 ms |
4840 KB |
Output is correct |
81 |
Correct |
56 ms |
4948 KB |
Output is correct |
82 |
Correct |
47 ms |
4832 KB |
Output is correct |
83 |
Correct |
64 ms |
4876 KB |
Output is correct |
84 |
Correct |
54 ms |
4916 KB |
Output is correct |
85 |
Correct |
62 ms |
4872 KB |
Output is correct |
86 |
Correct |
59 ms |
4812 KB |
Output is correct |
87 |
Correct |
59 ms |
4876 KB |
Output is correct |
88 |
Correct |
62 ms |
4812 KB |
Output is correct |
89 |
Correct |
73 ms |
4812 KB |
Output is correct |
90 |
Correct |
70 ms |
4764 KB |
Output is correct |
91 |
Correct |
65 ms |
4848 KB |
Output is correct |
92 |
Correct |
73 ms |
4852 KB |
Output is correct |