#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
using namespace std;
using pii = pair < int, int >;
const int N = 1005;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
char a[N][N];
pii mecho, home;
int n, s, hive[N][N];
bool used[N][N];
void hive_bfs(){
memset( hive, 11, sizeof(hive) );
queue < pii > q;
for(int i = 0 ; i < n ; i++) for(int j = 0 ; j < n ; j++){
if( a[i][j] == 'H' ){
q.push( mp( i, j ) );
hive[i][j] = 0;
}
}
int dis = 0;
while( !q.empty() ){
dis += s;
int sz = q.size();
while( sz-- ){
int x, y;
tie(x, y) = q.front();
q.pop();
for(int i = 0 ; i < 4 ; i++){
int tx = x+dx[i], ty = y+dy[i];
if( tx < 0 || ty < 0 || tx >= n || ty >= n ) continue;
if( a[tx][ty] == 'G' && hive[tx][ty] > 2e6 ){
hive[tx][ty] = dis;
q.push( mp( tx, ty ) );
}
}
}
}
/*
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
if( hive[i][j] > 2e6 ) printf("@@ ");
else printf("%2d ", hive[i][j]);
}
printf("\n");
}
*/
}
bool mecho_bfs(int start_time){
memset( used, 0, sizeof(used) );
queue < pii > q;
q.push( mecho );
int dis = start_time;
while( !q.empty() ){
dis++;
// cout << "dis " << dis << " : ";
int sz = q.size();
while( sz-- ){
int x, y;
tie(x, y) = q.front();
q.pop();
for(int i = 0 ; i < 4 ; i++){
int tx = x+dx[i], ty = y+dy[i];
if( tx < 0 || ty < 0 || tx >= n || ty >= n ) continue;
if( !used[tx][ty] && a[tx][ty] == 'G' && hive[tx][ty] > dis ){
used[tx][ty] = 1;
// cout << "( " << tx << ',' << ty << " ), ";
q.push( mp( tx, ty ) );
}
}
}
// cout << endl;
}
return used[ home.first ][ home.second ];
}
int main(){
scanf("%d %d", &n, &s);
for(int i = 0 ; i < n ; i++){
scanf("%s", a[i]);
}
hive_bfs();
for(int i = 0 ; i < n ; i++) for(int j = 0 ; j < n ; j++){
if( a[i][j] == 'D' ){
home = mp( i, j );
a[i][j] = 'G';
}
if( a[i][j] == 'M' ){
mecho = mp( i, j );
}
}
if( !mecho_bfs( 0 ) ){
printf("-1\n");
return 0;
}
int res = 0;
while( mecho_bfs( s*(res+1) ) ) res++;
cout << res << endl;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:88:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &s);
~~~~~^~~~~~~~~~~~~~~~~
mecho.cpp:90:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", a[i]);
~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5248 KB |
Output is correct |
2 |
Correct |
6 ms |
5248 KB |
Output is correct |
3 |
Correct |
6 ms |
5248 KB |
Output is correct |
4 |
Correct |
6 ms |
5248 KB |
Output is correct |
5 |
Correct |
6 ms |
5248 KB |
Output is correct |
6 |
Correct |
6 ms |
5248 KB |
Output is correct |
7 |
Correct |
273 ms |
6240 KB |
Output is correct |
8 |
Correct |
6 ms |
5248 KB |
Output is correct |
9 |
Correct |
7 ms |
5248 KB |
Output is correct |
10 |
Correct |
6 ms |
5248 KB |
Output is correct |
11 |
Correct |
8 ms |
5220 KB |
Output is correct |
12 |
Execution timed out |
1059 ms |
5248 KB |
Time limit exceeded |
13 |
Incorrect |
44 ms |
5376 KB |
Output isn't correct |
14 |
Correct |
7 ms |
5248 KB |
Output is correct |
15 |
Correct |
15 ms |
5220 KB |
Output is correct |
16 |
Correct |
11 ms |
5248 KB |
Output is correct |
17 |
Correct |
21 ms |
5248 KB |
Output is correct |
18 |
Correct |
12 ms |
5248 KB |
Output is correct |
19 |
Correct |
24 ms |
5248 KB |
Output is correct |
20 |
Correct |
16 ms |
5248 KB |
Output is correct |
21 |
Correct |
47 ms |
5504 KB |
Output is correct |
22 |
Correct |
27 ms |
5376 KB |
Output is correct |
23 |
Correct |
73 ms |
5376 KB |
Output is correct |
24 |
Correct |
39 ms |
5376 KB |
Output is correct |
25 |
Correct |
110 ms |
5496 KB |
Output is correct |
26 |
Correct |
59 ms |
5376 KB |
Output is correct |
27 |
Correct |
130 ms |
5368 KB |
Output is correct |
28 |
Correct |
92 ms |
5376 KB |
Output is correct |
29 |
Correct |
149 ms |
5368 KB |
Output is correct |
30 |
Correct |
87 ms |
5404 KB |
Output is correct |
31 |
Correct |
187 ms |
5496 KB |
Output is correct |
32 |
Correct |
101 ms |
5376 KB |
Output is correct |
33 |
Execution timed out |
1079 ms |
5632 KB |
Time limit exceeded |
34 |
Runtime error |
262 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
35 |
Correct |
22 ms |
5632 KB |
Output is correct |
36 |
Execution timed out |
1085 ms |
5632 KB |
Time limit exceeded |
37 |
Runtime error |
252 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
38 |
Correct |
22 ms |
5632 KB |
Output is correct |
39 |
Execution timed out |
1078 ms |
5760 KB |
Time limit exceeded |
40 |
Runtime error |
299 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
41 |
Correct |
28 ms |
5632 KB |
Output is correct |
42 |
Execution timed out |
1083 ms |
5760 KB |
Time limit exceeded |
43 |
Runtime error |
284 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
44 |
Correct |
36 ms |
5760 KB |
Output is correct |
45 |
Execution timed out |
1073 ms |
5752 KB |
Time limit exceeded |
46 |
Runtime error |
270 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
47 |
Correct |
61 ms |
5880 KB |
Output is correct |
48 |
Execution timed out |
1069 ms |
5888 KB |
Time limit exceeded |
49 |
Runtime error |
287 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
50 |
Correct |
58 ms |
5888 KB |
Output is correct |
51 |
Execution timed out |
1084 ms |
5888 KB |
Time limit exceeded |
52 |
Runtime error |
330 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
53 |
Correct |
86 ms |
5888 KB |
Output is correct |
54 |
Execution timed out |
1082 ms |
5888 KB |
Time limit exceeded |
55 |
Runtime error |
273 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
56 |
Correct |
95 ms |
5888 KB |
Output is correct |
57 |
Execution timed out |
1064 ms |
5952 KB |
Time limit exceeded |
58 |
Runtime error |
300 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
59 |
Correct |
113 ms |
6136 KB |
Output is correct |
60 |
Execution timed out |
1090 ms |
6016 KB |
Time limit exceeded |
61 |
Runtime error |
302 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
62 |
Correct |
109 ms |
6016 KB |
Output is correct |
63 |
Correct |
53 ms |
6008 KB |
Output is correct |
64 |
Runtime error |
239 ms |
66560 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
65 |
Execution timed out |
1066 ms |
6008 KB |
Time limit exceeded |
66 |
Execution timed out |
1050 ms |
6008 KB |
Time limit exceeded |
67 |
Correct |
35 ms |
6008 KB |
Output is correct |
68 |
Correct |
37 ms |
6016 KB |
Output is correct |
69 |
Correct |
152 ms |
6136 KB |
Output is correct |
70 |
Correct |
88 ms |
6136 KB |
Output is correct |
71 |
Correct |
59 ms |
6036 KB |
Output is correct |
72 |
Execution timed out |
1054 ms |
6136 KB |
Time limit exceeded |
73 |
Incorrect |
737 ms |
6240 KB |
Output isn't correct |
74 |
Execution timed out |
1086 ms |
6240 KB |
Time limit exceeded |
75 |
Correct |
791 ms |
6112 KB |
Output is correct |
76 |
Correct |
438 ms |
6240 KB |
Output is correct |
77 |
Execution timed out |
1076 ms |
6240 KB |
Time limit exceeded |
78 |
Correct |
36 ms |
6140 KB |
Output is correct |
79 |
Execution timed out |
1085 ms |
6140 KB |
Time limit exceeded |
80 |
Correct |
731 ms |
6336 KB |
Output is correct |
81 |
Correct |
363 ms |
6268 KB |
Output is correct |
82 |
Correct |
916 ms |
6140 KB |
Output is correct |
83 |
Correct |
43 ms |
6176 KB |
Output is correct |
84 |
Correct |
981 ms |
6304 KB |
Output is correct |
85 |
Correct |
483 ms |
6176 KB |
Output is correct |
86 |
Correct |
240 ms |
6176 KB |
Output is correct |
87 |
Correct |
641 ms |
6316 KB |
Output is correct |
88 |
Correct |
48 ms |
6228 KB |
Output is correct |
89 |
Correct |
624 ms |
6328 KB |
Output is correct |
90 |
Correct |
288 ms |
6228 KB |
Output is correct |
91 |
Correct |
413 ms |
6228 KB |
Output is correct |
92 |
Correct |
153 ms |
6228 KB |
Output is correct |