#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();
//cout<<u<<' ';
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();
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,v);
/*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=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: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]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
281 ms |
43540 KB |
Output is correct |
8 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 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 |
0 ms |
212 KB |
Output isn't correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Incorrect |
0 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 |
0 ms |
340 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 |
340 KB |
Output isn't correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Incorrect |
1 ms |
468 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 |
18 ms |
6676 KB |
Output isn't correct |
34 |
Correct |
18 ms |
6680 KB |
Output is correct |
35 |
Correct |
46 ms |
7604 KB |
Output is correct |
36 |
Incorrect |
24 ms |
8632 KB |
Output isn't correct |
37 |
Correct |
25 ms |
8536 KB |
Output is correct |
38 |
Correct |
78 ms |
9852 KB |
Output is correct |
39 |
Incorrect |
29 ms |
10784 KB |
Output isn't correct |
40 |
Correct |
29 ms |
10828 KB |
Output is correct |
41 |
Correct |
92 ms |
12332 KB |
Output is correct |
42 |
Incorrect |
36 ms |
13264 KB |
Output isn't correct |
43 |
Correct |
36 ms |
13236 KB |
Output is correct |
44 |
Correct |
114 ms |
15216 KB |
Output is correct |
45 |
Incorrect |
44 ms |
15960 KB |
Output isn't correct |
46 |
Correct |
44 ms |
15964 KB |
Output is correct |
47 |
Correct |
146 ms |
18448 KB |
Output is correct |
48 |
Incorrect |
51 ms |
18960 KB |
Output isn't correct |
49 |
Correct |
52 ms |
18912 KB |
Output is correct |
50 |
Correct |
160 ms |
21712 KB |
Output is correct |
51 |
Incorrect |
61 ms |
22164 KB |
Output isn't correct |
52 |
Correct |
61 ms |
22164 KB |
Output is correct |
53 |
Correct |
190 ms |
25500 KB |
Output is correct |
54 |
Incorrect |
71 ms |
25752 KB |
Output isn't correct |
55 |
Correct |
72 ms |
25740 KB |
Output is correct |
56 |
Correct |
252 ms |
29540 KB |
Output is correct |
57 |
Incorrect |
79 ms |
29488 KB |
Output isn't correct |
58 |
Correct |
80 ms |
29600 KB |
Output is correct |
59 |
Correct |
299 ms |
33868 KB |
Output is correct |
60 |
Incorrect |
91 ms |
33460 KB |
Output isn't correct |
61 |
Correct |
94 ms |
33480 KB |
Output is correct |
62 |
Correct |
359 ms |
38592 KB |
Output is correct |
63 |
Correct |
244 ms |
35448 KB |
Output is correct |
64 |
Correct |
332 ms |
35552 KB |
Output is correct |
65 |
Correct |
328 ms |
35448 KB |
Output is correct |
66 |
Incorrect |
250 ms |
35452 KB |
Output isn't correct |
67 |
Correct |
247 ms |
35356 KB |
Output is correct |
68 |
Correct |
171 ms |
35392 KB |
Output is correct |
69 |
Correct |
164 ms |
35404 KB |
Output is correct |
70 |
Correct |
147 ms |
35488 KB |
Output is correct |
71 |
Correct |
150 ms |
35532 KB |
Output is correct |
72 |
Correct |
145 ms |
35560 KB |
Output is correct |
73 |
Correct |
224 ms |
43408 KB |
Output is correct |
74 |
Correct |
247 ms |
43556 KB |
Output is correct |
75 |
Correct |
268 ms |
43476 KB |
Output is correct |
76 |
Correct |
252 ms |
43468 KB |
Output is correct |
77 |
Correct |
256 ms |
43436 KB |
Output is correct |
78 |
Correct |
251 ms |
43444 KB |
Output is correct |
79 |
Correct |
221 ms |
43544 KB |
Output is correct |
80 |
Correct |
228 ms |
43464 KB |
Output is correct |
81 |
Correct |
248 ms |
43536 KB |
Output is correct |
82 |
Correct |
230 ms |
43400 KB |
Output is correct |
83 |
Correct |
303 ms |
43292 KB |
Output is correct |
84 |
Correct |
256 ms |
43328 KB |
Output is correct |
85 |
Correct |
269 ms |
43452 KB |
Output is correct |
86 |
Correct |
277 ms |
43388 KB |
Output is correct |
87 |
Correct |
263 ms |
43400 KB |
Output is correct |
88 |
Correct |
304 ms |
43492 KB |
Output is correct |
89 |
Correct |
327 ms |
43532 KB |
Output is correct |
90 |
Correct |
316 ms |
43340 KB |
Output is correct |
91 |
Correct |
300 ms |
43340 KB |
Output is correct |
92 |
Correct |
287 ms |
43340 KB |
Output is correct |