#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;
int dist[maxn][maxn];
char str[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')
continue;
Q.push(pi(x,y));
distBee[x][y] = distBee[r][c] + s;
}
}
}
bool bfs(int d) {
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();
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:71:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
71 | scanf("%d%d",&n,&s);
| ~~~~~^~~~~~~~~~~~~~
mecho.cpp:73:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
73 | scanf("%s",str[i]+1);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5376 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 |
101 ms |
6764 KB |
Output is correct |
8 |
Incorrect |
4 ms |
5356 KB |
Output isn't correct |
9 |
Correct |
4 ms |
5356 KB |
Output is correct |
10 |
Correct |
4 ms |
5356 KB |
Output is correct |
11 |
Correct |
4 ms |
5356 KB |
Output is correct |
12 |
Incorrect |
4 ms |
5504 KB |
Output isn't correct |
13 |
Incorrect |
4 ms |
5484 KB |
Output isn't correct |
14 |
Incorrect |
4 ms |
5484 KB |
Output isn't correct |
15 |
Correct |
4 ms |
5356 KB |
Output is correct |
16 |
Correct |
4 ms |
5356 KB |
Output is correct |
17 |
Correct |
4 ms |
5356 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 |
5376 KB |
Output is correct |
21 |
Correct |
4 ms |
5484 KB |
Output is correct |
22 |
Correct |
4 ms |
5484 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 |
5 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 |
5 ms |
5484 KB |
Output is correct |
32 |
Correct |
5 ms |
5484 KB |
Output is correct |
33 |
Correct |
7 ms |
5740 KB |
Output is correct |
34 |
Correct |
7 ms |
5740 KB |
Output is correct |
35 |
Correct |
28 ms |
5740 KB |
Output is correct |
36 |
Correct |
8 ms |
5868 KB |
Output is correct |
37 |
Correct |
9 ms |
5868 KB |
Output is correct |
38 |
Correct |
36 ms |
5868 KB |
Output is correct |
39 |
Correct |
9 ms |
5996 KB |
Output is correct |
40 |
Correct |
9 ms |
5996 KB |
Output is correct |
41 |
Correct |
47 ms |
5996 KB |
Output is correct |
42 |
Correct |
11 ms |
5996 KB |
Output is correct |
43 |
Correct |
11 ms |
5996 KB |
Output is correct |
44 |
Correct |
60 ms |
5996 KB |
Output is correct |
45 |
Correct |
11 ms |
6124 KB |
Output is correct |
46 |
Correct |
11 ms |
6124 KB |
Output is correct |
47 |
Correct |
67 ms |
6252 KB |
Output is correct |
48 |
Correct |
12 ms |
6252 KB |
Output is correct |
49 |
Correct |
12 ms |
6252 KB |
Output is correct |
50 |
Correct |
82 ms |
6380 KB |
Output is correct |
51 |
Correct |
13 ms |
6380 KB |
Output is correct |
52 |
Correct |
13 ms |
6380 KB |
Output is correct |
53 |
Correct |
99 ms |
6508 KB |
Output is correct |
54 |
Correct |
14 ms |
6380 KB |
Output is correct |
55 |
Correct |
14 ms |
6380 KB |
Output is correct |
56 |
Correct |
113 ms |
6508 KB |
Output is correct |
57 |
Correct |
16 ms |
6508 KB |
Output is correct |
58 |
Correct |
16 ms |
6508 KB |
Output is correct |
59 |
Correct |
133 ms |
6508 KB |
Output is correct |
60 |
Correct |
18 ms |
6636 KB |
Output is correct |
61 |
Correct |
17 ms |
6636 KB |
Output is correct |
62 |
Correct |
151 ms |
6764 KB |
Output is correct |
63 |
Correct |
117 ms |
6764 KB |
Output is correct |
64 |
Correct |
225 ms |
6724 KB |
Output is correct |
65 |
Correct |
167 ms |
6636 KB |
Output is correct |
66 |
Correct |
143 ms |
6764 KB |
Output is correct |
67 |
Incorrect |
130 ms |
6636 KB |
Output isn't correct |
68 |
Correct |
50 ms |
6636 KB |
Output is correct |
69 |
Correct |
51 ms |
6752 KB |
Output is correct |
70 |
Correct |
37 ms |
6636 KB |
Output is correct |
71 |
Correct |
42 ms |
6636 KB |
Output is correct |
72 |
Incorrect |
34 ms |
6636 KB |
Output isn't correct |
73 |
Incorrect |
82 ms |
6892 KB |
Output isn't correct |
74 |
Correct |
67 ms |
7020 KB |
Output is correct |
75 |
Correct |
81 ms |
6892 KB |
Output is correct |
76 |
Correct |
71 ms |
6892 KB |
Output is correct |
77 |
Correct |
70 ms |
6892 KB |
Output is correct |
78 |
Incorrect |
88 ms |
6892 KB |
Output isn't correct |
79 |
Correct |
55 ms |
6892 KB |
Output is correct |
80 |
Correct |
69 ms |
6892 KB |
Output is correct |
81 |
Correct |
80 ms |
7020 KB |
Output is correct |
82 |
Correct |
68 ms |
6892 KB |
Output is correct |
83 |
Correct |
89 ms |
6892 KB |
Output is correct |
84 |
Correct |
80 ms |
6892 KB |
Output is correct |
85 |
Correct |
88 ms |
7020 KB |
Output is correct |
86 |
Correct |
86 ms |
6892 KB |
Output is correct |
87 |
Correct |
87 ms |
6892 KB |
Output is correct |
88 |
Correct |
92 ms |
6764 KB |
Output is correct |
89 |
Correct |
92 ms |
6764 KB |
Output is correct |
90 |
Correct |
96 ms |
6992 KB |
Output is correct |
91 |
Correct |
105 ms |
6884 KB |
Output is correct |
92 |
Correct |
101 ms |
6764 KB |
Output is correct |