#include <bits/stdc++.h>
#define maxn 805
#define fi first
#define se second
using namespace std;
typedef pair<int,int> pi;
int n,s,ans;
int mr,mc,dr,dc;
char str[maxn][maxn];
int dist[maxn][maxn];
int distBee[maxn][maxn];
int dir[4] = {0,1,0,-1};
void bfsBee() {
queue<pi> Q;
memset(distBee,-1,sizeof(distBee));
for( int i = 1 ; i <= n ; i++ )
for( int j = 1 ; j <= n ; j++ )
if(str[i][j] == 'H') {
Q.push(pi(i,j));
distBee[i][j] = 0;
}
while(!Q.empty()) {
pi temp = Q.front();
Q.pop();
int r = temp.fi;
int c = temp.se;
for( int i = 0 ; i < 4 ; i++ ) {
int x = r + dir[i];
int y = c + dir[i^1];
if(x < 1 || x > n || y < 1 || y > n)
continue;
if(distBee[x][y] != -1 || (str[x][y] != 'G' && str[x][y] != 'M'))
continue;
Q.push(pi(x,y));
distBee[x][y] = distBee[r][c] + s;
}
}
}
bool bfs(int d) {
if(distBee[mr][mc] != -1 && distBee[mr][mc] <= s*d)
return false;
queue<pi> Q;
memset(dist,-1,sizeof(dist));
Q.push(pi(mr,mc));
dist[mr][mc] = s*d;
while(!Q.empty()) {
pi temp = Q.front();
Q.pop();
int r = temp.fi;
int c = temp.se;
if(r == dr && c == dc)
return true;
for( int i = 0 ; i < 4 ; i++ ) {
int x = r + dir[i];
int y = c + dir[i^1];
if(x < 1 || x > n || y < 1 || y > n)
continue;
if(dist[x][y] != -1 || (str[x][y] != 'G' && str[x][y] != 'D'))
continue;
if(distBee[x][y] != -1 && dist[r][c]+1 >= distBee[x][y])
continue;
Q.push(pi(x,y));
dist[x][y] = dist[r][c] + 1;
}
}
return false;
}
int main() {
scanf("%d%d",&n,&s);
for( int i = 1 ; i <= n ; i++ ) {
scanf("%s",str[i]+1);
for( int j = 1 ; j <= n ; j++ )
if(str[i][j] == 'M')
mr = i , mc = j;
else if(str[i][j] == 'D')
dr = i , dc = j;
}
bfsBee();
ans = -1;
int l = 0 , r = n*n;
while(l <= r) {
int m = (l+r) >> 1;
if(bfs(m))
ans = m , l = m+1;
else
r = m-1;
}
printf("%d\n",ans);
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:73:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
73 | scanf("%d%d",&n,&s);
| ~~~~~^~~~~~~~~~~~~~
mecho.cpp:75:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
75 | scanf("%s",str[i]+1);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5356 KB |
Output is correct |
2 |
Correct |
4 ms |
5356 KB |
Output is correct |
3 |
Correct |
4 ms |
5356 KB |
Output is correct |
4 |
Correct |
4 ms |
5356 KB |
Output is correct |
5 |
Correct |
4 ms |
5356 KB |
Output is correct |
6 |
Correct |
4 ms |
5356 KB |
Output is correct |
7 |
Correct |
103 ms |
6124 KB |
Output is correct |
8 |
Correct |
4 ms |
5356 KB |
Output is correct |
9 |
Correct |
3 ms |
5356 KB |
Output is correct |
10 |
Correct |
4 ms |
5356 KB |
Output is correct |
11 |
Correct |
3 ms |
5356 KB |
Output is correct |
12 |
Correct |
3 ms |
5484 KB |
Output is correct |
13 |
Correct |
3 ms |
5484 KB |
Output is correct |
14 |
Correct |
4 ms |
5484 KB |
Output is correct |
15 |
Correct |
4 ms |
5356 KB |
Output is correct |
16 |
Correct |
4 ms |
5356 KB |
Output is correct |
17 |
Correct |
4 ms |
5376 KB |
Output is correct |
18 |
Correct |
4 ms |
5356 KB |
Output is correct |
19 |
Correct |
4 ms |
5356 KB |
Output is correct |
20 |
Correct |
4 ms |
5356 KB |
Output is correct |
21 |
Correct |
4 ms |
5356 KB |
Output is correct |
22 |
Correct |
4 ms |
5356 KB |
Output is correct |
23 |
Correct |
4 ms |
5484 KB |
Output is correct |
24 |
Correct |
4 ms |
5484 KB |
Output is correct |
25 |
Correct |
4 ms |
5484 KB |
Output is correct |
26 |
Correct |
4 ms |
5484 KB |
Output is correct |
27 |
Correct |
4 ms |
5484 KB |
Output is correct |
28 |
Correct |
4 ms |
5484 KB |
Output is correct |
29 |
Correct |
4 ms |
5484 KB |
Output is correct |
30 |
Correct |
4 ms |
5484 KB |
Output is correct |
31 |
Correct |
4 ms |
5484 KB |
Output is correct |
32 |
Correct |
5 ms |
5484 KB |
Output is correct |
33 |
Correct |
7 ms |
5616 KB |
Output is correct |
34 |
Correct |
7 ms |
5612 KB |
Output is correct |
35 |
Correct |
28 ms |
5724 KB |
Output is correct |
36 |
Correct |
7 ms |
5740 KB |
Output is correct |
37 |
Correct |
8 ms |
5740 KB |
Output is correct |
38 |
Correct |
35 ms |
5740 KB |
Output is correct |
39 |
Correct |
8 ms |
5740 KB |
Output is correct |
40 |
Correct |
8 ms |
5740 KB |
Output is correct |
41 |
Correct |
47 ms |
5740 KB |
Output is correct |
42 |
Correct |
9 ms |
5740 KB |
Output is correct |
43 |
Correct |
9 ms |
5740 KB |
Output is correct |
44 |
Correct |
58 ms |
5868 KB |
Output is correct |
45 |
Correct |
10 ms |
5868 KB |
Output is correct |
46 |
Correct |
13 ms |
5868 KB |
Output is correct |
47 |
Correct |
67 ms |
5868 KB |
Output is correct |
48 |
Correct |
11 ms |
5868 KB |
Output is correct |
49 |
Correct |
11 ms |
5868 KB |
Output is correct |
50 |
Correct |
85 ms |
5996 KB |
Output is correct |
51 |
Correct |
18 ms |
5868 KB |
Output is correct |
52 |
Correct |
12 ms |
5868 KB |
Output is correct |
53 |
Correct |
95 ms |
5868 KB |
Output is correct |
54 |
Correct |
13 ms |
5996 KB |
Output is correct |
55 |
Correct |
13 ms |
5996 KB |
Output is correct |
56 |
Correct |
116 ms |
5996 KB |
Output is correct |
57 |
Correct |
14 ms |
5996 KB |
Output is correct |
58 |
Correct |
15 ms |
5996 KB |
Output is correct |
59 |
Correct |
132 ms |
5996 KB |
Output is correct |
60 |
Correct |
18 ms |
5996 KB |
Output is correct |
61 |
Correct |
16 ms |
5996 KB |
Output is correct |
62 |
Correct |
162 ms |
5996 KB |
Output is correct |
63 |
Correct |
116 ms |
6080 KB |
Output is correct |
64 |
Correct |
225 ms |
5996 KB |
Output is correct |
65 |
Correct |
172 ms |
6124 KB |
Output is correct |
66 |
Correct |
132 ms |
5996 KB |
Output is correct |
67 |
Correct |
128 ms |
5996 KB |
Output is correct |
68 |
Correct |
56 ms |
5996 KB |
Output is correct |
69 |
Correct |
48 ms |
5996 KB |
Output is correct |
70 |
Correct |
37 ms |
5996 KB |
Output is correct |
71 |
Correct |
42 ms |
5996 KB |
Output is correct |
72 |
Correct |
31 ms |
5996 KB |
Output is correct |
73 |
Correct |
37 ms |
6252 KB |
Output is correct |
74 |
Correct |
64 ms |
6252 KB |
Output is correct |
75 |
Correct |
84 ms |
6380 KB |
Output is correct |
76 |
Correct |
74 ms |
6252 KB |
Output is correct |
77 |
Correct |
71 ms |
6252 KB |
Output is correct |
78 |
Correct |
90 ms |
6252 KB |
Output is correct |
79 |
Correct |
53 ms |
6252 KB |
Output is correct |
80 |
Correct |
73 ms |
6252 KB |
Output is correct |
81 |
Correct |
77 ms |
6252 KB |
Output is correct |
82 |
Correct |
67 ms |
6252 KB |
Output is correct |
83 |
Correct |
92 ms |
6252 KB |
Output is correct |
84 |
Correct |
76 ms |
6252 KB |
Output is correct |
85 |
Correct |
89 ms |
6252 KB |
Output is correct |
86 |
Correct |
83 ms |
6252 KB |
Output is correct |
87 |
Correct |
84 ms |
6380 KB |
Output is correct |
88 |
Correct |
88 ms |
6124 KB |
Output is correct |
89 |
Correct |
89 ms |
6124 KB |
Output is correct |
90 |
Correct |
95 ms |
6124 KB |
Output is correct |
91 |
Correct |
93 ms |
6252 KB |
Output is correct |
92 |
Correct |
100 ms |
6124 KB |
Output is correct |