#include <bits/stdc++.h>
#define infi INT_MAX
using namespace std;
using pii = pair<int,int>;
const int N = 801;
vector<pii>path = {{-1, 0}, {+1, 0}, {0, -1}, {0, +1}};
int n, s;
char grid[N][N];
int bees[N][N];
bool vis[N][N];
int dist[N][N];
int sr, sc;
int dr, dc;
queue<pii>Q;
queue<pair<int, pii>>q;
bool Valid(int i, int j){
return (i>=0 && j>=0 && i<n && j<n);
}
void initial(int type){
if(type == 0){
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
bees[i][j] = infi;
}
}
}else if(type == 1){
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
dist[i][j] = infi;
}
}
}
}
bool check(int wait){
if(wait >= bees[sr][sc]) return false;
memset(vis, false, sizeof(vis));
initial(1);
q.push({sr, {sc, 0}}); dist[sr][sc] = wait;
while(!q.empty()){
int i = q.front().first;
int j = q.front().second.first;
int cnt = q.front().second.second;
q.pop();
if(dist[i][j] == infi) continue;
for(pii it: path){
int x = i + it.first; int y = j + it.second;
int distXY = dist[i][j] + ((cnt+1)%s == 0);
if(Valid(x, y) && !vis[x][y] && grid[x][y] != 'T' && grid[x][y] != 'H' && distXY < bees[x][y]){
dist[x][y] = distXY;
vis[x][y] = true;
q.push({x, {y, cnt+1}});
}
}
}
if(dist[dr][dc] == infi) return false;
else return true;
}
int32_t main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin>>n>>s;
initial(0); memset(vis, false, sizeof(vis));
for(int i = 0; i<n; i++){
string s; cin>>s;
for(int j = 0; j<n; j++){
grid[i][j] = s[j];
if(grid[i][j] == 'M'){
sr = i; sc = j;
}else if(grid[i][j] == 'D'){
dr = i; dc= j;
}else if(grid[i][j] == 'H'){
Q.push({i, j}); bees[i][j] = 0; vis[i][j] = true;
}
}
}
while(!Q.empty()){
int i = Q.front().first;
int j = Q.front().second;
Q.pop();
for(pii it: path){
int x = i + it.first; int y = j + it.second;
if(Valid(x, y) && !vis[x][y] && grid[x][y] != 'T' && grid[x][y] != 'D'){
vis[x][y] = true;
bees[x][y] = bees[i][j] + 1;
Q.push({x, y});
}
}
}
int l = 0, h = n*n;
int ans = -1;
while(l<=h){
int mid = l + (h-l)/2;
if(check(mid)){
l = mid+1;
ans = mid;
}else{
h = mid-1;
}
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
99 ms |
6664 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
1364 KB |
Output is correct |
13 |
Correct |
1 ms |
1236 KB |
Output is correct |
14 |
Correct |
1 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
1108 KB |
Output is correct |
18 |
Correct |
1 ms |
1108 KB |
Output is correct |
19 |
Correct |
1 ms |
1108 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
1236 KB |
Output is correct |
22 |
Correct |
1 ms |
1236 KB |
Output is correct |
23 |
Correct |
1 ms |
1236 KB |
Output is correct |
24 |
Correct |
1 ms |
1236 KB |
Output is correct |
25 |
Correct |
1 ms |
1364 KB |
Output is correct |
26 |
Correct |
1 ms |
1364 KB |
Output is correct |
27 |
Correct |
1 ms |
1364 KB |
Output is correct |
28 |
Correct |
1 ms |
1364 KB |
Output is correct |
29 |
Correct |
1 ms |
1364 KB |
Output is correct |
30 |
Correct |
1 ms |
1364 KB |
Output is correct |
31 |
Correct |
1 ms |
1492 KB |
Output is correct |
32 |
Correct |
1 ms |
1492 KB |
Output is correct |
33 |
Correct |
5 ms |
3436 KB |
Output is correct |
34 |
Correct |
4 ms |
3412 KB |
Output is correct |
35 |
Correct |
20 ms |
3412 KB |
Output is correct |
36 |
Correct |
5 ms |
3700 KB |
Output is correct |
37 |
Correct |
9 ms |
3796 KB |
Output is correct |
38 |
Correct |
29 ms |
3796 KB |
Output is correct |
39 |
Correct |
7 ms |
4052 KB |
Output is correct |
40 |
Correct |
8 ms |
4160 KB |
Output is correct |
41 |
Correct |
39 ms |
4052 KB |
Output is correct |
42 |
Correct |
9 ms |
4436 KB |
Output is correct |
43 |
Correct |
9 ms |
4512 KB |
Output is correct |
44 |
Correct |
39 ms |
4520 KB |
Output is correct |
45 |
Correct |
12 ms |
4820 KB |
Output is correct |
46 |
Correct |
10 ms |
4864 KB |
Output is correct |
47 |
Correct |
54 ms |
4820 KB |
Output is correct |
48 |
Correct |
11 ms |
5204 KB |
Output is correct |
49 |
Correct |
15 ms |
5204 KB |
Output is correct |
50 |
Correct |
69 ms |
5216 KB |
Output is correct |
51 |
Correct |
13 ms |
5584 KB |
Output is correct |
52 |
Correct |
13 ms |
5464 KB |
Output is correct |
53 |
Correct |
66 ms |
5480 KB |
Output is correct |
54 |
Correct |
14 ms |
5844 KB |
Output is correct |
55 |
Correct |
15 ms |
5932 KB |
Output is correct |
56 |
Correct |
81 ms |
5924 KB |
Output is correct |
57 |
Correct |
16 ms |
6288 KB |
Output is correct |
58 |
Correct |
18 ms |
6244 KB |
Output is correct |
59 |
Correct |
98 ms |
6288 KB |
Output is correct |
60 |
Correct |
18 ms |
6612 KB |
Output is correct |
61 |
Correct |
19 ms |
6612 KB |
Output is correct |
62 |
Correct |
106 ms |
6628 KB |
Output is correct |
63 |
Correct |
93 ms |
6616 KB |
Output is correct |
64 |
Correct |
187 ms |
6612 KB |
Output is correct |
65 |
Correct |
138 ms |
6616 KB |
Output is correct |
66 |
Correct |
123 ms |
6620 KB |
Output is correct |
67 |
Correct |
105 ms |
6616 KB |
Output is correct |
68 |
Correct |
41 ms |
6640 KB |
Output is correct |
69 |
Correct |
39 ms |
6612 KB |
Output is correct |
70 |
Correct |
29 ms |
6564 KB |
Output is correct |
71 |
Correct |
35 ms |
6600 KB |
Output is correct |
72 |
Correct |
24 ms |
6572 KB |
Output is correct |
73 |
Correct |
27 ms |
6832 KB |
Output is correct |
74 |
Correct |
48 ms |
6892 KB |
Output is correct |
75 |
Correct |
70 ms |
6932 KB |
Output is correct |
76 |
Correct |
50 ms |
6844 KB |
Output is correct |
77 |
Correct |
49 ms |
6848 KB |
Output is correct |
78 |
Correct |
65 ms |
6880 KB |
Output is correct |
79 |
Correct |
41 ms |
6868 KB |
Output is correct |
80 |
Correct |
55 ms |
6864 KB |
Output is correct |
81 |
Correct |
63 ms |
6812 KB |
Output is correct |
82 |
Correct |
53 ms |
6932 KB |
Output is correct |
83 |
Correct |
64 ms |
6828 KB |
Output is correct |
84 |
Correct |
60 ms |
6732 KB |
Output is correct |
85 |
Correct |
67 ms |
6828 KB |
Output is correct |
86 |
Correct |
60 ms |
6820 KB |
Output is correct |
87 |
Correct |
64 ms |
6740 KB |
Output is correct |
88 |
Correct |
68 ms |
6828 KB |
Output is correct |
89 |
Correct |
69 ms |
6776 KB |
Output is correct |
90 |
Correct |
78 ms |
6772 KB |
Output is correct |
91 |
Correct |
72 ms |
6772 KB |
Output is correct |
92 |
Correct |
72 ms |
6768 KB |
Output is correct |