#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <cassert>
#include <math.h>
using namespace std;
#define endl "\n"
#define int int64_t
#define pb push_back
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define FOR0(i,a) FOR(i,0,a)
#define FOR1(i,a) for (int i = (1); i <= (a); ++i)
#define TRAV(a,x) for (auto& a: x)
using pii = pair<int,int>;
using vi = vector<int>;
vi dx = {-1,0,1,0};
vi dy = {0,-1,0,1};
const int NMAX=801;
int N;
int S;
pii start={0,0};
pii end2={0,0};
bool f(int k,string grid[NMAX][NMAX],int distBees[NMAX][NMAX]){
queue<pii> cur;
cur.push(start);
int dist[N][N];
FOR0(i,N){
FOR0(j,N){
dist[i][j]=-1;
}
}
dist[start.first][start.second]=0;
while(!cur.empty()){
pii curNode=cur.front();
cur.pop();
FOR0(i,4){
int newR=curNode.first+dx[i];
int newC=curNode.second+dy[i];
if(newR<0 || newR>=N || newC<0 || newC>=N){continue;}
if((grid[newR][newC]=="G" || grid[newR][newC]=="D") && dist[newR][newC]==(-1)){
if((distBees[newR][newC]-k)>((dist[curNode.first][curNode.second]+1)/S)){
//cout<<newR<<" "<<newC<<endl;
dist[newR][newC]=dist[curNode.first][curNode.second]+1;
cur.push({newR,newC});
}
}
}
}
// FOR0(i,N){
// FOR0(j,N){
// cout<<dist[i][j]<<" ";
// }
// cout<<endl;
// }
bool ans=false;
FOR0(i,4){
int newR=end2.first+dx[i];
int newC=end2.second+dy[i];
if(newR<0 || newR>=N || newC<0 || newC>=N){continue;}
if((grid[newR][newC]=="G" || grid[newR][newC]=="D") && dist[newR][newC]!=(-1)){
//cout<<newR<<" "<<newC<<endl;
ans=true;
}
}
return ans;
}
int last_true(int lo, int hi,string grid[NMAX][NMAX],int distBees[NMAX][NMAX]) {
// if none of the values in the range work, return lo - 1
lo--;
while (lo < hi) {
// find the middle of the current range (rounding up)
int mid = lo + (hi - lo + 1) / 2;
if (f(mid,grid,distBees)) {
// if mid works, then all numbers smaller than mid also work
lo = mid;
} else {
// if mid does not work, greater values would not work either
hi = mid - 1;
}
}
return lo;
}
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
// cout<<"yo"<<endl;
// return 0;
cin>>N>>S;
string grid[NMAX][NMAX];
int distBees[NMAX][NMAX];
queue<pii> cur;
FOR0(r,N){
string in;
cin>>in;
FOR0(c,N){
distBees[r][c]=-1;
grid[r][c]=(in.at(c));
if(grid[r][c]=="H"){
cur.push({r,c});
distBees[r][c]=0;
}
if(grid[r][c]=="M"){
start={r,c};
}
if(grid[r][c]=="D"){
end2={r,c};
}
}
}
// return 0;
while(!cur.empty()){
pii curNode=cur.front();
int curR=curNode.first;
int curC=curNode.second;
cur.pop();
FOR0(i,4){
int newR=curNode.first+dx[i];
int newC=curNode.second+dy[i];
if(newR<0 || newR>=N || newC<0 || newC>=N){continue;}
if(grid[newR][newC]!="T" && distBees[newR][newC]==(-1)){
distBees[newR][newC]=distBees[curR][curC]+1;
cur.push({newR,newC});
}
}
}
// FOR0(i,N){
// FOR0(j,N){
// cout<<distBees[i][j]<<" ";
// }
// cout<<endl;
// }
// cout<<endl;
// cout<<f(1,grid,distBees)<<endl;
// return 0;
int ans=last_true(0,100000000,grid,distBees);
if(ans==100000000){
ans=-1;
}
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
25436 KB |
Output is correct |
2 |
Correct |
17 ms |
25564 KB |
Output is correct |
3 |
Correct |
14 ms |
25436 KB |
Output is correct |
4 |
Correct |
14 ms |
25436 KB |
Output is correct |
5 |
Correct |
16 ms |
25464 KB |
Output is correct |
6 |
Correct |
14 ms |
25432 KB |
Output is correct |
7 |
Correct |
188 ms |
30804 KB |
Output is correct |
8 |
Correct |
18 ms |
25692 KB |
Output is correct |
9 |
Correct |
14 ms |
25436 KB |
Output is correct |
10 |
Correct |
16 ms |
25316 KB |
Output is correct |
11 |
Correct |
15 ms |
25360 KB |
Output is correct |
12 |
Incorrect |
14 ms |
25432 KB |
Output isn't correct |
13 |
Incorrect |
15 ms |
25432 KB |
Output isn't correct |
14 |
Correct |
15 ms |
25432 KB |
Output is correct |
15 |
Correct |
14 ms |
25436 KB |
Output is correct |
16 |
Correct |
16 ms |
25436 KB |
Output is correct |
17 |
Correct |
17 ms |
25564 KB |
Output is correct |
18 |
Correct |
17 ms |
25436 KB |
Output is correct |
19 |
Correct |
19 ms |
25436 KB |
Output is correct |
20 |
Correct |
17 ms |
25424 KB |
Output is correct |
21 |
Correct |
15 ms |
25436 KB |
Output is correct |
22 |
Correct |
14 ms |
25436 KB |
Output is correct |
23 |
Correct |
15 ms |
25436 KB |
Output is correct |
24 |
Correct |
15 ms |
25436 KB |
Output is correct |
25 |
Correct |
15 ms |
25568 KB |
Output is correct |
26 |
Correct |
15 ms |
25436 KB |
Output is correct |
27 |
Correct |
14 ms |
25432 KB |
Output is correct |
28 |
Correct |
15 ms |
25432 KB |
Output is correct |
29 |
Correct |
15 ms |
25432 KB |
Output is correct |
30 |
Correct |
15 ms |
25436 KB |
Output is correct |
31 |
Correct |
15 ms |
25436 KB |
Output is correct |
32 |
Correct |
15 ms |
25524 KB |
Output is correct |
33 |
Correct |
22 ms |
26328 KB |
Output is correct |
34 |
Correct |
26 ms |
26460 KB |
Output is correct |
35 |
Correct |
52 ms |
26332 KB |
Output is correct |
36 |
Correct |
30 ms |
26708 KB |
Output is correct |
37 |
Correct |
29 ms |
26716 KB |
Output is correct |
38 |
Correct |
71 ms |
26804 KB |
Output is correct |
39 |
Correct |
29 ms |
26972 KB |
Output is correct |
40 |
Correct |
30 ms |
27120 KB |
Output is correct |
41 |
Correct |
87 ms |
27152 KB |
Output is correct |
42 |
Correct |
33 ms |
27484 KB |
Output is correct |
43 |
Correct |
34 ms |
27488 KB |
Output is correct |
44 |
Correct |
113 ms |
27364 KB |
Output is correct |
45 |
Correct |
34 ms |
27928 KB |
Output is correct |
46 |
Correct |
35 ms |
28080 KB |
Output is correct |
47 |
Correct |
147 ms |
27940 KB |
Output is correct |
48 |
Correct |
38 ms |
28500 KB |
Output is correct |
49 |
Correct |
43 ms |
28248 KB |
Output is correct |
50 |
Correct |
176 ms |
28392 KB |
Output is correct |
51 |
Correct |
40 ms |
28756 KB |
Output is correct |
52 |
Correct |
40 ms |
28756 KB |
Output is correct |
53 |
Correct |
187 ms |
28896 KB |
Output is correct |
54 |
Correct |
46 ms |
29264 KB |
Output is correct |
55 |
Correct |
53 ms |
29264 KB |
Output is correct |
56 |
Correct |
218 ms |
29264 KB |
Output is correct |
57 |
Correct |
53 ms |
29952 KB |
Output is correct |
58 |
Correct |
55 ms |
29848 KB |
Output is correct |
59 |
Correct |
273 ms |
29988 KB |
Output is correct |
60 |
Correct |
57 ms |
30396 KB |
Output is correct |
61 |
Correct |
70 ms |
30548 KB |
Output is correct |
62 |
Correct |
336 ms |
30448 KB |
Output is correct |
63 |
Correct |
215 ms |
30584 KB |
Output is correct |
64 |
Correct |
271 ms |
30548 KB |
Output is correct |
65 |
Correct |
343 ms |
30800 KB |
Output is correct |
66 |
Correct |
257 ms |
30584 KB |
Output is correct |
67 |
Correct |
261 ms |
30580 KB |
Output is correct |
68 |
Correct |
103 ms |
30492 KB |
Output is correct |
69 |
Correct |
111 ms |
30488 KB |
Output is correct |
70 |
Correct |
101 ms |
30640 KB |
Output is correct |
71 |
Correct |
85 ms |
30632 KB |
Output is correct |
72 |
Incorrect |
78 ms |
30488 KB |
Output isn't correct |
73 |
Incorrect |
103 ms |
30924 KB |
Output isn't correct |
74 |
Correct |
138 ms |
30936 KB |
Output is correct |
75 |
Correct |
146 ms |
30992 KB |
Output is correct |
76 |
Correct |
160 ms |
31088 KB |
Output is correct |
77 |
Correct |
145 ms |
31060 KB |
Output is correct |
78 |
Correct |
177 ms |
31232 KB |
Output is correct |
79 |
Correct |
139 ms |
31060 KB |
Output is correct |
80 |
Correct |
143 ms |
30984 KB |
Output is correct |
81 |
Correct |
174 ms |
31084 KB |
Output is correct |
82 |
Correct |
164 ms |
31056 KB |
Output is correct |
83 |
Correct |
165 ms |
30800 KB |
Output is correct |
84 |
Correct |
162 ms |
31000 KB |
Output is correct |
85 |
Correct |
150 ms |
30996 KB |
Output is correct |
86 |
Correct |
183 ms |
30864 KB |
Output is correct |
87 |
Correct |
199 ms |
30804 KB |
Output is correct |
88 |
Correct |
168 ms |
30804 KB |
Output is correct |
89 |
Correct |
207 ms |
30804 KB |
Output is correct |
90 |
Correct |
183 ms |
30800 KB |
Output is correct |
91 |
Correct |
230 ms |
30968 KB |
Output is correct |
92 |
Correct |
192 ms |
30756 KB |
Output is correct |