#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=800;
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]>(floor((dist[curNode.first][curNode.second]+1)/S)+k)){
//cout<<newR<<" "<<newC<<endl;
dist[newR][newC]=dist[curNode.first][curNode.second]+1;
cur.push({newR,newC});
}
}
}
}
if(dist[end2.first][end2.second]!=(-1)){
return true;
}
return false;
}
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];
int distMan[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]!="M" && distBees[newR][newC]==(-1)){
distBees[newR][newC]=distBees[curR][curC]+1;
cur.push({newR,newC});
}
}
}
//return 0;
cout<<last_true(2,1000000,grid,distBees)<<endl;
return 0;
}
Compilation message
mecho.cpp: In function 'int32_t main()':
mecho.cpp:95:6: warning: unused variable 'distMan' [-Wunused-variable]
95 | int distMan[NMAX][NMAX];
| ^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
25436 KB |
Output is correct |
2 |
Correct |
17 ms |
25436 KB |
Output is correct |
3 |
Correct |
16 ms |
25432 KB |
Output is correct |
4 |
Incorrect |
17 ms |
25500 KB |
Output isn't correct |
5 |
Incorrect |
16 ms |
25436 KB |
Output isn't correct |
6 |
Correct |
16 ms |
25436 KB |
Output is correct |
7 |
Incorrect |
191 ms |
31256 KB |
Output isn't correct |
8 |
Incorrect |
16 ms |
25436 KB |
Output isn't correct |
9 |
Incorrect |
16 ms |
25436 KB |
Output isn't correct |
10 |
Correct |
16 ms |
25432 KB |
Output is correct |
11 |
Correct |
16 ms |
25432 KB |
Output is correct |
12 |
Incorrect |
17 ms |
25436 KB |
Output isn't correct |
13 |
Incorrect |
16 ms |
25436 KB |
Output isn't correct |
14 |
Incorrect |
21 ms |
25544 KB |
Output isn't correct |
15 |
Incorrect |
16 ms |
25436 KB |
Output isn't correct |
16 |
Incorrect |
17 ms |
25420 KB |
Output isn't correct |
17 |
Incorrect |
17 ms |
25436 KB |
Output isn't correct |
18 |
Incorrect |
16 ms |
25436 KB |
Output isn't correct |
19 |
Incorrect |
18 ms |
25348 KB |
Output isn't correct |
20 |
Incorrect |
16 ms |
25500 KB |
Output isn't correct |
21 |
Incorrect |
17 ms |
25432 KB |
Output isn't correct |
22 |
Incorrect |
17 ms |
25436 KB |
Output isn't correct |
23 |
Incorrect |
17 ms |
25504 KB |
Output isn't correct |
24 |
Incorrect |
17 ms |
25520 KB |
Output isn't correct |
25 |
Incorrect |
16 ms |
25436 KB |
Output isn't correct |
26 |
Incorrect |
17 ms |
25436 KB |
Output isn't correct |
27 |
Incorrect |
17 ms |
25404 KB |
Output isn't correct |
28 |
Incorrect |
17 ms |
25432 KB |
Output isn't correct |
29 |
Incorrect |
19 ms |
25324 KB |
Output isn't correct |
30 |
Incorrect |
17 ms |
25436 KB |
Output isn't correct |
31 |
Incorrect |
18 ms |
25436 KB |
Output isn't correct |
32 |
Incorrect |
17 ms |
25508 KB |
Output isn't correct |
33 |
Incorrect |
26 ms |
26368 KB |
Output isn't correct |
34 |
Incorrect |
28 ms |
26456 KB |
Output isn't correct |
35 |
Correct |
70 ms |
26572 KB |
Output is correct |
36 |
Incorrect |
31 ms |
26964 KB |
Output isn't correct |
37 |
Incorrect |
30 ms |
26708 KB |
Output isn't correct |
38 |
Correct |
73 ms |
26908 KB |
Output is correct |
39 |
Incorrect |
32 ms |
27288 KB |
Output isn't correct |
40 |
Incorrect |
41 ms |
27220 KB |
Output isn't correct |
41 |
Correct |
118 ms |
27260 KB |
Output is correct |
42 |
Incorrect |
41 ms |
27640 KB |
Output isn't correct |
43 |
Incorrect |
38 ms |
27472 KB |
Output isn't correct |
44 |
Correct |
116 ms |
27712 KB |
Output is correct |
45 |
Incorrect |
44 ms |
28176 KB |
Output isn't correct |
46 |
Incorrect |
50 ms |
27988 KB |
Output isn't correct |
47 |
Correct |
173 ms |
28380 KB |
Output is correct |
48 |
Incorrect |
44 ms |
28496 KB |
Output isn't correct |
49 |
Incorrect |
69 ms |
28500 KB |
Output isn't correct |
50 |
Correct |
213 ms |
28500 KB |
Output is correct |
51 |
Incorrect |
61 ms |
28992 KB |
Output isn't correct |
52 |
Incorrect |
72 ms |
29000 KB |
Output isn't correct |
53 |
Correct |
216 ms |
29008 KB |
Output is correct |
54 |
Incorrect |
54 ms |
29576 KB |
Output isn't correct |
55 |
Incorrect |
75 ms |
29840 KB |
Output isn't correct |
56 |
Correct |
265 ms |
29844 KB |
Output is correct |
57 |
Incorrect |
71 ms |
30416 KB |
Output isn't correct |
58 |
Incorrect |
84 ms |
30412 KB |
Output isn't correct |
59 |
Correct |
290 ms |
30436 KB |
Output is correct |
60 |
Incorrect |
70 ms |
31116 KB |
Output isn't correct |
61 |
Incorrect |
91 ms |
31044 KB |
Output isn't correct |
62 |
Correct |
378 ms |
31020 KB |
Output is correct |
63 |
Incorrect |
85 ms |
31056 KB |
Output isn't correct |
64 |
Incorrect |
258 ms |
31152 KB |
Output isn't correct |
65 |
Incorrect |
233 ms |
31004 KB |
Output isn't correct |
66 |
Incorrect |
92 ms |
31188 KB |
Output isn't correct |
67 |
Incorrect |
113 ms |
31060 KB |
Output isn't correct |
68 |
Incorrect |
90 ms |
31316 KB |
Output isn't correct |
69 |
Incorrect |
87 ms |
31316 KB |
Output isn't correct |
70 |
Incorrect |
86 ms |
31376 KB |
Output isn't correct |
71 |
Incorrect |
86 ms |
31312 KB |
Output isn't correct |
72 |
Incorrect |
84 ms |
31240 KB |
Output isn't correct |
73 |
Incorrect |
101 ms |
31564 KB |
Output isn't correct |
74 |
Correct |
146 ms |
31564 KB |
Output is correct |
75 |
Correct |
168 ms |
31572 KB |
Output is correct |
76 |
Correct |
135 ms |
31656 KB |
Output is correct |
77 |
Correct |
176 ms |
31572 KB |
Output is correct |
78 |
Incorrect |
189 ms |
31648 KB |
Output isn't correct |
79 |
Correct |
132 ms |
31568 KB |
Output is correct |
80 |
Correct |
145 ms |
31528 KB |
Output is correct |
81 |
Correct |
186 ms |
31816 KB |
Output is correct |
82 |
Correct |
149 ms |
31568 KB |
Output is correct |
83 |
Incorrect |
189 ms |
31424 KB |
Output isn't correct |
84 |
Correct |
198 ms |
31568 KB |
Output is correct |
85 |
Correct |
177 ms |
31416 KB |
Output is correct |
86 |
Correct |
176 ms |
31568 KB |
Output is correct |
87 |
Correct |
168 ms |
31432 KB |
Output is correct |
88 |
Incorrect |
202 ms |
31472 KB |
Output isn't correct |
89 |
Correct |
190 ms |
31324 KB |
Output is correct |
90 |
Correct |
208 ms |
31472 KB |
Output is correct |
91 |
Correct |
194 ms |
31328 KB |
Output is correct |
92 |
Correct |
197 ms |
31656 KB |
Output is correct |