#include <bits/stdc++.h>
using namespace std;
int n,s;
string matrix[805];
int mechox, mechoy,homex,homey;
vector<pair<int,int>> hives;
bool validsquare(int x, int y){
return x>=0 && x<n && y>=0 && y<n && (matrix[x][y] == 'G' || matrix[x][y] == 'M');
}
bool mecho_reached(int mecho_dis, int bees_dis){
return mecho_dis / s < bees_dis;
}
int main()
{
cin >> n >> s;
for(int i = 0;i<n;i++){
cin >> matrix[i];
}
for(int i = 0;i<n;i++){
for(int j = 0;j<n;j++){
if(matrix[i][j] == 'M'){
mechox = i;
mechoy = j;
}
if(matrix[i][j] == 'D'){
homex = i;
homey = j;
}
if(matrix[i][j] == 'H'){
hives.push_back(make_pair(i,j));
}
}
}
queue<pair<int,int>> q;
vector<vector<bool>> beesvis(n,vector<bool>(n));
vector<vector<int>> beestime(n,vector<int>(n));
for(auto hive: hives){
q.push({hive.first,hive.second});
beesvis[hive.first][hive.second] = true;
}
int dx[] = {0,-1,0,1};
int dy[] = {-1,0,1,0};
while(!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
for(int i = 0;i<4;i++){
int newx = x+dx[i],newy=y+dy[i];
if(validsquare(newx,newy) && !beesvis[newx][newy]){
beestime[newx][newy] = beestime[x][y]+1;
q.push({newx,newy});
beesvis[newx][newy] = true;
}
}
}
int l = 0, r = n*n;
while(l<=r){
vector<vector<bool>> mechovis(n,vector<bool>(n));
vector<vector<int>> mechotime(n,vector<int>(n));
queue<pair<int,int>> q2;
int mid = (l+r)/2;
q2.push({mechox,mechoy});
mechovis[mechox][mechoy] = true;
if(beestime[mechox][mechoy] <= mid){
q2.pop();
}
while(!q2.empty()){
int x = q2.front().first;
int y = q2.front().second;
q2.pop();
for(int i = 0;i<4;i++){
int newx = x+dx[i],newy=y+dy[i];
if(validsquare(newx,newy) && !mechovis[newx][newy] && mecho_reached(mechotime[x][y] + 1, beestime[newx][newy] - mid)){
mechotime[newx][newy] = mechotime[x][y]+1;
q2.push({newx,newy});
mechovis[newx][newy] = true;
}
}
}
bool good = false;
for(int i = 0;i<4;i++){
int nx = homex+dx[i],ny = homey+dy[i];
if(validsquare(nx,ny) && mechovis[nx][ny] && mecho_reached(mechotime[nx][ny],beestime[nx][ny]-mid)){
good = true;
break;
}
}
if(good){
l = mid+1;
}
else{
r = mid-1;
}
}
cout << l-1 << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
292 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 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 |
114 ms |
7100 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
328 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 |
328 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
332 KB |
Output is correct |
33 |
Correct |
9 ms |
1672 KB |
Output is correct |
34 |
Correct |
12 ms |
1684 KB |
Output is correct |
35 |
Correct |
32 ms |
1668 KB |
Output is correct |
36 |
Correct |
12 ms |
2052 KB |
Output is correct |
37 |
Correct |
12 ms |
2036 KB |
Output is correct |
38 |
Correct |
45 ms |
2032 KB |
Output is correct |
39 |
Correct |
13 ms |
2456 KB |
Output is correct |
40 |
Correct |
15 ms |
2452 KB |
Output is correct |
41 |
Correct |
48 ms |
2456 KB |
Output is correct |
42 |
Correct |
19 ms |
3160 KB |
Output is correct |
43 |
Correct |
21 ms |
3156 KB |
Output is correct |
44 |
Correct |
62 ms |
3196 KB |
Output is correct |
45 |
Correct |
27 ms |
3712 KB |
Output is correct |
46 |
Correct |
23 ms |
3708 KB |
Output is correct |
47 |
Correct |
85 ms |
3668 KB |
Output is correct |
48 |
Correct |
25 ms |
4352 KB |
Output is correct |
49 |
Correct |
26 ms |
4284 KB |
Output is correct |
50 |
Correct |
79 ms |
4332 KB |
Output is correct |
51 |
Correct |
29 ms |
4908 KB |
Output is correct |
52 |
Correct |
30 ms |
4956 KB |
Output is correct |
53 |
Correct |
122 ms |
4960 KB |
Output is correct |
54 |
Correct |
42 ms |
5580 KB |
Output is correct |
55 |
Correct |
37 ms |
5572 KB |
Output is correct |
56 |
Correct |
127 ms |
5572 KB |
Output is correct |
57 |
Correct |
38 ms |
6308 KB |
Output is correct |
58 |
Correct |
42 ms |
6296 KB |
Output is correct |
59 |
Correct |
151 ms |
6364 KB |
Output is correct |
60 |
Correct |
57 ms |
7060 KB |
Output is correct |
61 |
Correct |
49 ms |
7056 KB |
Output is correct |
62 |
Correct |
154 ms |
7068 KB |
Output is correct |
63 |
Correct |
142 ms |
7056 KB |
Output is correct |
64 |
Correct |
251 ms |
7052 KB |
Output is correct |
65 |
Correct |
179 ms |
7080 KB |
Output is correct |
66 |
Correct |
156 ms |
7040 KB |
Output is correct |
67 |
Correct |
145 ms |
7056 KB |
Output is correct |
68 |
Correct |
70 ms |
7064 KB |
Output is correct |
69 |
Correct |
72 ms |
7080 KB |
Output is correct |
70 |
Correct |
62 ms |
7084 KB |
Output is correct |
71 |
Correct |
76 ms |
7080 KB |
Output is correct |
72 |
Correct |
52 ms |
7060 KB |
Output is correct |
73 |
Correct |
56 ms |
7112 KB |
Output is correct |
74 |
Correct |
81 ms |
7100 KB |
Output is correct |
75 |
Correct |
101 ms |
7084 KB |
Output is correct |
76 |
Correct |
98 ms |
7100 KB |
Output is correct |
77 |
Correct |
103 ms |
7088 KB |
Output is correct |
78 |
Correct |
100 ms |
7072 KB |
Output is correct |
79 |
Correct |
72 ms |
7080 KB |
Output is correct |
80 |
Correct |
96 ms |
7060 KB |
Output is correct |
81 |
Correct |
122 ms |
7120 KB |
Output is correct |
82 |
Correct |
86 ms |
7084 KB |
Output is correct |
83 |
Correct |
104 ms |
7100 KB |
Output is correct |
84 |
Correct |
111 ms |
7080 KB |
Output is correct |
85 |
Correct |
107 ms |
7088 KB |
Output is correct |
86 |
Correct |
105 ms |
7080 KB |
Output is correct |
87 |
Correct |
124 ms |
7168 KB |
Output is correct |
88 |
Correct |
111 ms |
7068 KB |
Output is correct |
89 |
Correct |
108 ms |
7068 KB |
Output is correct |
90 |
Correct |
113 ms |
7252 KB |
Output is correct |
91 |
Correct |
145 ms |
7096 KB |
Output is correct |
92 |
Correct |
126 ms |
7032 KB |
Output is correct |