#include<bits/stdc++.h>
using namespace std;
int n, s, u, V, x, y, z;
int a[803][803], dx[4]={0, 0, 1, -1}, dy[4]={1, -1, 0, 0};
int bear[803][803];
vector<string> v;
vector<pair<int, int> >bee, o;
queue<pair<int, int>>q, b;
queue<pair<pair<int, int>, int> >t;
bool BFS(){
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;
t.push({b.front(), 0});
b.pop();
bear[x][y]=0;
}
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();
while(!t.empty())t.pop();
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();
while(!t.empty())t.pop();
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:34: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++) {
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5368 KB |
Output is correct |
2 |
Correct |
9 ms |
5516 KB |
Output is correct |
3 |
Correct |
8 ms |
5516 KB |
Output is correct |
4 |
Correct |
8 ms |
5624 KB |
Output is correct |
5 |
Correct |
9 ms |
5624 KB |
Output is correct |
6 |
Correct |
9 ms |
5624 KB |
Output is correct |
7 |
Correct |
488 ms |
6440 KB |
Output is correct |
8 |
Correct |
11 ms |
6440 KB |
Output is correct |
9 |
Correct |
9 ms |
6440 KB |
Output is correct |
10 |
Correct |
9 ms |
6440 KB |
Output is correct |
11 |
Correct |
9 ms |
6440 KB |
Output is correct |
12 |
Correct |
11 ms |
6440 KB |
Output is correct |
13 |
Correct |
11 ms |
6440 KB |
Output is correct |
14 |
Correct |
12 ms |
6440 KB |
Output is correct |
15 |
Correct |
9 ms |
6440 KB |
Output is correct |
16 |
Correct |
10 ms |
6440 KB |
Output is correct |
17 |
Correct |
10 ms |
6440 KB |
Output is correct |
18 |
Correct |
9 ms |
6440 KB |
Output is correct |
19 |
Correct |
11 ms |
6440 KB |
Output is correct |
20 |
Correct |
10 ms |
6440 KB |
Output is correct |
21 |
Correct |
10 ms |
6440 KB |
Output is correct |
22 |
Correct |
12 ms |
6440 KB |
Output is correct |
23 |
Correct |
11 ms |
6440 KB |
Output is correct |
24 |
Correct |
11 ms |
6440 KB |
Output is correct |
25 |
Correct |
11 ms |
6440 KB |
Output is correct |
26 |
Correct |
10 ms |
6440 KB |
Output is correct |
27 |
Correct |
11 ms |
6440 KB |
Output is correct |
28 |
Correct |
11 ms |
6440 KB |
Output is correct |
29 |
Correct |
12 ms |
6440 KB |
Output is correct |
30 |
Correct |
11 ms |
6440 KB |
Output is correct |
31 |
Correct |
13 ms |
6440 KB |
Output is correct |
32 |
Correct |
13 ms |
6440 KB |
Output is correct |
33 |
Correct |
45 ms |
6440 KB |
Output is correct |
34 |
Correct |
46 ms |
6440 KB |
Output is correct |
35 |
Correct |
90 ms |
6440 KB |
Output is correct |
36 |
Correct |
60 ms |
6440 KB |
Output is correct |
37 |
Correct |
56 ms |
6440 KB |
Output is correct |
38 |
Correct |
136 ms |
6440 KB |
Output is correct |
39 |
Correct |
73 ms |
6440 KB |
Output is correct |
40 |
Correct |
70 ms |
6440 KB |
Output is correct |
41 |
Correct |
163 ms |
6440 KB |
Output is correct |
42 |
Correct |
94 ms |
6440 KB |
Output is correct |
43 |
Correct |
91 ms |
6440 KB |
Output is correct |
44 |
Correct |
180 ms |
6440 KB |
Output is correct |
45 |
Correct |
150 ms |
6440 KB |
Output is correct |
46 |
Correct |
149 ms |
6440 KB |
Output is correct |
47 |
Correct |
267 ms |
6440 KB |
Output is correct |
48 |
Correct |
141 ms |
6440 KB |
Output is correct |
49 |
Correct |
161 ms |
6440 KB |
Output is correct |
50 |
Correct |
329 ms |
6440 KB |
Output is correct |
51 |
Correct |
157 ms |
6440 KB |
Output is correct |
52 |
Correct |
150 ms |
6440 KB |
Output is correct |
53 |
Correct |
369 ms |
6440 KB |
Output is correct |
54 |
Correct |
163 ms |
6440 KB |
Output is correct |
55 |
Correct |
171 ms |
6440 KB |
Output is correct |
56 |
Correct |
441 ms |
6440 KB |
Output is correct |
57 |
Correct |
268 ms |
6440 KB |
Output is correct |
58 |
Correct |
198 ms |
6440 KB |
Output is correct |
59 |
Correct |
480 ms |
6440 KB |
Output is correct |
60 |
Correct |
264 ms |
6440 KB |
Output is correct |
61 |
Correct |
237 ms |
6440 KB |
Output is correct |
62 |
Correct |
579 ms |
6440 KB |
Output is correct |
63 |
Correct |
699 ms |
6440 KB |
Output is correct |
64 |
Correct |
610 ms |
6440 KB |
Output is correct |
65 |
Correct |
590 ms |
6440 KB |
Output is correct |
66 |
Correct |
597 ms |
6440 KB |
Output is correct |
67 |
Correct |
696 ms |
6440 KB |
Output is correct |
68 |
Correct |
573 ms |
6524 KB |
Output is correct |
69 |
Correct |
535 ms |
6524 KB |
Output is correct |
70 |
Correct |
632 ms |
6524 KB |
Output is correct |
71 |
Correct |
639 ms |
6524 KB |
Output is correct |
72 |
Correct |
613 ms |
6524 KB |
Output is correct |
73 |
Correct |
718 ms |
6652 KB |
Output is correct |
74 |
Correct |
709 ms |
6780 KB |
Output is correct |
75 |
Correct |
690 ms |
6780 KB |
Output is correct |
76 |
Correct |
839 ms |
6780 KB |
Output is correct |
77 |
Correct |
669 ms |
6780 KB |
Output is correct |
78 |
Correct |
618 ms |
6780 KB |
Output is correct |
79 |
Correct |
664 ms |
6780 KB |
Output is correct |
80 |
Correct |
634 ms |
6780 KB |
Output is correct |
81 |
Correct |
651 ms |
6780 KB |
Output is correct |
82 |
Correct |
579 ms |
6780 KB |
Output is correct |
83 |
Correct |
699 ms |
6780 KB |
Output is correct |
84 |
Correct |
593 ms |
6780 KB |
Output is correct |
85 |
Correct |
611 ms |
6780 KB |
Output is correct |
86 |
Correct |
773 ms |
6780 KB |
Output is correct |
87 |
Correct |
716 ms |
6780 KB |
Output is correct |
88 |
Correct |
712 ms |
6780 KB |
Output is correct |
89 |
Correct |
774 ms |
6780 KB |
Output is correct |
90 |
Correct |
574 ms |
6780 KB |
Output is correct |
91 |
Correct |
547 ms |
6780 KB |
Output is correct |
92 |
Correct |
659 ms |
6780 KB |
Output is correct |