#include<bits/stdc++.h>
using namespace std;
int n, s, u, V, x, y, z;
int a[802][802], dx[4]={0, 0, 1, -1}, dy[4]={1, -1, 0, 0};
int bear[802][802];
vector<string> v;
vector<pair<int, int> >bee, o;
queue<pair<int, int>>q, b;
bool BFS(){
queue<pair<pair<int, int>, int> >t;
t.push({{x, y}, 0});
bear[x][y]=0;
while(!t.empty()){
if(t.front().second>=s) return false;
x=t.front().first.first, y=t.front().first.second, z=t.front().second;
t.pop();
if(a[x][y]!=-1) continue;
for(int i=0; i<4; i++){
if(v[x+dx[i]][y+dy[i]]=='D') return true;
if(v[x+dx[i]][y+dy[i]]=='G' && a[x+dx[i]][y+dy[i]]==-1 && bear[x+dx[i]][y+dy[i]]==-1){
bear[x+dx[i]][y+dy[i]]=z+1;
if(z+1>=s) o.push_back({x+dx[i], y+dy[i]});
else t.push({{x+dx[i], y+dy[i]}, z+1});
}
}
}
return false;
}
bool check(int m){
int curr;
for(int i=0; i<bee.size(); i++){
x=bee[i].first, y=bee[i].second;
a[x][y]=0;
q.push(bee[i]);
}
b.push({u, V});
bear[u][V]=0;
while(!q.empty()){
x=q.front().first, y=q.front().second;
curr=z=a[x][y];
if(z>=m){
while(!b.empty()){
x=b.front().first, y=b.front().second;
b.pop();
if(BFS()) return true;
}
for(int i=0; i<o.size(); i++) b.push(o[i]);
o.clear();
}
while(!q.empty() && a[q.front().first][q.front().second]==curr){
x=q.front().first, y=q.front().second;
q.pop();
for(int i=0; i<4; i++){
if((v[x+dx[i]][y+dy[i]]=='G' || v[x+dx[i]][y+dy[i]]=='M')&& a[x+dx[i]][y+dy[i]]==-1){
a[x+dx[i]][y+dy[i]]=a[x][y]+1;
q.push({x+dx[i], y+dy[i]});
}
}
}
}
return false;
}
void solve(){
int l=0, r=(n+1)*n, m=(l+r+1)/2;
while(l<r){
memset(a, -1, sizeof a);
memset(bear, -1, sizeof bear);
while(!q.empty())q.pop();
while(!b.empty())b.pop();
o.clear();
if(check(m)) l=m;
else r=m-1;
m=(l+r+1)/2;
}
memset(a, -1, sizeof a);
memset(bear, -1, sizeof bear);
while(!q.empty())q.pop();
while(!b.empty())b.pop();
o.clear();
if(check(l)==false) cout << -1;
else cout << l;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> s;
string tmp, k;
for(int i=0; i<n+2; i++) tmp+='T';
v.push_back(tmp);
for(int i=1; i<=n; i++){
cin >> k;
k='T'+k+'T';
for(int j=1; j<=n; j++){
if(k[j]=='M'){
u=i, V=j;
}
if(k[j]=='H'){
bee.push_back({i, j});
}
}
v.push_back(k);
}
v.push_back(tmp);
solve();
return 0;
}
Compilation message
mecho.cpp: In function 'bool check(int)':
mecho.cpp:36:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<bee.size(); i++){
~^~~~~~~~~~~
mecho.cpp:52:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<o.size(); i++) b.push(o[i]);
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5368 KB |
Output is correct |
2 |
Correct |
8 ms |
5368 KB |
Output is correct |
3 |
Correct |
8 ms |
5532 KB |
Output is correct |
4 |
Correct |
12 ms |
5532 KB |
Output is correct |
5 |
Correct |
8 ms |
5544 KB |
Output is correct |
6 |
Incorrect |
9 ms |
5612 KB |
Output isn't correct |
7 |
Correct |
597 ms |
6408 KB |
Output is correct |
8 |
Correct |
9 ms |
6408 KB |
Output is correct |
9 |
Correct |
10 ms |
6408 KB |
Output is correct |
10 |
Correct |
11 ms |
6408 KB |
Output is correct |
11 |
Correct |
10 ms |
6408 KB |
Output is correct |
12 |
Correct |
12 ms |
6408 KB |
Output is correct |
13 |
Correct |
11 ms |
6408 KB |
Output is correct |
14 |
Correct |
13 ms |
6408 KB |
Output is correct |
15 |
Correct |
9 ms |
6408 KB |
Output is correct |
16 |
Correct |
10 ms |
6408 KB |
Output is correct |
17 |
Correct |
12 ms |
6408 KB |
Output is correct |
18 |
Correct |
11 ms |
6408 KB |
Output is correct |
19 |
Correct |
11 ms |
6408 KB |
Output is correct |
20 |
Correct |
9 ms |
6408 KB |
Output is correct |
21 |
Correct |
12 ms |
6408 KB |
Output is correct |
22 |
Correct |
10 ms |
6408 KB |
Output is correct |
23 |
Correct |
11 ms |
6408 KB |
Output is correct |
24 |
Correct |
12 ms |
6408 KB |
Output is correct |
25 |
Correct |
15 ms |
6408 KB |
Output is correct |
26 |
Correct |
14 ms |
6408 KB |
Output is correct |
27 |
Correct |
12 ms |
6408 KB |
Output is correct |
28 |
Correct |
13 ms |
6408 KB |
Output is correct |
29 |
Correct |
14 ms |
6408 KB |
Output is correct |
30 |
Correct |
15 ms |
6408 KB |
Output is correct |
31 |
Correct |
13 ms |
6408 KB |
Output is correct |
32 |
Correct |
12 ms |
6408 KB |
Output is correct |
33 |
Correct |
47 ms |
6408 KB |
Output is correct |
34 |
Correct |
51 ms |
6408 KB |
Output is correct |
35 |
Correct |
101 ms |
6408 KB |
Output is correct |
36 |
Correct |
65 ms |
6408 KB |
Output is correct |
37 |
Correct |
68 ms |
6408 KB |
Output is correct |
38 |
Correct |
134 ms |
6408 KB |
Output is correct |
39 |
Correct |
77 ms |
6408 KB |
Output is correct |
40 |
Correct |
85 ms |
6408 KB |
Output is correct |
41 |
Correct |
227 ms |
6408 KB |
Output is correct |
42 |
Correct |
89 ms |
6408 KB |
Output is correct |
43 |
Correct |
95 ms |
6408 KB |
Output is correct |
44 |
Correct |
209 ms |
6408 KB |
Output is correct |
45 |
Correct |
104 ms |
6408 KB |
Output is correct |
46 |
Correct |
136 ms |
6408 KB |
Output is correct |
47 |
Correct |
277 ms |
6408 KB |
Output is correct |
48 |
Correct |
140 ms |
6408 KB |
Output is correct |
49 |
Correct |
147 ms |
6408 KB |
Output is correct |
50 |
Correct |
301 ms |
6408 KB |
Output is correct |
51 |
Correct |
145 ms |
6408 KB |
Output is correct |
52 |
Correct |
187 ms |
6408 KB |
Output is correct |
53 |
Correct |
453 ms |
6408 KB |
Output is correct |
54 |
Correct |
159 ms |
6408 KB |
Output is correct |
55 |
Correct |
221 ms |
6408 KB |
Output is correct |
56 |
Correct |
485 ms |
6408 KB |
Output is correct |
57 |
Correct |
246 ms |
6408 KB |
Output is correct |
58 |
Correct |
205 ms |
6408 KB |
Output is correct |
59 |
Correct |
645 ms |
6408 KB |
Output is correct |
60 |
Correct |
187 ms |
6408 KB |
Output is correct |
61 |
Correct |
262 ms |
6408 KB |
Output is correct |
62 |
Correct |
620 ms |
6408 KB |
Output is correct |
63 |
Correct |
675 ms |
6424 KB |
Output is correct |
64 |
Correct |
612 ms |
6424 KB |
Output is correct |
65 |
Correct |
680 ms |
6424 KB |
Output is correct |
66 |
Correct |
878 ms |
6424 KB |
Output is correct |
67 |
Correct |
732 ms |
6424 KB |
Output is correct |
68 |
Correct |
640 ms |
6424 KB |
Output is correct |
69 |
Correct |
502 ms |
6424 KB |
Output is correct |
70 |
Correct |
575 ms |
6520 KB |
Output is correct |
71 |
Correct |
592 ms |
6520 KB |
Output is correct |
72 |
Correct |
598 ms |
6520 KB |
Output is correct |
73 |
Correct |
536 ms |
6652 KB |
Output is correct |
74 |
Incorrect |
595 ms |
6780 KB |
Output isn't correct |
75 |
Incorrect |
741 ms |
6780 KB |
Output isn't correct |
76 |
Incorrect |
629 ms |
6880 KB |
Output isn't correct |
77 |
Incorrect |
701 ms |
6880 KB |
Output isn't correct |
78 |
Correct |
619 ms |
6880 KB |
Output is correct |
79 |
Correct |
616 ms |
6880 KB |
Output is correct |
80 |
Correct |
663 ms |
6880 KB |
Output is correct |
81 |
Incorrect |
523 ms |
6880 KB |
Output isn't correct |
82 |
Correct |
489 ms |
6880 KB |
Output is correct |
83 |
Correct |
526 ms |
6880 KB |
Output is correct |
84 |
Correct |
593 ms |
6880 KB |
Output is correct |
85 |
Correct |
554 ms |
6880 KB |
Output is correct |
86 |
Correct |
544 ms |
6880 KB |
Output is correct |
87 |
Correct |
701 ms |
6880 KB |
Output is correct |
88 |
Correct |
619 ms |
6880 KB |
Output is correct |
89 |
Correct |
740 ms |
6880 KB |
Output is correct |
90 |
Correct |
665 ms |
6880 KB |
Output is correct |
91 |
Correct |
531 ms |
6880 KB |
Output is correct |
92 |
Correct |
668 ms |
6880 KB |
Output is correct |