#include <bits/stdc++.h>
using namespace std;
pair<int,int>inicio;
pair<int,int>final;
bool limites(int x, int y, int n){
if(x>=0&&x<n&&y>=0&&y<n){
return true;
}
else{
return false;
}
}
bool bfs(vector<vector<char>>&grid,vector<vector<int>>&bee, int &s, int n, int time){
vector<int>dx={1,0,-1,0};
vector<int>dy={0,1,0,-1};
vector<vector<int>>dist(n,vector<int>(n,-1));
dist[inicio.first][inicio.second]=0;
if((dist[inicio.first][inicio.second]+time)>bee[inicio.first][inicio.second]){
return false;
}
queue<pair<int,int>>cola;
cola.push({inicio});
while(!cola.empty()){
int x=cola.front().first;
int y=cola.front().second;
cola.pop();
for(int i=0;i<4;i++){
int xx=x+dx[i];
int yy=y+dy[i];
if(limites(xx,yy,n)&&dist[xx][yy]==-1&&((ceil((dist[x][y]+1)/s)+time)<bee[xx][yy])&&grid[xx][yy]!='T'){
dist[xx][yy]=dist[x][y]+1;
cola.push({xx,yy});
}
}
}
if(dist[final.first][final.second]==-1){
return false;
}
else{
return true;
}
}
int main() {
int n,s;
cin>>n>>s;
vector<vector<char>>grid(n,vector<char>(n));
vector<pair<int,int>>abejas;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>grid[i][j];
if(grid[i][j]=='H'){
abejas.push_back({i,j});
}
else if(grid[i][j]=='M'){
inicio={i,j};
}
else if(grid[i][j]=='D'){
final={i,j};
}
}
}
vector<vector<int>>bee(n,vector<int>(n,INT_MAX));
queue<pair<int,int>>cola;
for(int i=0;i<abejas.size();i++){
cola.push(abejas[i]);
bee[abejas[i].first][abejas[i].second]=0;
}
vector<int>dx={1,0,-1,0};
vector<int>dy={0,1,0,-1};
while(!cola.empty()){
int x=cola.front().first;
int y=cola.front().second;
cola.pop();
for(int i=0;i<4;i++){
int xx=x+dx[i];
int yy=y+dy[i];
if(limites(xx,yy,n)&&bee[xx][yy]==INT_MAX&&grid[xx][yy]!='T'&&grid[xx][yy]!='D'){
bee[xx][yy]=bee[x][y]+1;
cola.push({xx,yy});
}
}
}
if(!bfs(grid,bee,s,n,0)){
cout<<"-1\n";
return 0;
}
int l=0, r=1000000;
while(l<r){
int mid=(l+r+1)/2;
if(bfs(grid,bee,s,n,mid)){
l=mid;
}
else{
r=mid-1;
}
}
cout<<l<<"\n";
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:64:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for(int i=0;i<abejas.size();i++){
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
82 ms |
6264 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
440 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
688 KB |
Output is correct |
23 |
Correct |
0 ms |
436 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
440 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
9 ms |
1620 KB |
Output is correct |
34 |
Correct |
10 ms |
1640 KB |
Output is correct |
35 |
Correct |
21 ms |
1692 KB |
Output is correct |
36 |
Correct |
11 ms |
1884 KB |
Output is correct |
37 |
Correct |
12 ms |
1884 KB |
Output is correct |
38 |
Correct |
28 ms |
1884 KB |
Output is correct |
39 |
Correct |
15 ms |
2444 KB |
Output is correct |
40 |
Correct |
15 ms |
2396 KB |
Output is correct |
41 |
Correct |
36 ms |
2396 KB |
Output is correct |
42 |
Correct |
19 ms |
2852 KB |
Output is correct |
43 |
Correct |
28 ms |
2844 KB |
Output is correct |
44 |
Correct |
46 ms |
2872 KB |
Output is correct |
45 |
Correct |
21 ms |
3336 KB |
Output is correct |
46 |
Correct |
21 ms |
3348 KB |
Output is correct |
47 |
Correct |
57 ms |
3412 KB |
Output is correct |
48 |
Correct |
26 ms |
3872 KB |
Output is correct |
49 |
Correct |
27 ms |
3868 KB |
Output is correct |
50 |
Correct |
67 ms |
4008 KB |
Output is correct |
51 |
Correct |
30 ms |
4564 KB |
Output is correct |
52 |
Correct |
31 ms |
4436 KB |
Output is correct |
53 |
Correct |
83 ms |
4568 KB |
Output is correct |
54 |
Correct |
36 ms |
5124 KB |
Output is correct |
55 |
Correct |
38 ms |
5172 KB |
Output is correct |
56 |
Correct |
96 ms |
5272 KB |
Output is correct |
57 |
Correct |
39 ms |
5792 KB |
Output is correct |
58 |
Correct |
40 ms |
5884 KB |
Output is correct |
59 |
Correct |
106 ms |
5836 KB |
Output is correct |
60 |
Correct |
44 ms |
6632 KB |
Output is correct |
61 |
Correct |
45 ms |
6608 KB |
Output is correct |
62 |
Correct |
124 ms |
6692 KB |
Output is correct |
63 |
Correct |
110 ms |
6564 KB |
Output is correct |
64 |
Correct |
169 ms |
6548 KB |
Output is correct |
65 |
Correct |
167 ms |
6608 KB |
Output is correct |
66 |
Correct |
148 ms |
6564 KB |
Output is correct |
67 |
Correct |
40 ms |
6480 KB |
Output is correct |
68 |
Correct |
63 ms |
6656 KB |
Output is correct |
69 |
Correct |
66 ms |
6616 KB |
Output is correct |
70 |
Correct |
61 ms |
6648 KB |
Output is correct |
71 |
Correct |
59 ms |
6708 KB |
Output is correct |
72 |
Incorrect |
56 ms |
6624 KB |
Output isn't correct |
73 |
Incorrect |
60 ms |
6676 KB |
Output isn't correct |
74 |
Correct |
74 ms |
6528 KB |
Output is correct |
75 |
Correct |
76 ms |
6832 KB |
Output is correct |
76 |
Correct |
67 ms |
6644 KB |
Output is correct |
77 |
Correct |
72 ms |
6680 KB |
Output is correct |
78 |
Correct |
34 ms |
6484 KB |
Output is correct |
79 |
Correct |
74 ms |
6660 KB |
Output is correct |
80 |
Correct |
68 ms |
6596 KB |
Output is correct |
81 |
Correct |
76 ms |
6600 KB |
Output is correct |
82 |
Correct |
72 ms |
6628 KB |
Output is correct |
83 |
Correct |
78 ms |
6628 KB |
Output is correct |
84 |
Correct |
76 ms |
6460 KB |
Output is correct |
85 |
Correct |
73 ms |
6612 KB |
Output is correct |
86 |
Correct |
83 ms |
6664 KB |
Output is correct |
87 |
Correct |
76 ms |
6688 KB |
Output is correct |
88 |
Correct |
78 ms |
6644 KB |
Output is correct |
89 |
Correct |
86 ms |
6624 KB |
Output is correct |
90 |
Correct |
80 ms |
6648 KB |
Output is correct |
91 |
Correct |
87 ms |
6616 KB |
Output is correct |
92 |
Correct |
83 ms |
6596 KB |
Output is correct |