#include <bits/stdc++.h>
using namespace std;
#define MAXN 805
#define INF (int)1e9
typedef pair<int,int> pii;
int N,S;
char grid[MAXN][MAXN];
int dist_bees[MAXN][MAXN];
int dist_mecho[MAXN][MAXN];
pii mecho,home;
const vector<pii> deltas={{1,0},{-1,0},{0,1},{0,-1}};
bool inbounds(int x,int y){
return 1<=x&&x<=N&&1<=y&&y<=N;
}
bool check(int k){//k<=dist_bees[mecho.first][mecho.second]-1
queue<pii> q;
bool used[MAXN][MAXN];
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
used[i][j]=false;
dist_mecho[i][j]=INF;
}
}
used[mecho.first][mecho.second]=true;
dist_mecho[mecho.first][mecho.second]=0;
q.push(mecho);
while(!q.empty()){
pii p=q.front();
q.pop();
int x=p.first;
int y=p.second;
for(auto d:deltas){
int x_=x+d.first;
int y_=y+d.second;
int steps=dist_mecho[x][y]+1;
if(make_pair(x_,y_)==home) return true;
if(inbounds(x_,y_)&&grid[x_][y_]=='G'&&!used[x_][y_]&&(steps/S+k<dist_bees[x_][y_])){
used[x_][y_]=true;
dist_mecho[x_][y_]=steps;
q.push({x_,y_});
}
}
}
return false;
}
int main(){
scanf("%d%d",&N,&S);
queue<pii> q;
bool used[MAXN][MAXN];
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++){
used[i][j]=false;
dist_bees[i][j]=INF;
scanf(" %c",&grid[i][j]);
if(grid[i][j]=='M') mecho={i,j};
else if(grid[i][j]=='D') home={i,j};
else if(grid[i][j]=='H'){
q.push({i,j});//hive
dist_bees[i][j]=0;
used[i][j]=true;
}
}
}
while(!q.empty()){
pii p=q.front();
q.pop();
int x=p.first;
int y=p.second;
for(auto d:deltas){
int x_=x+d.first;
int y_=y+d.second;
if(inbounds(x_,y_)&&(grid[x_][y_]=='G'||grid[x_][y_]=='M')&&!used[x_][y_]){
used[x_][y_]=true;
dist_bees[x_][y_]=dist_bees[x][y]+1;
q.push({x_,y_});
}
}
}
int ans=0;
int max_ans=dist_bees[mecho.first][mecho.second]-1;
for(int b=max_ans;b>0;b/=2){
while(ans+b<=max_ans&&check(ans+b)) ans+=b;
}
printf("%d\n",check(ans)?ans:-1);
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:52:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | scanf("%d%d",&N,&S);
| ~~~~~^~~~~~~~~~~~~~
mecho.cpp:59:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | scanf(" %c",&grid[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1484 KB |
Output is correct |
2 |
Correct |
1 ms |
1612 KB |
Output is correct |
3 |
Correct |
1 ms |
1612 KB |
Output is correct |
4 |
Correct |
1 ms |
1612 KB |
Output is correct |
5 |
Correct |
1 ms |
1612 KB |
Output is correct |
6 |
Correct |
1 ms |
1572 KB |
Output is correct |
7 |
Correct |
159 ms |
7964 KB |
Output is correct |
8 |
Correct |
1 ms |
1612 KB |
Output is correct |
9 |
Correct |
1 ms |
1612 KB |
Output is correct |
10 |
Correct |
1 ms |
1612 KB |
Output is correct |
11 |
Correct |
1 ms |
1612 KB |
Output is correct |
12 |
Correct |
2 ms |
1996 KB |
Output is correct |
13 |
Correct |
2 ms |
1868 KB |
Output is correct |
14 |
Correct |
3 ms |
1996 KB |
Output is correct |
15 |
Correct |
1 ms |
1612 KB |
Output is correct |
16 |
Correct |
1 ms |
1704 KB |
Output is correct |
17 |
Correct |
2 ms |
1700 KB |
Output is correct |
18 |
Correct |
1 ms |
1740 KB |
Output is correct |
19 |
Correct |
2 ms |
1740 KB |
Output is correct |
20 |
Correct |
1 ms |
1740 KB |
Output is correct |
21 |
Correct |
2 ms |
1740 KB |
Output is correct |
22 |
Correct |
2 ms |
1740 KB |
Output is correct |
23 |
Correct |
2 ms |
1868 KB |
Output is correct |
24 |
Correct |
2 ms |
1868 KB |
Output is correct |
25 |
Correct |
2 ms |
1996 KB |
Output is correct |
26 |
Correct |
2 ms |
1996 KB |
Output is correct |
27 |
Correct |
2 ms |
1996 KB |
Output is correct |
28 |
Correct |
2 ms |
1996 KB |
Output is correct |
29 |
Correct |
2 ms |
1996 KB |
Output is correct |
30 |
Correct |
2 ms |
1996 KB |
Output is correct |
31 |
Correct |
2 ms |
1996 KB |
Output is correct |
32 |
Correct |
2 ms |
1996 KB |
Output is correct |
33 |
Correct |
12 ms |
4052 KB |
Output is correct |
34 |
Correct |
12 ms |
4172 KB |
Output is correct |
35 |
Correct |
41 ms |
4164 KB |
Output is correct |
36 |
Correct |
17 ms |
4524 KB |
Output is correct |
37 |
Correct |
19 ms |
4472 KB |
Output is correct |
38 |
Correct |
48 ms |
4460 KB |
Output is correct |
39 |
Correct |
26 ms |
4932 KB |
Output is correct |
40 |
Correct |
21 ms |
4936 KB |
Output is correct |
41 |
Correct |
59 ms |
4940 KB |
Output is correct |
42 |
Correct |
25 ms |
5304 KB |
Output is correct |
43 |
Correct |
22 ms |
5272 KB |
Output is correct |
44 |
Correct |
77 ms |
5316 KB |
Output is correct |
45 |
Correct |
33 ms |
5700 KB |
Output is correct |
46 |
Correct |
27 ms |
5724 KB |
Output is correct |
47 |
Correct |
93 ms |
5700 KB |
Output is correct |
48 |
Correct |
34 ms |
6084 KB |
Output is correct |
49 |
Correct |
32 ms |
6084 KB |
Output is correct |
50 |
Correct |
105 ms |
6168 KB |
Output is correct |
51 |
Correct |
38 ms |
6568 KB |
Output is correct |
52 |
Correct |
39 ms |
6576 KB |
Output is correct |
53 |
Correct |
135 ms |
6580 KB |
Output is correct |
54 |
Correct |
48 ms |
6904 KB |
Output is correct |
55 |
Correct |
44 ms |
6948 KB |
Output is correct |
56 |
Correct |
156 ms |
6920 KB |
Output is correct |
57 |
Correct |
49 ms |
7368 KB |
Output is correct |
58 |
Correct |
47 ms |
7364 KB |
Output is correct |
59 |
Correct |
184 ms |
7312 KB |
Output is correct |
60 |
Correct |
63 ms |
7760 KB |
Output is correct |
61 |
Correct |
55 ms |
7804 KB |
Output is correct |
62 |
Correct |
202 ms |
7868 KB |
Output is correct |
63 |
Correct |
155 ms |
7856 KB |
Output is correct |
64 |
Correct |
299 ms |
7860 KB |
Output is correct |
65 |
Correct |
295 ms |
7860 KB |
Output is correct |
66 |
Correct |
244 ms |
7760 KB |
Output is correct |
67 |
Correct |
175 ms |
7856 KB |
Output is correct |
68 |
Correct |
96 ms |
7876 KB |
Output is correct |
69 |
Correct |
95 ms |
7800 KB |
Output is correct |
70 |
Correct |
88 ms |
7788 KB |
Output is correct |
71 |
Correct |
89 ms |
7972 KB |
Output is correct |
72 |
Correct |
67 ms |
7772 KB |
Output is correct |
73 |
Correct |
77 ms |
8012 KB |
Output is correct |
74 |
Correct |
114 ms |
8132 KB |
Output is correct |
75 |
Correct |
129 ms |
8100 KB |
Output is correct |
76 |
Correct |
144 ms |
8136 KB |
Output is correct |
77 |
Correct |
139 ms |
8132 KB |
Output is correct |
78 |
Correct |
134 ms |
8024 KB |
Output is correct |
79 |
Correct |
141 ms |
8108 KB |
Output is correct |
80 |
Correct |
137 ms |
8108 KB |
Output is correct |
81 |
Correct |
159 ms |
8084 KB |
Output is correct |
82 |
Correct |
136 ms |
7996 KB |
Output is correct |
83 |
Correct |
137 ms |
8004 KB |
Output is correct |
84 |
Correct |
144 ms |
8080 KB |
Output is correct |
85 |
Correct |
152 ms |
7948 KB |
Output is correct |
86 |
Correct |
163 ms |
8072 KB |
Output is correct |
87 |
Correct |
150 ms |
8076 KB |
Output is correct |
88 |
Correct |
142 ms |
8048 KB |
Output is correct |
89 |
Correct |
166 ms |
8040 KB |
Output is correct |
90 |
Correct |
174 ms |
8004 KB |
Output is correct |
91 |
Correct |
176 ms |
7952 KB |
Output is correct |
92 |
Correct |
178 ms |
8016 KB |
Output is correct |