#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+1LL*mid*s<1LL*dist[v]*s){
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]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
331 ms |
41676 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
260 KB |
Output is correct |
10 |
Correct |
1 ms |
212 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 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
340 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 |
304 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
2 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 |
512 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
560 KB |
Output is correct |
33 |
Correct |
17 ms |
6220 KB |
Output is correct |
34 |
Correct |
16 ms |
6260 KB |
Output is correct |
35 |
Correct |
51 ms |
7248 KB |
Output is correct |
36 |
Correct |
21 ms |
8160 KB |
Output is correct |
37 |
Correct |
20 ms |
8124 KB |
Output is correct |
38 |
Correct |
73 ms |
9300 KB |
Output is correct |
39 |
Correct |
37 ms |
10192 KB |
Output is correct |
40 |
Correct |
31 ms |
10212 KB |
Output is correct |
41 |
Correct |
125 ms |
11724 KB |
Output is correct |
42 |
Correct |
35 ms |
12516 KB |
Output is correct |
43 |
Correct |
34 ms |
12496 KB |
Output is correct |
44 |
Correct |
137 ms |
14448 KB |
Output is correct |
45 |
Correct |
41 ms |
15120 KB |
Output is correct |
46 |
Correct |
43 ms |
15052 KB |
Output is correct |
47 |
Correct |
147 ms |
17436 KB |
Output is correct |
48 |
Correct |
48 ms |
17852 KB |
Output is correct |
49 |
Correct |
49 ms |
17928 KB |
Output is correct |
50 |
Correct |
177 ms |
20720 KB |
Output is correct |
51 |
Correct |
61 ms |
20924 KB |
Output is correct |
52 |
Correct |
55 ms |
20888 KB |
Output is correct |
53 |
Correct |
206 ms |
24228 KB |
Output is correct |
54 |
Correct |
67 ms |
24320 KB |
Output is correct |
55 |
Correct |
62 ms |
24248 KB |
Output is correct |
56 |
Correct |
239 ms |
28140 KB |
Output is correct |
57 |
Correct |
82 ms |
27756 KB |
Output is correct |
58 |
Correct |
73 ms |
27848 KB |
Output is correct |
59 |
Correct |
297 ms |
32212 KB |
Output is correct |
60 |
Correct |
105 ms |
31656 KB |
Output is correct |
61 |
Correct |
88 ms |
31640 KB |
Output is correct |
62 |
Correct |
294 ms |
36632 KB |
Output is correct |
63 |
Correct |
250 ms |
33580 KB |
Output is correct |
64 |
Correct |
337 ms |
33556 KB |
Output is correct |
65 |
Correct |
325 ms |
33672 KB |
Output is correct |
66 |
Correct |
256 ms |
33544 KB |
Output is correct |
67 |
Correct |
232 ms |
33580 KB |
Output is correct |
68 |
Correct |
156 ms |
33568 KB |
Output is correct |
69 |
Correct |
155 ms |
33596 KB |
Output is correct |
70 |
Correct |
145 ms |
33576 KB |
Output is correct |
71 |
Correct |
136 ms |
33700 KB |
Output is correct |
72 |
Correct |
125 ms |
33556 KB |
Output is correct |
73 |
Correct |
212 ms |
41660 KB |
Output is correct |
74 |
Correct |
231 ms |
41596 KB |
Output is correct |
75 |
Correct |
239 ms |
41648 KB |
Output is correct |
76 |
Correct |
250 ms |
41596 KB |
Output is correct |
77 |
Correct |
231 ms |
41572 KB |
Output is correct |
78 |
Correct |
253 ms |
41548 KB |
Output is correct |
79 |
Correct |
214 ms |
41576 KB |
Output is correct |
80 |
Correct |
219 ms |
41516 KB |
Output is correct |
81 |
Correct |
223 ms |
41576 KB |
Output is correct |
82 |
Correct |
235 ms |
41548 KB |
Output is correct |
83 |
Correct |
298 ms |
41464 KB |
Output is correct |
84 |
Correct |
272 ms |
41524 KB |
Output is correct |
85 |
Correct |
288 ms |
41520 KB |
Output is correct |
86 |
Correct |
319 ms |
41512 KB |
Output is correct |
87 |
Correct |
305 ms |
41548 KB |
Output is correct |
88 |
Correct |
321 ms |
41516 KB |
Output is correct |
89 |
Correct |
290 ms |
41560 KB |
Output is correct |
90 |
Correct |
333 ms |
41512 KB |
Output is correct |
91 |
Correct |
294 ms |
41608 KB |
Output is correct |
92 |
Correct |
340 ms |
41544 KB |
Output is correct |