#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 dfs(int len, int x, int y){
vis[x][y] = true;
int dist = (len + 1) / step;
for(int i = 0;i < 4;++i){
int u = dx[i] + x;
int v = dy[i] + y;
if(u == homex && v == homey){
return true;
}
if(u < 1 || v < 1 || u > n || v > n) continue;
if((a[u][v] == 'G') && !vis[u][v] && ((len + 1 % step == 0) ? (dist < dis[u][v]) : (dist <= dis[u][v]))){
if(dfs(len + 1, u, v)){
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(dfs(mid * step, mx, my)){
l = mid + 1;
ans = mid;
}
else{
r = mid - 1;
}
}
cout << ((ans >= 1) ? ans : -1) ;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
7 |
Incorrect |
25 ms |
8284 KB |
Output isn't correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Incorrect |
1 ms |
4736 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
12 |
Incorrect |
2 ms |
4700 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
4564 KB |
Output isn't correct |
14 |
Correct |
4 ms |
4700 KB |
Output is correct |
15 |
Incorrect |
1 ms |
4736 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
20 |
Incorrect |
2 ms |
4700 KB |
Output isn't correct |
21 |
Incorrect |
2 ms |
4700 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
4696 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
25 |
Incorrect |
2 ms |
4700 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
4700 KB |
Output isn't correct |
30 |
Incorrect |
2 ms |
4700 KB |
Output isn't correct |
31 |
Incorrect |
3 ms |
4700 KB |
Output isn't correct |
32 |
Incorrect |
2 ms |
4700 KB |
Output isn't correct |
33 |
Incorrect |
4 ms |
6748 KB |
Output isn't correct |
34 |
Incorrect |
4 ms |
6748 KB |
Output isn't correct |
35 |
Correct |
17 ms |
11356 KB |
Output is correct |
36 |
Incorrect |
4 ms |
6748 KB |
Output isn't correct |
37 |
Incorrect |
5 ms |
6748 KB |
Output isn't correct |
38 |
Correct |
27 ms |
12800 KB |
Output is correct |
39 |
Incorrect |
5 ms |
6748 KB |
Output isn't correct |
40 |
Incorrect |
5 ms |
6748 KB |
Output isn't correct |
41 |
Correct |
32 ms |
14584 KB |
Output is correct |
42 |
Incorrect |
6 ms |
6748 KB |
Output isn't correct |
43 |
Incorrect |
7 ms |
6748 KB |
Output isn't correct |
44 |
Correct |
46 ms |
16228 KB |
Output is correct |
45 |
Incorrect |
6 ms |
7000 KB |
Output isn't correct |
46 |
Incorrect |
7 ms |
6748 KB |
Output isn't correct |
47 |
Correct |
55 ms |
18444 KB |
Output is correct |
48 |
Incorrect |
7 ms |
6748 KB |
Output isn't correct |
49 |
Incorrect |
7 ms |
6624 KB |
Output isn't correct |
50 |
Correct |
66 ms |
20580 KB |
Output is correct |
51 |
Incorrect |
8 ms |
6748 KB |
Output isn't correct |
52 |
Incorrect |
8 ms |
6748 KB |
Output isn't correct |
53 |
Correct |
102 ms |
23068 KB |
Output is correct |
54 |
Incorrect |
9 ms |
6748 KB |
Output isn't correct |
55 |
Incorrect |
9 ms |
6836 KB |
Output isn't correct |
56 |
Correct |
98 ms |
25684 KB |
Output is correct |
57 |
Incorrect |
11 ms |
6744 KB |
Output isn't correct |
58 |
Incorrect |
10 ms |
6620 KB |
Output isn't correct |
59 |
Correct |
123 ms |
28520 KB |
Output is correct |
60 |
Incorrect |
15 ms |
6624 KB |
Output isn't correct |
61 |
Incorrect |
11 ms |
7000 KB |
Output isn't correct |
62 |
Correct |
136 ms |
31572 KB |
Output is correct |
63 |
Incorrect |
59 ms |
6976 KB |
Output isn't correct |
64 |
Incorrect |
86 ms |
6972 KB |
Output isn't correct |
65 |
Incorrect |
85 ms |
7004 KB |
Output isn't correct |
66 |
Incorrect |
63 ms |
6876 KB |
Output isn't correct |
67 |
Correct |
62 ms |
6748 KB |
Output is correct |
68 |
Incorrect |
34 ms |
7088 KB |
Output isn't correct |
69 |
Incorrect |
26 ms |
7004 KB |
Output isn't correct |
70 |
Incorrect |
24 ms |
6944 KB |
Output isn't correct |
71 |
Incorrect |
24 ms |
6944 KB |
Output isn't correct |
72 |
Incorrect |
25 ms |
6736 KB |
Output isn't correct |
73 |
Incorrect |
43 ms |
7516 KB |
Output isn't correct |
74 |
Incorrect |
45 ms |
14476 KB |
Output isn't correct |
75 |
Incorrect |
42 ms |
7516 KB |
Output isn't correct |
76 |
Incorrect |
33 ms |
7504 KB |
Output isn't correct |
77 |
Incorrect |
57 ms |
7912 KB |
Output isn't correct |
78 |
Correct |
26 ms |
7516 KB |
Output is correct |
79 |
Incorrect |
38 ms |
13660 KB |
Output isn't correct |
80 |
Incorrect |
40 ms |
7884 KB |
Output isn't correct |
81 |
Incorrect |
27 ms |
7516 KB |
Output isn't correct |
82 |
Incorrect |
39 ms |
7888 KB |
Output isn't correct |
83 |
Incorrect |
29 ms |
7404 KB |
Output isn't correct |
84 |
Incorrect |
45 ms |
13752 KB |
Output isn't correct |
85 |
Incorrect |
38 ms |
7760 KB |
Output isn't correct |
86 |
Incorrect |
46 ms |
7552 KB |
Output isn't correct |
87 |
Incorrect |
41 ms |
8284 KB |
Output isn't correct |
88 |
Incorrect |
39 ms |
7764 KB |
Output isn't correct |
89 |
Incorrect |
47 ms |
14940 KB |
Output isn't correct |
90 |
Incorrect |
26 ms |
8284 KB |
Output isn't correct |
91 |
Incorrect |
37 ms |
9212 KB |
Output isn't correct |
92 |
Incorrect |
32 ms |
8016 KB |
Output isn't correct |