#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});*/
b.push({x+dx[i], y+dy[i]});
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=2*n, m=(l+r+1)/2;
while(l<r){
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=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++){
~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
7928 KB |
Output is correct |
2 |
Incorrect |
11 ms |
7928 KB |
Output isn't correct |
3 |
Correct |
13 ms |
8100 KB |
Output is correct |
4 |
Incorrect |
12 ms |
8100 KB |
Output isn't correct |
5 |
Correct |
11 ms |
8100 KB |
Output is correct |
6 |
Incorrect |
15 ms |
8264 KB |
Output isn't correct |
7 |
Incorrect |
710 ms |
9720 KB |
Output isn't correct |
8 |
Incorrect |
11 ms |
9720 KB |
Output isn't correct |
9 |
Incorrect |
12 ms |
9720 KB |
Output isn't correct |
10 |
Incorrect |
12 ms |
9720 KB |
Output isn't correct |
11 |
Incorrect |
13 ms |
9720 KB |
Output isn't correct |
12 |
Incorrect |
17 ms |
9720 KB |
Output isn't correct |
13 |
Correct |
13 ms |
9720 KB |
Output is correct |
14 |
Incorrect |
18 ms |
9720 KB |
Output isn't correct |
15 |
Incorrect |
16 ms |
9720 KB |
Output isn't correct |
16 |
Incorrect |
16 ms |
9720 KB |
Output isn't correct |
17 |
Incorrect |
15 ms |
9720 KB |
Output isn't correct |
18 |
Incorrect |
12 ms |
9720 KB |
Output isn't correct |
19 |
Incorrect |
14 ms |
9720 KB |
Output isn't correct |
20 |
Incorrect |
12 ms |
9720 KB |
Output isn't correct |
21 |
Incorrect |
15 ms |
9720 KB |
Output isn't correct |
22 |
Incorrect |
17 ms |
9720 KB |
Output isn't correct |
23 |
Incorrect |
12 ms |
9720 KB |
Output isn't correct |
24 |
Incorrect |
13 ms |
9720 KB |
Output isn't correct |
25 |
Incorrect |
13 ms |
9720 KB |
Output isn't correct |
26 |
Incorrect |
17 ms |
9720 KB |
Output isn't correct |
27 |
Incorrect |
14 ms |
9720 KB |
Output isn't correct |
28 |
Incorrect |
13 ms |
9720 KB |
Output isn't correct |
29 |
Incorrect |
14 ms |
9720 KB |
Output isn't correct |
30 |
Incorrect |
13 ms |
9720 KB |
Output isn't correct |
31 |
Incorrect |
15 ms |
9720 KB |
Output isn't correct |
32 |
Incorrect |
15 ms |
9720 KB |
Output isn't correct |
33 |
Incorrect |
15 ms |
9720 KB |
Output isn't correct |
34 |
Incorrect |
22 ms |
9720 KB |
Output isn't correct |
35 |
Incorrect |
183 ms |
9768 KB |
Output isn't correct |
36 |
Incorrect |
15 ms |
9768 KB |
Output isn't correct |
37 |
Incorrect |
21 ms |
9788 KB |
Output isn't correct |
38 |
Incorrect |
250 ms |
10460 KB |
Output isn't correct |
39 |
Incorrect |
15 ms |
10460 KB |
Output isn't correct |
40 |
Incorrect |
18 ms |
10460 KB |
Output isn't correct |
41 |
Incorrect |
322 ms |
11304 KB |
Output isn't correct |
42 |
Incorrect |
16 ms |
11304 KB |
Output isn't correct |
43 |
Incorrect |
34 ms |
11304 KB |
Output isn't correct |
44 |
Incorrect |
441 ms |
12172 KB |
Output isn't correct |
45 |
Incorrect |
24 ms |
12172 KB |
Output isn't correct |
46 |
Incorrect |
41 ms |
12172 KB |
Output isn't correct |
47 |
Incorrect |
523 ms |
13316 KB |
Output isn't correct |
48 |
Incorrect |
17 ms |
13316 KB |
Output isn't correct |
49 |
Incorrect |
30 ms |
13316 KB |
Output isn't correct |
50 |
Incorrect |
685 ms |
14548 KB |
Output isn't correct |
51 |
Incorrect |
20 ms |
14548 KB |
Output isn't correct |
52 |
Incorrect |
21 ms |
14548 KB |
Output isn't correct |
53 |
Incorrect |
688 ms |
15968 KB |
Output isn't correct |
54 |
Incorrect |
21 ms |
15968 KB |
Output isn't correct |
55 |
Incorrect |
30 ms |
15968 KB |
Output isn't correct |
56 |
Incorrect |
912 ms |
17752 KB |
Output isn't correct |
57 |
Incorrect |
20 ms |
17752 KB |
Output isn't correct |
58 |
Incorrect |
24 ms |
17752 KB |
Output isn't correct |
59 |
Execution timed out |
1006 ms |
19740 KB |
Time limit exceeded |
60 |
Incorrect |
19 ms |
19740 KB |
Output isn't correct |
61 |
Incorrect |
21 ms |
19740 KB |
Output isn't correct |
62 |
Execution timed out |
1074 ms |
21900 KB |
Time limit exceeded |
63 |
Incorrect |
761 ms |
21900 KB |
Output isn't correct |
64 |
Execution timed out |
1020 ms |
22832 KB |
Time limit exceeded |
65 |
Incorrect |
987 ms |
22832 KB |
Output isn't correct |
66 |
Incorrect |
681 ms |
22832 KB |
Output isn't correct |
67 |
Execution timed out |
1006 ms |
23208 KB |
Time limit exceeded |
68 |
Incorrect |
325 ms |
24108 KB |
Output isn't correct |
69 |
Incorrect |
389 ms |
24616 KB |
Output isn't correct |
70 |
Incorrect |
317 ms |
25304 KB |
Output isn't correct |
71 |
Incorrect |
375 ms |
25864 KB |
Output isn't correct |
72 |
Correct |
354 ms |
26416 KB |
Output is correct |
73 |
Correct |
555 ms |
27252 KB |
Output is correct |
74 |
Correct |
541 ms |
28280 KB |
Output is correct |
75 |
Incorrect |
521 ms |
28508 KB |
Output isn't correct |
76 |
Incorrect |
512 ms |
29136 KB |
Output isn't correct |
77 |
Incorrect |
812 ms |
29780 KB |
Output isn't correct |
78 |
Incorrect |
606 ms |
30536 KB |
Output isn't correct |
79 |
Correct |
595 ms |
32184 KB |
Output is correct |
80 |
Incorrect |
559 ms |
32184 KB |
Output isn't correct |
81 |
Incorrect |
542 ms |
32292 KB |
Output isn't correct |
82 |
Incorrect |
494 ms |
33020 KB |
Output isn't correct |
83 |
Incorrect |
719 ms |
33680 KB |
Output isn't correct |
84 |
Correct |
723 ms |
35140 KB |
Output is correct |
85 |
Incorrect |
810 ms |
35140 KB |
Output isn't correct |
86 |
Incorrect |
751 ms |
35564 KB |
Output isn't correct |
87 |
Incorrect |
811 ms |
36136 KB |
Output isn't correct |
88 |
Incorrect |
626 ms |
36708 KB |
Output isn't correct |
89 |
Correct |
574 ms |
38448 KB |
Output is correct |
90 |
Incorrect |
641 ms |
38448 KB |
Output isn't correct |
91 |
Incorrect |
681 ms |
38956 KB |
Output isn't correct |
92 |
Incorrect |
712 ms |
39236 KB |
Output isn't correct |