#include <iostream>
#include <vector>
#include <queue>
using namespace std;
void bfs(queue<int>& q,vector<int>& dist,vector<vector<int>>& g){
int n=dist.size();
while(!q.empty()){
int u=q.front();
//cout<<u<<' ';
q.pop();
for(int v:g[u]){
if(dist[v]==-1){
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<pair<int,int>> d(n,{-1,-1});
queue<int> q;
n=7;
q.push(a);
//cout<<mid<<'\n';
d[a].first=mid;
d[a].second=-1;
while(!q.empty()){
int u=q.front();
q.pop();
//cout<<d[u].first<<' ';
for(int v:g[u]){
if(d[v].first==-1){
d[v]=d[u];
d[v].second++;
if(d[v].second==s){
d[v].second=0;
d[v].first++;
}
if(d[v].first<dist[v])q.push(v);
}
}
}
/*for(int i=0;i<n;i++){
for(int j=0;j<n;j++)cout<<d[i*n+j].first<<' ';
cout<<'\n';
}*/
if(d[b].first==-1)return false;
return true;
}
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);
/*for(int i=0;i<n;i++){
for(int j=0;j<n;j++)cout<<dist[i*n+j]<<' ';
cout<<'\n';
}*/
int l=0,r=1e6,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> >&)':
mecho.cpp:6:9: warning: unused variable 'n' [-Wunused-variable]
6 | int n=dist.size();
| ^
mecho.cpp: In function 'int main()':
mecho.cpp:53:10: warning: unused variable 'c' [-Wunused-variable]
53 | char c;
| ^
mecho.cpp:92:15: warning: 'v' may be used uninitialized in this function [-Wmaybe-uninitialized]
92 | if(pos(u,v,mid,s,g,dist)){
| ~~~^~~~~~~~~~~~~~~~~~
mecho.cpp:92:15: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
304 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
304 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
283 ms |
44208 KB |
Output is correct |
8 |
Incorrect |
1 ms |
300 KB |
Output isn't correct |
9 |
Correct |
1 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 |
2 ms |
468 KB |
Output is correct |
15 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Incorrect |
1 ms |
300 KB |
Output isn't correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
26 |
Correct |
1 ms |
424 KB |
Output is correct |
27 |
Incorrect |
1 ms |
428 KB |
Output isn't correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Incorrect |
1 ms |
560 KB |
Output isn't correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Incorrect |
18 ms |
6728 KB |
Output isn't correct |
34 |
Correct |
18 ms |
6768 KB |
Output is correct |
35 |
Correct |
46 ms |
7728 KB |
Output is correct |
36 |
Incorrect |
22 ms |
8680 KB |
Output isn't correct |
37 |
Correct |
30 ms |
8680 KB |
Output is correct |
38 |
Correct |
78 ms |
9944 KB |
Output is correct |
39 |
Incorrect |
28 ms |
10956 KB |
Output isn't correct |
40 |
Correct |
28 ms |
10956 KB |
Output is correct |
41 |
Correct |
88 ms |
12516 KB |
Output is correct |
42 |
Incorrect |
36 ms |
13516 KB |
Output isn't correct |
43 |
Correct |
42 ms |
13480 KB |
Output is correct |
44 |
Correct |
119 ms |
15456 KB |
Output is correct |
45 |
Incorrect |
42 ms |
16272 KB |
Output isn't correct |
46 |
Correct |
43 ms |
16440 KB |
Output is correct |
47 |
Correct |
144 ms |
18584 KB |
Output is correct |
48 |
Incorrect |
61 ms |
19316 KB |
Output isn't correct |
49 |
Correct |
58 ms |
19296 KB |
Output is correct |
50 |
Correct |
170 ms |
22136 KB |
Output is correct |
51 |
Incorrect |
56 ms |
22652 KB |
Output isn't correct |
52 |
Correct |
69 ms |
22584 KB |
Output is correct |
53 |
Correct |
210 ms |
25928 KB |
Output is correct |
54 |
Incorrect |
68 ms |
26132 KB |
Output isn't correct |
55 |
Correct |
69 ms |
26228 KB |
Output is correct |
56 |
Correct |
250 ms |
30044 KB |
Output is correct |
57 |
Incorrect |
84 ms |
30156 KB |
Output isn't correct |
58 |
Correct |
77 ms |
30056 KB |
Output is correct |
59 |
Correct |
293 ms |
34448 KB |
Output is correct |
60 |
Incorrect |
93 ms |
34144 KB |
Output isn't correct |
61 |
Correct |
93 ms |
34116 KB |
Output is correct |
62 |
Correct |
352 ms |
39128 KB |
Output is correct |
63 |
Correct |
263 ms |
36072 KB |
Output is correct |
64 |
Correct |
316 ms |
36140 KB |
Output is correct |
65 |
Correct |
299 ms |
35984 KB |
Output is correct |
66 |
Incorrect |
283 ms |
36080 KB |
Output isn't correct |
67 |
Correct |
256 ms |
36020 KB |
Output is correct |
68 |
Correct |
170 ms |
36092 KB |
Output is correct |
69 |
Correct |
163 ms |
36092 KB |
Output is correct |
70 |
Correct |
162 ms |
36100 KB |
Output is correct |
71 |
Correct |
145 ms |
36224 KB |
Output is correct |
72 |
Incorrect |
149 ms |
36112 KB |
Output isn't correct |
73 |
Incorrect |
210 ms |
44176 KB |
Output isn't correct |
74 |
Correct |
241 ms |
44096 KB |
Output is correct |
75 |
Correct |
257 ms |
44040 KB |
Output is correct |
76 |
Correct |
232 ms |
44108 KB |
Output is correct |
77 |
Correct |
266 ms |
44060 KB |
Output is correct |
78 |
Correct |
259 ms |
44092 KB |
Output is correct |
79 |
Correct |
222 ms |
44012 KB |
Output is correct |
80 |
Correct |
247 ms |
44108 KB |
Output is correct |
81 |
Correct |
240 ms |
44164 KB |
Output is correct |
82 |
Correct |
238 ms |
44100 KB |
Output is correct |
83 |
Correct |
282 ms |
44024 KB |
Output is correct |
84 |
Correct |
266 ms |
44080 KB |
Output is correct |
85 |
Correct |
291 ms |
44000 KB |
Output is correct |
86 |
Correct |
253 ms |
44012 KB |
Output is correct |
87 |
Correct |
300 ms |
44024 KB |
Output is correct |
88 |
Correct |
304 ms |
44124 KB |
Output is correct |
89 |
Correct |
296 ms |
44040 KB |
Output is correct |
90 |
Correct |
300 ms |
43980 KB |
Output is correct |
91 |
Correct |
288 ms |
44012 KB |
Output is correct |
92 |
Correct |
318 ms |
44016 KB |
Output is correct |