#include <iostream>
#include <vector>
#include <queue>
using namespace std;
void bfs(queue<int>& q,vector<int>& dist,vector<vector<int>>& g,int b){
int n=dist.size();
while(!q.empty()){
int u=q.front();
q.pop();
for(int v:g[u]){
if(dist[v]==-1&&v!=b){
dist[v]=dist[u]+1;
q.push(v);
}
}
}
}
bool pos(int a,int b,int mid,int s,vector<vector<int>>& g,vector<int>& dist){
int n=dist.size();
vector<int> d(n,-1);
queue<int> q;
q.push(a);
//cout<<mid<<'\n';
if(mid>dist[a])return false;
d[a]=0;
while(!q.empty()){
int u=q.front();
if(u==b)return true;
q.pop();
//cout<<d[u].first<<' ';
for(int v:g[u]){
if(d[v]==-1&&(d[u]+1)/s+mid<dist[v]){
d[v]=d[u]+1;
q.push(v);
}
}
}
return false;
}
int main(){
int n,s,u,v;
char c;
cin>>n>>s;
vector<vector<int>> g(n*n,vector<int>());
vector<vector<char>> grid(n,vector<char>(n));
vector<int> dist(n*n,-1);
queue<int> q;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>grid[i][j];
if(grid[i][j]=='T')continue;
else{
if(i&&grid[i-1][j]!='T'){
g[i*n+j].push_back(i*n+j-n);
g[i*n+j-n].push_back(i*n+j);
}
if(j&&grid[i][j-1]!='T'){
g[i*n+j].push_back(i*n+j-1);
g[i*n+j-1].push_back(i*n+j);
}
if(grid[i][j]=='D'){
v=i*n+j;
dist[v]=1e9;
}
if(grid[i][j]=='M')u=i*n+j;
if(grid[i][j]=='H'){
q.push(i*n+j);
dist[i*n+j]=0;
}
}
}
}
bfs(q,dist,g,v);
int l=0,r=1e9,ans=-1;
while(l<=r){
int mid=(r+l)/2;
if(pos(u,v,mid,s,g,dist)){
l=mid+1;
ans=mid;
}
else r=mid-1;
}
cout<<ans;
}
Compilation message
mecho.cpp: In function 'void bfs(std::queue<int>&, std::vector<int>&, std::vector<std::vector<int> >&, int)':
mecho.cpp:6:9: warning: unused variable 'n' [-Wunused-variable]
6 | int n=dist.size();
| ^
mecho.cpp: In function 'int main()':
mecho.cpp:42:10: warning: unused variable 'c' [-Wunused-variable]
42 | char c;
| ^
mecho.cpp:77:15: warning: 'v' may be used uninitialized in this function [-Wmaybe-uninitialized]
77 | if(pos(u,v,mid,s,g,dist)){
| ~~~^~~~~~~~~~~~~~~~~~
mecho.cpp:77:15: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
300 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
342 ms |
41328 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
300 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
428 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
432 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
17 ms |
6228 KB |
Output is correct |
34 |
Correct |
15 ms |
6260 KB |
Output is correct |
35 |
Correct |
55 ms |
7252 KB |
Output is correct |
36 |
Correct |
21 ms |
8140 KB |
Output is correct |
37 |
Correct |
23 ms |
8112 KB |
Output is correct |
38 |
Correct |
58 ms |
9304 KB |
Output is correct |
39 |
Correct |
25 ms |
10216 KB |
Output is correct |
40 |
Correct |
26 ms |
10156 KB |
Output is correct |
41 |
Correct |
92 ms |
11760 KB |
Output is correct |
42 |
Correct |
30 ms |
12536 KB |
Output is correct |
43 |
Correct |
31 ms |
12536 KB |
Output is correct |
44 |
Correct |
119 ms |
14488 KB |
Output is correct |
45 |
Correct |
40 ms |
15052 KB |
Output is correct |
46 |
Correct |
41 ms |
15044 KB |
Output is correct |
47 |
Correct |
143 ms |
17420 KB |
Output is correct |
48 |
Correct |
49 ms |
17836 KB |
Output is correct |
49 |
Correct |
45 ms |
17804 KB |
Output is correct |
50 |
Correct |
176 ms |
20576 KB |
Output is correct |
51 |
Correct |
52 ms |
20808 KB |
Output is correct |
52 |
Correct |
56 ms |
20816 KB |
Output is correct |
53 |
Correct |
199 ms |
24244 KB |
Output is correct |
54 |
Correct |
65 ms |
24028 KB |
Output is correct |
55 |
Correct |
61 ms |
24016 KB |
Output is correct |
56 |
Correct |
257 ms |
27884 KB |
Output is correct |
57 |
Correct |
71 ms |
27640 KB |
Output is correct |
58 |
Correct |
88 ms |
27512 KB |
Output is correct |
59 |
Correct |
313 ms |
31928 KB |
Output is correct |
60 |
Correct |
81 ms |
31244 KB |
Output is correct |
61 |
Correct |
84 ms |
31332 KB |
Output is correct |
62 |
Correct |
365 ms |
36256 KB |
Output is correct |
63 |
Correct |
238 ms |
33208 KB |
Output is correct |
64 |
Correct |
323 ms |
33188 KB |
Output is correct |
65 |
Correct |
329 ms |
33264 KB |
Output is correct |
66 |
Correct |
248 ms |
33208 KB |
Output is correct |
67 |
Correct |
238 ms |
33208 KB |
Output is correct |
68 |
Correct |
151 ms |
33208 KB |
Output is correct |
69 |
Correct |
164 ms |
33212 KB |
Output is correct |
70 |
Correct |
143 ms |
33164 KB |
Output is correct |
71 |
Correct |
144 ms |
33208 KB |
Output is correct |
72 |
Incorrect |
127 ms |
33196 KB |
Output isn't correct |
73 |
Incorrect |
218 ms |
41260 KB |
Output isn't correct |
74 |
Correct |
243 ms |
41212 KB |
Output is correct |
75 |
Correct |
244 ms |
41224 KB |
Output is correct |
76 |
Correct |
260 ms |
41160 KB |
Output is correct |
77 |
Correct |
245 ms |
41152 KB |
Output is correct |
78 |
Correct |
236 ms |
41164 KB |
Output is correct |
79 |
Correct |
207 ms |
41332 KB |
Output is correct |
80 |
Correct |
227 ms |
41196 KB |
Output is correct |
81 |
Correct |
243 ms |
41168 KB |
Output is correct |
82 |
Correct |
204 ms |
41172 KB |
Output is correct |
83 |
Correct |
343 ms |
41124 KB |
Output is correct |
84 |
Correct |
278 ms |
41108 KB |
Output is correct |
85 |
Correct |
323 ms |
41156 KB |
Output is correct |
86 |
Correct |
316 ms |
41132 KB |
Output is correct |
87 |
Correct |
300 ms |
41148 KB |
Output is correct |
88 |
Correct |
335 ms |
41184 KB |
Output is correct |
89 |
Correct |
364 ms |
41244 KB |
Output is correct |
90 |
Correct |
326 ms |
41160 KB |
Output is correct |
91 |
Correct |
268 ms |
41172 KB |
Output is correct |
92 |
Correct |
342 ms |
41264 KB |
Output is correct |