#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<pair<int,int>> d(n,{-1,-1});
queue<int> q;
q.push(a);
//cout<<mid<<'\n';
if(mid>=dist[a])return false;
d[a].first=mid;
d[a].second=-1;
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].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);
}
}
}
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:48:10: warning: unused variable 'c' [-Wunused-variable]
48 | char c;
| ^
mecho.cpp:83:15: warning: 'v' may be used uninitialized in this function [-Wmaybe-uninitialized]
83 | if(pos(u,v,mid,s,g,dist)){
| ~~~^~~~~~~~~~~~~~~~~~
mecho.cpp:83:15: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
256 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
212 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 |
212 KB |
Output is correct |
7 |
Correct |
335 ms |
43708 KB |
Output is correct |
8 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
16 |
Correct |
1 ms |
296 KB |
Output is correct |
17 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
18 |
Correct |
1 ms |
300 KB |
Output is correct |
19 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
20 |
Correct |
1 ms |
296 KB |
Output is correct |
21 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Incorrect |
1 ms |
432 KB |
Output isn't correct |
24 |
Correct |
1 ms |
424 KB |
Output is correct |
25 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
26 |
Correct |
1 ms |
428 KB |
Output is correct |
27 |
Incorrect |
2 ms |
436 KB |
Output isn't correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
30 |
Correct |
1 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 |
17 ms |
6760 KB |
Output isn't correct |
34 |
Correct |
19 ms |
6788 KB |
Output is correct |
35 |
Correct |
65 ms |
7700 KB |
Output is correct |
36 |
Incorrect |
24 ms |
8688 KB |
Output isn't correct |
37 |
Correct |
28 ms |
8792 KB |
Output is correct |
38 |
Correct |
69 ms |
9972 KB |
Output is correct |
39 |
Incorrect |
30 ms |
10944 KB |
Output isn't correct |
40 |
Correct |
30 ms |
10904 KB |
Output is correct |
41 |
Correct |
98 ms |
12516 KB |
Output is correct |
42 |
Incorrect |
38 ms |
13388 KB |
Output isn't correct |
43 |
Correct |
40 ms |
13320 KB |
Output is correct |
44 |
Correct |
115 ms |
15312 KB |
Output is correct |
45 |
Incorrect |
43 ms |
16132 KB |
Output isn't correct |
46 |
Correct |
43 ms |
16112 KB |
Output is correct |
47 |
Correct |
146 ms |
18424 KB |
Output is correct |
48 |
Incorrect |
57 ms |
19000 KB |
Output isn't correct |
49 |
Correct |
50 ms |
18992 KB |
Output is correct |
50 |
Correct |
175 ms |
21832 KB |
Output is correct |
51 |
Incorrect |
63 ms |
22352 KB |
Output isn't correct |
52 |
Correct |
62 ms |
22368 KB |
Output is correct |
53 |
Correct |
208 ms |
25656 KB |
Output is correct |
54 |
Incorrect |
72 ms |
25860 KB |
Output isn't correct |
55 |
Correct |
76 ms |
25876 KB |
Output is correct |
56 |
Correct |
260 ms |
29612 KB |
Output is correct |
57 |
Incorrect |
80 ms |
29608 KB |
Output isn't correct |
58 |
Correct |
81 ms |
29600 KB |
Output is correct |
59 |
Correct |
307 ms |
34084 KB |
Output is correct |
60 |
Incorrect |
105 ms |
33600 KB |
Output isn't correct |
61 |
Correct |
95 ms |
33624 KB |
Output is correct |
62 |
Correct |
367 ms |
38636 KB |
Output is correct |
63 |
Correct |
250 ms |
35532 KB |
Output is correct |
64 |
Correct |
326 ms |
35556 KB |
Output is correct |
65 |
Correct |
348 ms |
35656 KB |
Output is correct |
66 |
Incorrect |
262 ms |
35544 KB |
Output isn't correct |
67 |
Correct |
263 ms |
35584 KB |
Output is correct |
68 |
Correct |
171 ms |
35532 KB |
Output is correct |
69 |
Correct |
169 ms |
35508 KB |
Output is correct |
70 |
Correct |
161 ms |
35604 KB |
Output is correct |
71 |
Correct |
152 ms |
35532 KB |
Output is correct |
72 |
Correct |
142 ms |
35548 KB |
Output is correct |
73 |
Correct |
271 ms |
43516 KB |
Output is correct |
74 |
Correct |
253 ms |
43588 KB |
Output is correct |
75 |
Correct |
262 ms |
43580 KB |
Output is correct |
76 |
Correct |
275 ms |
43596 KB |
Output is correct |
77 |
Correct |
251 ms |
43596 KB |
Output is correct |
78 |
Correct |
276 ms |
43596 KB |
Output is correct |
79 |
Correct |
234 ms |
43584 KB |
Output is correct |
80 |
Correct |
242 ms |
43636 KB |
Output is correct |
81 |
Correct |
235 ms |
43540 KB |
Output is correct |
82 |
Correct |
237 ms |
43596 KB |
Output is correct |
83 |
Correct |
304 ms |
43472 KB |
Output is correct |
84 |
Correct |
266 ms |
43444 KB |
Output is correct |
85 |
Correct |
293 ms |
43528 KB |
Output is correct |
86 |
Correct |
315 ms |
43628 KB |
Output is correct |
87 |
Correct |
278 ms |
43620 KB |
Output is correct |
88 |
Correct |
329 ms |
43648 KB |
Output is correct |
89 |
Correct |
319 ms |
43552 KB |
Output is correct |
90 |
Correct |
327 ms |
43560 KB |
Output is correct |
91 |
Correct |
313 ms |
43576 KB |
Output is correct |
92 |
Correct |
297 ms |
43540 KB |
Output is correct |