#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n,one;
cin >> n >> one;
vector<string> s(n);
array<int,2> bear;
array<int,2> house;
vector<array<int,2>> bees;
for(int i=0;i<n;i++)
{
cin >> s[i];
for(int j=0;j<n;j++)
{
if(s[i][j]=='M') bear={i,j};
if(s[i][j]=='D') house={i,j};
if(s[i][j]=='H') bees.push_back({i,j});
}
}
auto in=[&](int r,int c)->bool{return (0<=r&&r<n&&0<=c&&c<n);};
vector<array<int,2>> z={{1,0},{-1,0},{0,-1},{0,1}};
int ltime=-1,rtime=n*n;
while(ltime<rtime-1)
{
int m=(ltime+rtime)/2;
queue<array<int,2>> qbees,qbear;
vector dbee(n,vector(n,int(-1)));
auto add_bee=[&](int r,int c,int nd)
{
if(in(r,c)&&s[r][c]!='T'&&s[r][c]!='D'&&dbee[r][c]==-1)
{
qbees.push({r,c});
dbee[r][c]=nd;
}
};
vector dbear(n,vector(n,int(-1)));
auto add_bear=[&](int r,int c,int nd)
{
if(in(r,c)&&s[r][c]!='T'&&dbee[r][c]==-1&&dbear[r][c]==-1)
{
qbear.push({r,c});
dbear[r][c]=nd;
}
};
for(auto [r,c]:bees) add_bee(r,c,0);
add_bear(bear[0],bear[1],0);
for(int i=0;!qbear.empty();i++)
{
while(!qbear.empty())
{
auto [r,c]=qbear.front();
int d=dbear[r][c];
if(d<(i-m+1)*one)
{
qbear.pop();
if(dbee[r][c]!=-1) continue;
for(auto [dr,dc]:z) add_bear(r+dr,c+dc,dbear[r][c]+1);
}
else break;
}
while(!qbees.empty())
{
auto [r,c]=qbees.front();
int d=dbee[r][c];
if(d<=i)
{
qbees.pop();
for(auto [dr,dc]:z) add_bee(r+dr,c+dc,dbee[r][c]+1);
}
else break;
}
}
if(dbear[house[0]][house[1]]!=-1) ltime=m;
else rtime=m;
}
cout << ltime << "\n";
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:79:36: warning: 'house.std::array<int, 2>::_M_elems[1]' may be used uninitialized in this function [-Wmaybe-uninitialized]
79 | if(dbear[house[0]][house[1]]!=-1) ltime=m;
| ^
mecho.cpp:13:18: warning: 'bear.std::array<int, 2>::_M_elems[0]' may be used uninitialized in this function [-Wmaybe-uninitialized]
13 | array<int,2> bear;
| ^~~~
mecho.cpp:79:26: warning: 'house.std::array<int, 2>::_M_elems[0]' may be used uninitialized in this function [-Wmaybe-uninitialized]
79 | if(dbear[house[0]][house[1]]!=-1) ltime=m;
| ^
mecho.cpp:13:18: warning: 'bear.std::array<int, 2>::_M_elems[1]' may be used uninitialized in this function [-Wmaybe-uninitialized]
13 | array<int,2> bear;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
319 ms |
6856 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 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 |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 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 |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
328 KB |
Output is correct |
33 |
Correct |
29 ms |
1548 KB |
Output is correct |
34 |
Correct |
32 ms |
1588 KB |
Output is correct |
35 |
Correct |
40 ms |
1564 KB |
Output is correct |
36 |
Correct |
41 ms |
1960 KB |
Output is correct |
37 |
Correct |
39 ms |
1988 KB |
Output is correct |
38 |
Correct |
49 ms |
1876 KB |
Output is correct |
39 |
Correct |
56 ms |
2384 KB |
Output is correct |
40 |
Correct |
49 ms |
2372 KB |
Output is correct |
41 |
Correct |
65 ms |
2352 KB |
Output is correct |
42 |
Correct |
67 ms |
2880 KB |
Output is correct |
43 |
Correct |
78 ms |
2892 KB |
Output is correct |
44 |
Correct |
81 ms |
2832 KB |
Output is correct |
45 |
Correct |
85 ms |
3364 KB |
Output is correct |
46 |
Correct |
74 ms |
3360 KB |
Output is correct |
47 |
Correct |
104 ms |
3356 KB |
Output is correct |
48 |
Correct |
100 ms |
3960 KB |
Output is correct |
49 |
Correct |
91 ms |
3944 KB |
Output is correct |
50 |
Correct |
111 ms |
3940 KB |
Output is correct |
51 |
Correct |
119 ms |
4560 KB |
Output is correct |
52 |
Correct |
116 ms |
4704 KB |
Output is correct |
53 |
Correct |
155 ms |
4544 KB |
Output is correct |
54 |
Correct |
139 ms |
5240 KB |
Output is correct |
55 |
Correct |
126 ms |
5232 KB |
Output is correct |
56 |
Correct |
188 ms |
5296 KB |
Output is correct |
57 |
Correct |
158 ms |
5948 KB |
Output is correct |
58 |
Correct |
144 ms |
5956 KB |
Output is correct |
59 |
Correct |
199 ms |
5952 KB |
Output is correct |
60 |
Correct |
196 ms |
6724 KB |
Output is correct |
61 |
Correct |
166 ms |
6728 KB |
Output is correct |
62 |
Correct |
227 ms |
6712 KB |
Output is correct |
63 |
Correct |
339 ms |
6836 KB |
Output is correct |
64 |
Correct |
335 ms |
6652 KB |
Output is correct |
65 |
Correct |
330 ms |
6880 KB |
Output is correct |
66 |
Correct |
359 ms |
6716 KB |
Output is correct |
67 |
Correct |
345 ms |
6900 KB |
Output is correct |
68 |
Correct |
327 ms |
6740 KB |
Output is correct |
69 |
Correct |
319 ms |
6860 KB |
Output is correct |
70 |
Correct |
323 ms |
6740 KB |
Output is correct |
71 |
Correct |
374 ms |
6752 KB |
Output is correct |
72 |
Correct |
369 ms |
6740 KB |
Output is correct |
73 |
Correct |
245 ms |
7000 KB |
Output is correct |
74 |
Correct |
343 ms |
7024 KB |
Output is correct |
75 |
Correct |
341 ms |
7004 KB |
Output is correct |
76 |
Correct |
339 ms |
7152 KB |
Output is correct |
77 |
Correct |
314 ms |
6996 KB |
Output is correct |
78 |
Correct |
337 ms |
7048 KB |
Output is correct |
79 |
Correct |
318 ms |
6984 KB |
Output is correct |
80 |
Correct |
348 ms |
7116 KB |
Output is correct |
81 |
Correct |
329 ms |
6920 KB |
Output is correct |
82 |
Correct |
338 ms |
6932 KB |
Output is correct |
83 |
Correct |
332 ms |
6936 KB |
Output is correct |
84 |
Correct |
331 ms |
6936 KB |
Output is correct |
85 |
Correct |
372 ms |
7072 KB |
Output is correct |
86 |
Correct |
340 ms |
6932 KB |
Output is correct |
87 |
Correct |
368 ms |
7184 KB |
Output is correct |
88 |
Correct |
333 ms |
6856 KB |
Output is correct |
89 |
Correct |
330 ms |
6880 KB |
Output is correct |
90 |
Correct |
321 ms |
6876 KB |
Output is correct |
91 |
Correct |
343 ms |
6900 KB |
Output is correct |
92 |
Correct |
356 ms |
6872 KB |
Output is correct |