#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];
int c[802][802];
vector<string> v;
vector<pair<int, int> >bee;
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();
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) b.push({x+dx[i], y+dy[i]});
else t.push({{x+dx[i], y+dy[i]}, z+1});
}
}
}
return false;
}
bool check(int m){
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});
c[u][V]=0;
while(!q.empty()){
x=q.front().first, y=q.front().second;
z=a[x][y];
if(z>m){
while(!b.empty() && c[b.front().first][b.front().second]<z){
x=b.front().first, y=b.front().second;
b.pop();
if(BFS()) return true;
}
}
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*n, m=(l+r+1)/2;
while(l<r){
//cout << l << " " << r << endl;
memset(a, -1, sizeof a);
memset(c, -1, sizeof c);
memset(bear, -1, sizeof bear);
while(!q.empty())q.pop();
while(!b.empty())b.pop();
if(check(m)) l=m;
else r=m-1;
m=(l+r+1)/2;
}
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=0; i<n; i++){
cin >> k;
k='T'+k+'T';
for(int j=1; j<=n; j++){
if(k[j]=='M'){
u=i+1, V=j;
}
if(k[j]=='H'){
bee.push_back({i+1, 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++){
~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
7800 KB |
Output is correct |
2 |
Incorrect |
15 ms |
8036 KB |
Output isn't correct |
3 |
Correct |
12 ms |
8036 KB |
Output is correct |
4 |
Incorrect |
15 ms |
8036 KB |
Output isn't correct |
5 |
Correct |
18 ms |
8076 KB |
Output is correct |
6 |
Incorrect |
12 ms |
8076 KB |
Output isn't correct |
7 |
Incorrect |
734 ms |
8892 KB |
Output isn't correct |
8 |
Incorrect |
13 ms |
8892 KB |
Output isn't correct |
9 |
Incorrect |
17 ms |
8892 KB |
Output isn't correct |
10 |
Incorrect |
18 ms |
8892 KB |
Output isn't correct |
11 |
Incorrect |
14 ms |
8892 KB |
Output isn't correct |
12 |
Correct |
18 ms |
8892 KB |
Output is correct |
13 |
Correct |
20 ms |
8892 KB |
Output is correct |
14 |
Incorrect |
18 ms |
8892 KB |
Output isn't correct |
15 |
Correct |
14 ms |
8892 KB |
Output is correct |
16 |
Correct |
15 ms |
8892 KB |
Output is correct |
17 |
Correct |
14 ms |
8892 KB |
Output is correct |
18 |
Correct |
15 ms |
8892 KB |
Output is correct |
19 |
Correct |
6 ms |
8892 KB |
Output is correct |
20 |
Correct |
14 ms |
8892 KB |
Output is correct |
21 |
Correct |
18 ms |
8892 KB |
Output is correct |
22 |
Correct |
15 ms |
8892 KB |
Output is correct |
23 |
Correct |
24 ms |
8892 KB |
Output is correct |
24 |
Correct |
16 ms |
8892 KB |
Output is correct |
25 |
Correct |
23 ms |
8892 KB |
Output is correct |
26 |
Correct |
27 ms |
8892 KB |
Output is correct |
27 |
Correct |
25 ms |
8892 KB |
Output is correct |
28 |
Correct |
31 ms |
8892 KB |
Output is correct |
29 |
Correct |
23 ms |
8892 KB |
Output is correct |
30 |
Correct |
19 ms |
8892 KB |
Output is correct |
31 |
Correct |
15 ms |
8892 KB |
Output is correct |
32 |
Correct |
19 ms |
8892 KB |
Output is correct |
33 |
Correct |
70 ms |
8892 KB |
Output is correct |
34 |
Correct |
78 ms |
8892 KB |
Output is correct |
35 |
Incorrect |
162 ms |
8892 KB |
Output isn't correct |
36 |
Correct |
83 ms |
8892 KB |
Output is correct |
37 |
Correct |
109 ms |
8892 KB |
Output is correct |
38 |
Incorrect |
140 ms |
8892 KB |
Output isn't correct |
39 |
Correct |
79 ms |
8892 KB |
Output is correct |
40 |
Correct |
84 ms |
8892 KB |
Output is correct |
41 |
Incorrect |
212 ms |
8892 KB |
Output isn't correct |
42 |
Correct |
113 ms |
8892 KB |
Output is correct |
43 |
Correct |
94 ms |
8892 KB |
Output is correct |
44 |
Incorrect |
274 ms |
8892 KB |
Output isn't correct |
45 |
Correct |
113 ms |
8892 KB |
Output is correct |
46 |
Correct |
153 ms |
8892 KB |
Output is correct |
47 |
Incorrect |
244 ms |
8892 KB |
Output isn't correct |
48 |
Correct |
120 ms |
8892 KB |
Output is correct |
49 |
Correct |
149 ms |
8892 KB |
Output is correct |
50 |
Incorrect |
405 ms |
8892 KB |
Output isn't correct |
51 |
Correct |
147 ms |
8892 KB |
Output is correct |
52 |
Correct |
170 ms |
8892 KB |
Output is correct |
53 |
Incorrect |
367 ms |
8892 KB |
Output isn't correct |
54 |
Correct |
174 ms |
8892 KB |
Output is correct |
55 |
Correct |
195 ms |
8892 KB |
Output is correct |
56 |
Incorrect |
545 ms |
8892 KB |
Output isn't correct |
57 |
Correct |
223 ms |
8892 KB |
Output is correct |
58 |
Correct |
216 ms |
8892 KB |
Output is correct |
59 |
Incorrect |
561 ms |
8892 KB |
Output isn't correct |
60 |
Correct |
258 ms |
8892 KB |
Output is correct |
61 |
Correct |
250 ms |
8892 KB |
Output is correct |
62 |
Incorrect |
647 ms |
8892 KB |
Output isn't correct |
63 |
Incorrect |
686 ms |
8892 KB |
Output isn't correct |
64 |
Correct |
645 ms |
8892 KB |
Output is correct |
65 |
Incorrect |
858 ms |
8892 KB |
Output isn't correct |
66 |
Incorrect |
728 ms |
8892 KB |
Output isn't correct |
67 |
Incorrect |
794 ms |
8892 KB |
Output isn't correct |
68 |
Incorrect |
544 ms |
8956 KB |
Output isn't correct |
69 |
Incorrect |
494 ms |
8956 KB |
Output isn't correct |
70 |
Incorrect |
561 ms |
9076 KB |
Output isn't correct |
71 |
Incorrect |
550 ms |
9076 KB |
Output isn't correct |
72 |
Correct |
636 ms |
9076 KB |
Output is correct |
73 |
Correct |
479 ms |
9084 KB |
Output is correct |
74 |
Correct |
577 ms |
9084 KB |
Output is correct |
75 |
Incorrect |
640 ms |
9084 KB |
Output isn't correct |
76 |
Incorrect |
805 ms |
9212 KB |
Output isn't correct |
77 |
Incorrect |
815 ms |
9212 KB |
Output isn't correct |
78 |
Incorrect |
665 ms |
9212 KB |
Output isn't correct |
79 |
Correct |
549 ms |
9212 KB |
Output is correct |
80 |
Incorrect |
603 ms |
9212 KB |
Output isn't correct |
81 |
Incorrect |
576 ms |
9212 KB |
Output isn't correct |
82 |
Incorrect |
543 ms |
9212 KB |
Output isn't correct |
83 |
Incorrect |
588 ms |
9212 KB |
Output isn't correct |
84 |
Correct |
610 ms |
9212 KB |
Output is correct |
85 |
Incorrect |
525 ms |
9212 KB |
Output isn't correct |
86 |
Incorrect |
544 ms |
9212 KB |
Output isn't correct |
87 |
Incorrect |
572 ms |
9212 KB |
Output isn't correct |
88 |
Incorrect |
648 ms |
9212 KB |
Output isn't correct |
89 |
Correct |
520 ms |
9212 KB |
Output is correct |
90 |
Incorrect |
555 ms |
9212 KB |
Output isn't correct |
91 |
Incorrect |
659 ms |
9212 KB |
Output isn't correct |
92 |
Incorrect |
631 ms |
9212 KB |
Output isn't correct |