#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();
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: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 |
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 |
5504 KB |
Output is correct |
5 |
Correct |
4 ms |
5356 KB |
Output is correct |
6 |
Correct |
4 ms |
5356 KB |
Output is correct |
7 |
Correct |
98 ms |
6124 KB |
Output is correct |
8 |
Correct |
4 ms |
5356 KB |
Output is 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 |
5484 KB |
Output isn't correct |
13 |
Incorrect |
4 ms |
5484 KB |
Output isn't correct |
14 |
Correct |
6 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 |
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 |
5356 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 |
5 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 |
5612 KB |
Output is correct |
34 |
Correct |
7 ms |
5612 KB |
Output is correct |
35 |
Correct |
29 ms |
5612 KB |
Output is correct |
36 |
Correct |
8 ms |
5740 KB |
Output is correct |
37 |
Correct |
8 ms |
5740 KB |
Output is correct |
38 |
Correct |
35 ms |
5760 KB |
Output is correct |
39 |
Correct |
8 ms |
5740 KB |
Output is correct |
40 |
Correct |
9 ms |
5740 KB |
Output is correct |
41 |
Correct |
47 ms |
5740 KB |
Output is correct |
42 |
Correct |
9 ms |
5760 KB |
Output is correct |
43 |
Correct |
10 ms |
5740 KB |
Output is correct |
44 |
Correct |
61 ms |
5868 KB |
Output is correct |
45 |
Correct |
11 ms |
5868 KB |
Output is correct |
46 |
Correct |
11 ms |
5868 KB |
Output is correct |
47 |
Correct |
67 ms |
5996 KB |
Output is correct |
48 |
Correct |
11 ms |
5868 KB |
Output is correct |
49 |
Correct |
12 ms |
5868 KB |
Output is correct |
50 |
Correct |
80 ms |
5996 KB |
Output is correct |
51 |
Correct |
12 ms |
5868 KB |
Output is correct |
52 |
Correct |
13 ms |
5868 KB |
Output is correct |
53 |
Correct |
97 ms |
5868 KB |
Output is correct |
54 |
Correct |
14 ms |
5996 KB |
Output is correct |
55 |
Correct |
15 ms |
5996 KB |
Output is correct |
56 |
Correct |
117 ms |
6008 KB |
Output is correct |
57 |
Correct |
15 ms |
5996 KB |
Output is correct |
58 |
Correct |
16 ms |
5996 KB |
Output is correct |
59 |
Correct |
131 ms |
5996 KB |
Output is correct |
60 |
Correct |
16 ms |
5996 KB |
Output is correct |
61 |
Correct |
17 ms |
5996 KB |
Output is correct |
62 |
Correct |
150 ms |
6124 KB |
Output is correct |
63 |
Correct |
117 ms |
5996 KB |
Output is correct |
64 |
Correct |
230 ms |
5996 KB |
Output is correct |
65 |
Correct |
172 ms |
5996 KB |
Output is correct |
66 |
Correct |
135 ms |
5996 KB |
Output is correct |
67 |
Correct |
133 ms |
5996 KB |
Output is correct |
68 |
Correct |
50 ms |
5996 KB |
Output is correct |
69 |
Correct |
50 ms |
5996 KB |
Output is correct |
70 |
Correct |
38 ms |
5996 KB |
Output is correct |
71 |
Correct |
42 ms |
5996 KB |
Output is correct |
72 |
Incorrect |
33 ms |
5996 KB |
Output isn't correct |
73 |
Incorrect |
82 ms |
6252 KB |
Output isn't correct |
74 |
Correct |
65 ms |
6252 KB |
Output is correct |
75 |
Correct |
83 ms |
6380 KB |
Output is correct |
76 |
Correct |
72 ms |
6252 KB |
Output is correct |
77 |
Correct |
69 ms |
6252 KB |
Output is correct |
78 |
Correct |
87 ms |
6252 KB |
Output is correct |
79 |
Correct |
53 ms |
6252 KB |
Output is correct |
80 |
Correct |
68 ms |
6252 KB |
Output is correct |
81 |
Correct |
78 ms |
6252 KB |
Output is correct |
82 |
Correct |
70 ms |
6252 KB |
Output is correct |
83 |
Correct |
90 ms |
6252 KB |
Output is correct |
84 |
Correct |
82 ms |
6252 KB |
Output is correct |
85 |
Correct |
87 ms |
6252 KB |
Output is correct |
86 |
Correct |
85 ms |
6252 KB |
Output is correct |
87 |
Correct |
85 ms |
6252 KB |
Output is correct |
88 |
Correct |
91 ms |
6124 KB |
Output is correct |
89 |
Correct |
94 ms |
6252 KB |
Output is correct |
90 |
Correct |
98 ms |
6124 KB |
Output is correct |
91 |
Correct |
98 ms |
6124 KB |
Output is correct |
92 |
Correct |
103 ms |
6252 KB |
Output is correct |