#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define ft first
#define se second
#define NAME "A"
#define file freopen(NAME".INP","r",stdin); freopen(NAME".OUT","w",stdout);
#define sdf ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const int MOD = 1e9 + 7;
const int N = 800 + 5, INF = 1e15;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
char a[N][N];
int dis[N][N];
bool f = false, vis[N][N] = {false};
int n, step, homex, homey, mx, my;
bool bfs(int len, int x, int y){
vis[x][y] = true;
queue<tuple<int, int, int>> q;
q.push({x, y, len});
while(!q.empty()){
const auto [sx, sy, cur] = q.front(); q.pop();
int dist = (cur + 1) / step;
for(int i = 0;i < 4;++i){
int u = sx + dx[i], v = sy + dy[i];
if(u < 1 || v < 1 || v > n || u > n) continue;
if(a[u][v] == 'G' && !vis[u][v] && dist < dis[u][v]){
vis[u][v] = true;
q.push({u, v, cur + 1});
}
}
}
for(int i = 0;i < 4;++i){
int u = homex + dx[i], v = homey + dy[i];
if(vis[u][v] && a[u][v] == 'G'){
return true;
}
}
return false;
}
int32_t main(){
sdf
cin >> n >> step;
queue<pair<int, int>> q;
for(int i = 1;i <= n;++i){
for(int j = 1;j <= n;++j){
cin >> a[i][j];
dis[i][j] = INF;
if(a[i][j] == 'H'){
q.push({i, j});
dis[i][j] = 0;
}
else if(a[i][j] == 'D'){
homex = i; homey = j;
}
else if(a[i][j] == 'M'){
mx = i; my = j;
}
}
}
while(!q.empty()){
const auto [x, y] = q.front(); q.pop();
for(int i = 0;i < 4;++i){
int u = x + dx[i];
int v = y + dy[i];
if(u < 1 || v < 1 || u > n || v > n) continue;
if(dis[u][v] == INF && a[u][v] == 'G'){
dis[u][v] = dis[x][y] + 1;
q.push({u, v});
}
}
}
int l = 1, r = INF;
int ans =0;
while(l <= r){
int mid = (l + r) / 2;
memset(vis, false, sizeof(vis));
if(bfs(mid * step, mx, my)){
l = mid + 1;
ans = mid;
}
else{
r = mid - 1;
}
}
cout << ((ans >= 1) ? ans : -1) ;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4696 KB |
Output is correct |
4 |
Correct |
1 ms |
4564 KB |
Output is correct |
5 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
60 ms |
7056 KB |
Output is correct |
8 |
Correct |
2 ms |
4696 KB |
Output is correct |
9 |
Correct |
2 ms |
4696 KB |
Output is correct |
10 |
Correct |
2 ms |
4696 KB |
Output is correct |
11 |
Correct |
2 ms |
4700 KB |
Output is correct |
12 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
4696 KB |
Output isn't correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4700 KB |
Output is correct |
16 |
Correct |
2 ms |
4700 KB |
Output is correct |
17 |
Correct |
2 ms |
4700 KB |
Output is correct |
18 |
Correct |
2 ms |
4700 KB |
Output is correct |
19 |
Correct |
2 ms |
4700 KB |
Output is correct |
20 |
Correct |
1 ms |
4564 KB |
Output is correct |
21 |
Correct |
2 ms |
4700 KB |
Output is correct |
22 |
Correct |
1 ms |
4700 KB |
Output is correct |
23 |
Correct |
2 ms |
4700 KB |
Output is correct |
24 |
Correct |
1 ms |
4700 KB |
Output is correct |
25 |
Correct |
2 ms |
4700 KB |
Output is correct |
26 |
Correct |
1 ms |
4952 KB |
Output is correct |
27 |
Correct |
2 ms |
4700 KB |
Output is correct |
28 |
Correct |
2 ms |
4700 KB |
Output is correct |
29 |
Correct |
1 ms |
4700 KB |
Output is correct |
30 |
Correct |
2 ms |
4700 KB |
Output is correct |
31 |
Correct |
2 ms |
4900 KB |
Output is correct |
32 |
Correct |
2 ms |
4700 KB |
Output is correct |
33 |
Correct |
4 ms |
6748 KB |
Output is correct |
34 |
Correct |
4 ms |
6748 KB |
Output is correct |
35 |
Correct |
22 ms |
6696 KB |
Output is correct |
36 |
Correct |
5 ms |
6748 KB |
Output is correct |
37 |
Correct |
5 ms |
6748 KB |
Output is correct |
38 |
Correct |
19 ms |
6748 KB |
Output is correct |
39 |
Correct |
6 ms |
6748 KB |
Output is correct |
40 |
Correct |
8 ms |
6828 KB |
Output is correct |
41 |
Correct |
24 ms |
6748 KB |
Output is correct |
42 |
Correct |
6 ms |
6792 KB |
Output is correct |
43 |
Correct |
6 ms |
6748 KB |
Output is correct |
44 |
Correct |
36 ms |
6820 KB |
Output is correct |
45 |
Correct |
7 ms |
6748 KB |
Output is correct |
46 |
Correct |
7 ms |
6824 KB |
Output is correct |
47 |
Correct |
40 ms |
7004 KB |
Output is correct |
48 |
Correct |
8 ms |
7004 KB |
Output is correct |
49 |
Correct |
10 ms |
7020 KB |
Output is correct |
50 |
Correct |
48 ms |
7004 KB |
Output is correct |
51 |
Correct |
11 ms |
6876 KB |
Output is correct |
52 |
Correct |
10 ms |
7000 KB |
Output is correct |
53 |
Correct |
52 ms |
7004 KB |
Output is correct |
54 |
Correct |
10 ms |
7004 KB |
Output is correct |
55 |
Correct |
10 ms |
7004 KB |
Output is correct |
56 |
Correct |
61 ms |
7120 KB |
Output is correct |
57 |
Correct |
11 ms |
7004 KB |
Output is correct |
58 |
Correct |
11 ms |
7004 KB |
Output is correct |
59 |
Correct |
72 ms |
7004 KB |
Output is correct |
60 |
Correct |
14 ms |
7200 KB |
Output is correct |
61 |
Correct |
18 ms |
7144 KB |
Output is correct |
62 |
Correct |
84 ms |
7208 KB |
Output is correct |
63 |
Incorrect |
69 ms |
7188 KB |
Output isn't correct |
64 |
Correct |
157 ms |
6980 KB |
Output is correct |
65 |
Correct |
128 ms |
7052 KB |
Output is correct |
66 |
Correct |
94 ms |
7168 KB |
Output is correct |
67 |
Correct |
82 ms |
7200 KB |
Output is correct |
68 |
Incorrect |
34 ms |
7252 KB |
Output isn't correct |
69 |
Correct |
46 ms |
7292 KB |
Output is correct |
70 |
Correct |
32 ms |
7256 KB |
Output is correct |
71 |
Correct |
25 ms |
7100 KB |
Output is correct |
72 |
Incorrect |
32 ms |
7252 KB |
Output isn't correct |
73 |
Incorrect |
45 ms |
7512 KB |
Output isn't correct |
74 |
Correct |
43 ms |
7688 KB |
Output is correct |
75 |
Correct |
48 ms |
7764 KB |
Output is correct |
76 |
Correct |
42 ms |
7512 KB |
Output is correct |
77 |
Correct |
43 ms |
7720 KB |
Output is correct |
78 |
Correct |
48 ms |
7484 KB |
Output is correct |
79 |
Correct |
47 ms |
7620 KB |
Output is correct |
80 |
Correct |
56 ms |
7508 KB |
Output is correct |
81 |
Correct |
47 ms |
7504 KB |
Output is correct |
82 |
Correct |
42 ms |
7508 KB |
Output is correct |
83 |
Incorrect |
48 ms |
7516 KB |
Output isn't correct |
84 |
Correct |
45 ms |
7256 KB |
Output is correct |
85 |
Correct |
48 ms |
7456 KB |
Output is correct |
86 |
Correct |
52 ms |
7564 KB |
Output is correct |
87 |
Correct |
50 ms |
7632 KB |
Output is correct |
88 |
Incorrect |
62 ms |
7364 KB |
Output isn't correct |
89 |
Correct |
65 ms |
7504 KB |
Output is correct |
90 |
Correct |
57 ms |
7544 KB |
Output is correct |
91 |
Correct |
48 ms |
7504 KB |
Output is correct |
92 |
Correct |
58 ms |
7504 KB |
Output is correct |