#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<vector<bool>> maps;
vector<vector<int>> distBees;
int n,s;
pair<int, int> home, start;
vector<int> dx = {0,0,1,-1}, dy = {1,-1,0,0};
struct Compare {
bool operator()(tuple<int, int, int> a, tuple<int, int, int> b) {
return get<2>(a)>get<2>(b);
}
};
void print(vector<vector<int>> v) {
for(int i = 0; i < v.size(); i++) {
for(int j = 0; j < v.size(); j++) {
cout << v[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
bool possible(int mid) {
vector<vector<int>> temp = distBees;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) temp[i][j]-=mid;
}
//cout << mid << endl;
//print(temp);
vector<vector<int>> minDist(n, vector<int>(n, 1000000000));
minDist[start.first][start.second]=0;
deque<pair<int, int>> q;
q.push_back(start);
while(q.size()) {
pair<int, int> p = q.front();
q.pop_front();
for(int i = 0; i < 4; i++) {
int newx = p.first+dx[i], newy = p.second+dy[i];
if(newx>=0&&newy>=0&&newx<n&&newy<n&&maps[newx][newy]&&minDist[newx][newy]==1000000000) {
if((minDist[p.first][p.second]+1)/s<=temp[newx][newy]) {
minDist[newx][newy]=minDist[p.first][p.second]+1;
q.push_back({newx, newy});
}
}
}
}
return minDist[home.first][home.second]!=1000000000;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> s;
deque<pair<int, int>> bee;
maps=vector<vector<bool>>(n, vector<bool>(n));
distBees=vector<vector<int>>(n, vector<int>(n, 1000000000));
for(int i = 0; i < n; i++) {
string t;
cin >> t;
for(int j = 0; j < n; j++) {
if(t[j]=='G') {
maps[i][j]=true;
} else if(t[j]=='M') {
start={i,j};
maps[i][j]=true;
} else if(t[j]=='D') {
home = {i,j};
} else if(t[j]=='H') {
bee.push_back({i,j});
distBees[i][j]=-1;
}
}
}
while(bee.size()) {
pair<int, int> p = bee.front();
bee.pop_front();
for(int i = 0; i < 4; i++) {
int newx = p.first+dx[i], newy = p.second+dy[i];
if(newx>=0&&newy>=0&&newx<n&&newy<n&&maps[newx][newy]&&distBees[newx][newy]==1000000000) {
distBees[newx][newy]=distBees[p.first][p.second]+1;
bee.push_back({newx, newy});
}
}
}
maps[home.first][home.second]=true;
//print(distBees);
int lo = 0, hi = distBees[start.first][start.second];
if(!possible(0)) {
cout << -1 << endl;
return 0;
}
while(lo+1<hi) {
int mid = (lo+hi)/2;
if(possible(mid)) {
lo=mid;
} else {
hi=mid;
}
}
if(possible(hi)) {
cout << hi << endl;
} else {
cout << lo << endl;
}
}
Compilation message
mecho.cpp: In function 'void print(std::vector<std::vector<int> >)':
mecho.cpp:15:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for(int i = 0; i < v.size(); i++) {
| ~~^~~~~~~~~~
mecho.cpp:16:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | for(int j = 0; j < v.size(); j++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
96 ms |
8712 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
320 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
328 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
10 ms |
2004 KB |
Output is correct |
34 |
Correct |
8 ms |
1948 KB |
Output is correct |
35 |
Correct |
22 ms |
1956 KB |
Output is correct |
36 |
Correct |
10 ms |
2524 KB |
Output is correct |
37 |
Correct |
11 ms |
2380 KB |
Output is correct |
38 |
Correct |
30 ms |
2388 KB |
Output is correct |
39 |
Correct |
11 ms |
3076 KB |
Output is correct |
40 |
Correct |
13 ms |
2996 KB |
Output is correct |
41 |
Correct |
37 ms |
3004 KB |
Output is correct |
42 |
Correct |
14 ms |
3628 KB |
Output is correct |
43 |
Correct |
16 ms |
3692 KB |
Output is correct |
44 |
Correct |
45 ms |
3648 KB |
Output is correct |
45 |
Correct |
18 ms |
4284 KB |
Output is correct |
46 |
Correct |
20 ms |
4372 KB |
Output is correct |
47 |
Correct |
57 ms |
4280 KB |
Output is correct |
48 |
Correct |
22 ms |
5124 KB |
Output is correct |
49 |
Correct |
23 ms |
5112 KB |
Output is correct |
50 |
Correct |
67 ms |
5096 KB |
Output is correct |
51 |
Correct |
28 ms |
5908 KB |
Output is correct |
52 |
Correct |
27 ms |
5844 KB |
Output is correct |
53 |
Correct |
84 ms |
5868 KB |
Output is correct |
54 |
Correct |
30 ms |
6788 KB |
Output is correct |
55 |
Correct |
31 ms |
6820 KB |
Output is correct |
56 |
Correct |
100 ms |
6752 KB |
Output is correct |
57 |
Correct |
42 ms |
7792 KB |
Output is correct |
58 |
Correct |
37 ms |
7884 KB |
Output is correct |
59 |
Correct |
118 ms |
7708 KB |
Output is correct |
60 |
Correct |
39 ms |
8812 KB |
Output is correct |
61 |
Correct |
42 ms |
8732 KB |
Output is correct |
62 |
Correct |
142 ms |
8700 KB |
Output is correct |
63 |
Correct |
115 ms |
8716 KB |
Output is correct |
64 |
Correct |
192 ms |
8692 KB |
Output is correct |
65 |
Correct |
177 ms |
8688 KB |
Output is correct |
66 |
Correct |
136 ms |
8704 KB |
Output is correct |
67 |
Correct |
29 ms |
8660 KB |
Output is correct |
68 |
Correct |
61 ms |
8732 KB |
Output is correct |
69 |
Correct |
50 ms |
8792 KB |
Output is correct |
70 |
Correct |
50 ms |
8728 KB |
Output is correct |
71 |
Correct |
48 ms |
8740 KB |
Output is correct |
72 |
Correct |
48 ms |
8756 KB |
Output is correct |
73 |
Correct |
33 ms |
8732 KB |
Output is correct |
74 |
Correct |
87 ms |
8744 KB |
Output is correct |
75 |
Correct |
80 ms |
8756 KB |
Output is correct |
76 |
Correct |
74 ms |
8804 KB |
Output is correct |
77 |
Correct |
86 ms |
8772 KB |
Output is correct |
78 |
Correct |
25 ms |
8740 KB |
Output is correct |
79 |
Correct |
80 ms |
8732 KB |
Output is correct |
80 |
Correct |
75 ms |
8740 KB |
Output is correct |
81 |
Correct |
112 ms |
8736 KB |
Output is correct |
82 |
Correct |
81 ms |
8784 KB |
Output is correct |
83 |
Correct |
84 ms |
8728 KB |
Output is correct |
84 |
Correct |
88 ms |
8736 KB |
Output is correct |
85 |
Correct |
85 ms |
8736 KB |
Output is correct |
86 |
Correct |
93 ms |
8736 KB |
Output is correct |
87 |
Correct |
94 ms |
8716 KB |
Output is correct |
88 |
Correct |
90 ms |
8708 KB |
Output is correct |
89 |
Correct |
100 ms |
8732 KB |
Output is correct |
90 |
Correct |
100 ms |
8732 KB |
Output is correct |
91 |
Correct |
102 ms |
8796 KB |
Output is correct |
92 |
Correct |
102 ms |
8800 KB |
Output is correct |