#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
const int N=805;
int n,ANS=-1,k,Timer[N][N],F[N][N];
char c[N][N];
pair < int , int > a[6],Home,Honeypot;
vector < pair < int , int > > Hive;
void Bfs () {
queue < pair < int , int > > q;
for (int i=1; i<=n; i++)
for (int j=1; j<=n; j++)
Timer[i][j]=1e9;
for (int i=0; i<Hive.size(); i++)
q.push(Hive[i]),Timer[Hive[i].F][Hive[i].S]=0;
while (!q.empty()) {
pair < int , int > B=q.front();
q.pop();
for (int i=1; i<=4; i++) {
int X=B.F+a[i].F,Y=B.S+a[i].S;
if (!(1<=X && X<=n && 1<=Y && Y<=n)) continue;
if (c[X][Y]=='T' || c[X][Y]=='D' || Timer[X][Y]!=1e9) continue;
Timer[X][Y]=Timer[B.F][B.S]+k;
q.push({X,Y});
}
}
}
bool check(int x) {
if (x*k>=Timer[Honeypot.F][Honeypot.S]) return 0;
queue < pair < pair < int , int > , int > > Q;
while (!Q.empty()) Q.pop();
for (int i=1; i<=n; i++)
for (int j=1; j<=n; j++)
F[i][j]=0;
Q.push({{Honeypot.F,Honeypot.S},(x*k)});
F[Honeypot.F][Honeypot.S]=1;
while (!Q.empty()) {
pair < int , int > D=Q.front().F;
int timer=Q.front().S;
Q.pop();
if (c[D.F][D.S]=='D') return 1;
for (int i=1; i<=4; i++) {
int X=D.F+a[i].F,Y=D.S+a[i].S;
if (!(1<=X && X<=n && 1<=Y && Y<=n)) continue;
if (Timer[X][Y]<=timer+1 || F[X][Y] || c[X][Y]=='T') continue;
Q.push({{X,Y},timer+1});
F[X][Y]=1;
}
}
return 0;
}
main () {
a[1].F=1; a[1].S=0;
a[2].F=-1; a[2].S=0;
a[3].F=0; a[3].S=1;
a[4].F=0; a[4].S=-1;
cin>>n>>k;
for (int i=1; i<=n; i++)
for (int j=1; j<=n; j++){
cin>>c[i][j];
if (c[i][j]=='M') Honeypot.F=i,Honeypot.S=j;
if (c[i][j]=='H') Hive.push_back({i,j});
if (c[i][j]=='D') Home.F=i,Home.S=j;
}
Bfs();
int l=0,r=n*n,mid=0;
while (l<=r) {
mid=(l+r)/2;
if (check(mid)) { ANS=mid; l=mid+1; }
else r=mid-1;
}
cout<<ANS<<endl;
}
Compilation message
mecho.cpp: In function 'void Bfs()':
mecho.cpp:16:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0; i<Hive.size(); i++)
~^~~~~~~~~~~~
mecho.cpp: At global scope:
mecho.cpp:59:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main () {
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 KB |
Output is correct |
3 |
Correct |
2 ms |
408 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
164 ms |
6856 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
764 KB |
Output is correct |
13 |
Correct |
3 ms |
760 KB |
Output is correct |
14 |
Correct |
3 ms |
760 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
18 |
Correct |
2 ms |
504 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
508 KB |
Output is correct |
21 |
Correct |
2 ms |
632 KB |
Output is correct |
22 |
Correct |
3 ms |
680 KB |
Output is correct |
23 |
Correct |
3 ms |
760 KB |
Output is correct |
24 |
Correct |
3 ms |
636 KB |
Output is correct |
25 |
Correct |
3 ms |
760 KB |
Output is correct |
26 |
Correct |
3 ms |
760 KB |
Output is correct |
27 |
Correct |
3 ms |
760 KB |
Output is correct |
28 |
Correct |
3 ms |
888 KB |
Output is correct |
29 |
Correct |
3 ms |
760 KB |
Output is correct |
30 |
Correct |
3 ms |
888 KB |
Output is correct |
31 |
Correct |
3 ms |
888 KB |
Output is correct |
32 |
Correct |
3 ms |
888 KB |
Output is correct |
33 |
Correct |
18 ms |
2872 KB |
Output is correct |
34 |
Correct |
18 ms |
2936 KB |
Output is correct |
35 |
Correct |
36 ms |
2936 KB |
Output is correct |
36 |
Correct |
22 ms |
3320 KB |
Output is correct |
37 |
Correct |
23 ms |
3320 KB |
Output is correct |
38 |
Correct |
47 ms |
3320 KB |
Output is correct |
39 |
Correct |
28 ms |
3704 KB |
Output is correct |
40 |
Correct |
28 ms |
3704 KB |
Output is correct |
41 |
Correct |
64 ms |
3832 KB |
Output is correct |
42 |
Correct |
34 ms |
4220 KB |
Output is correct |
43 |
Correct |
34 ms |
4088 KB |
Output is correct |
44 |
Correct |
83 ms |
4088 KB |
Output is correct |
45 |
Correct |
41 ms |
4600 KB |
Output is correct |
46 |
Correct |
40 ms |
4600 KB |
Output is correct |
47 |
Correct |
92 ms |
4472 KB |
Output is correct |
48 |
Correct |
46 ms |
4944 KB |
Output is correct |
49 |
Correct |
47 ms |
4984 KB |
Output is correct |
50 |
Correct |
112 ms |
4984 KB |
Output is correct |
51 |
Correct |
53 ms |
5368 KB |
Output is correct |
52 |
Correct |
55 ms |
5368 KB |
Output is correct |
53 |
Correct |
142 ms |
5496 KB |
Output is correct |
54 |
Correct |
63 ms |
5752 KB |
Output is correct |
55 |
Correct |
62 ms |
5752 KB |
Output is correct |
56 |
Correct |
169 ms |
5752 KB |
Output is correct |
57 |
Correct |
71 ms |
6136 KB |
Output is correct |
58 |
Correct |
74 ms |
6188 KB |
Output is correct |
59 |
Correct |
193 ms |
6248 KB |
Output is correct |
60 |
Correct |
87 ms |
6652 KB |
Output is correct |
61 |
Correct |
81 ms |
6572 KB |
Output is correct |
62 |
Correct |
243 ms |
6776 KB |
Output is correct |
63 |
Correct |
183 ms |
6748 KB |
Output is correct |
64 |
Correct |
293 ms |
6676 KB |
Output is correct |
65 |
Correct |
238 ms |
6648 KB |
Output is correct |
66 |
Correct |
206 ms |
6756 KB |
Output is correct |
67 |
Correct |
202 ms |
6648 KB |
Output is correct |
68 |
Correct |
113 ms |
6648 KB |
Output is correct |
69 |
Correct |
112 ms |
6652 KB |
Output is correct |
70 |
Correct |
101 ms |
6636 KB |
Output is correct |
71 |
Correct |
105 ms |
6704 KB |
Output is correct |
72 |
Correct |
98 ms |
6648 KB |
Output is correct |
73 |
Correct |
98 ms |
6784 KB |
Output is correct |
74 |
Correct |
135 ms |
6784 KB |
Output is correct |
75 |
Correct |
155 ms |
7040 KB |
Output is correct |
76 |
Correct |
132 ms |
6864 KB |
Output is correct |
77 |
Correct |
127 ms |
6784 KB |
Output is correct |
78 |
Correct |
149 ms |
6832 KB |
Output is correct |
79 |
Correct |
111 ms |
6796 KB |
Output is correct |
80 |
Correct |
127 ms |
6924 KB |
Output is correct |
81 |
Correct |
135 ms |
6796 KB |
Output is correct |
82 |
Correct |
132 ms |
6792 KB |
Output is correct |
83 |
Correct |
152 ms |
6840 KB |
Output is correct |
84 |
Correct |
147 ms |
6840 KB |
Output is correct |
85 |
Correct |
146 ms |
6708 KB |
Output is correct |
86 |
Correct |
147 ms |
6840 KB |
Output is correct |
87 |
Correct |
151 ms |
6708 KB |
Output is correct |
88 |
Correct |
153 ms |
6876 KB |
Output is correct |
89 |
Correct |
157 ms |
6856 KB |
Output is correct |
90 |
Correct |
164 ms |
6944 KB |
Output is correct |
91 |
Correct |
160 ms |
6748 KB |
Output is correct |
92 |
Correct |
177 ms |
6948 KB |
Output is correct |