#include <bits/stdc++.h>
#define int long long
#define double (long double)
using namespace std;
struct node{
int x; int y;
};
const int max_n = 1000, INF = 1e8;
int n,s;
string a[max_n];
bool ch[max_n][max_n];
int dis[max_n][max_n][2];
queue<node> src;
node st,dt;
bool isin(node u){
return (u.x >= 0 && u.y >= 0 && u.x < n && u.y < n);
}
bool safe(node u, int h){
return (dis[u.x][u.y][0] - h > dis[u.x][u.y][1]/s);
}
void f(node v, node u){
if(isin(v) && !ch[v.x][v.y] && a[v.x][v.y] != 'T' && a[v.x][v.y] != 'D'){
dis[v.x][v.y][0] = dis[u.x][u.y][0] + 1;
ch[v.x][v.y] = 1;
src.push(v);
}
}
void g(node v, node u, int h){
if(isin(v) && ch[v.x][v.y] == 0 && a[v.x][v.y] != 'T'){
dis[v.x][v.y][1] = dis[u.x][u.y][1] + 1;
if(safe(v, h)){
ch[v.x][v.y] = 1;
src.push(v);
}
}
}
void relax1(node v){
node u,d,l,r;
u = d = l = r = v;
u.y++; d.y--; l.x--; r.x++;
f(u, v); f(d, v); f(l, v); f(r, v);
}
void relax2(node v, int h){
node u,d,l,r;
u = d = l = r = v;
u.y++; d.y--; l.x--; r.x++;
g(u, v, h); g(d, v, h); g(l, v, h); g(r, v, h);
}
bool check(int h){
for(int i = 0; i <= n; i++){
for(int j = 0; j <= n; j++){
ch[i][j] = 0;
dis[i][j][1] = INF;
}
}
ch[st.x][st.y] = 1;
dis[st.x][st.y][1] = 0;
src.push(st);
while(!src.empty()){
node u = src.front();
src.pop();
relax2(u, h);
}
return ch[dt.x][dt.y];
}
void bfs(){
for(int i = 0; i <= n; i++){
for(int j = 0; j <= n; j++){
ch[i][j] = 0;
}
}
queue<node>temp = src;
while(!temp.empty()){
ch[temp.front().x][temp.front().y] = 1;
temp.pop();
}
while(!src.empty()){
node u = src.front();
src.pop();
relax1(u);
}
}
int32_t main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
node temp;
cin>>n>>s;
for(int i = 0; i < n; i++){
cin>>a[i];
for(int j = 0; j < n; j++){
if(a[i][j] == 'M'){
st.x = i; st.y = j;
}
if(a[i][j] == 'D'){
dt.x = i; dt.y = j;
}
if(a[i][j] == 'H'){
temp.x = i; temp.y = j;
src.push(temp);
}
}
}
bfs();
src.push(st);
dis[dt.x][dt.y][0] = INF * INF;
int ans = -1;
for(int k = INF; k > 0; k /= 2){
while(ans + k < INF && check(ans + k)) ans += k;
}
cout<<ans<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
158 ms |
15196 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
480 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
744 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
748 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
6 ms |
4188 KB |
Output is correct |
34 |
Correct |
7 ms |
4208 KB |
Output is correct |
35 |
Correct |
18 ms |
4304 KB |
Output is correct |
36 |
Correct |
7 ms |
5076 KB |
Output is correct |
37 |
Correct |
8 ms |
5204 KB |
Output is correct |
38 |
Correct |
24 ms |
5076 KB |
Output is correct |
39 |
Correct |
10 ms |
6080 KB |
Output is correct |
40 |
Correct |
8 ms |
6100 KB |
Output is correct |
41 |
Correct |
43 ms |
6184 KB |
Output is correct |
42 |
Correct |
12 ms |
7328 KB |
Output is correct |
43 |
Correct |
11 ms |
7284 KB |
Output is correct |
44 |
Correct |
41 ms |
7252 KB |
Output is correct |
45 |
Correct |
15 ms |
8492 KB |
Output is correct |
46 |
Correct |
16 ms |
8436 KB |
Output is correct |
47 |
Correct |
67 ms |
8492 KB |
Output is correct |
48 |
Correct |
32 ms |
9720 KB |
Output is correct |
49 |
Correct |
24 ms |
9732 KB |
Output is correct |
50 |
Correct |
80 ms |
9736 KB |
Output is correct |
51 |
Correct |
29 ms |
10964 KB |
Output is correct |
52 |
Correct |
27 ms |
11012 KB |
Output is correct |
53 |
Correct |
96 ms |
11004 KB |
Output is correct |
54 |
Correct |
42 ms |
12564 KB |
Output is correct |
55 |
Correct |
42 ms |
12500 KB |
Output is correct |
56 |
Correct |
128 ms |
12476 KB |
Output is correct |
57 |
Correct |
54 ms |
13900 KB |
Output is correct |
58 |
Correct |
53 ms |
13980 KB |
Output is correct |
59 |
Correct |
154 ms |
14004 KB |
Output is correct |
60 |
Correct |
64 ms |
14932 KB |
Output is correct |
61 |
Correct |
62 ms |
14924 KB |
Output is correct |
62 |
Correct |
187 ms |
14928 KB |
Output is correct |
63 |
Correct |
126 ms |
14976 KB |
Output is correct |
64 |
Correct |
178 ms |
15052 KB |
Output is correct |
65 |
Correct |
181 ms |
14888 KB |
Output is correct |
66 |
Correct |
167 ms |
14976 KB |
Output is correct |
67 |
Correct |
128 ms |
14988 KB |
Output is correct |
68 |
Correct |
77 ms |
14924 KB |
Output is correct |
69 |
Correct |
84 ms |
14984 KB |
Output is correct |
70 |
Correct |
76 ms |
15036 KB |
Output is correct |
71 |
Correct |
72 ms |
15048 KB |
Output is correct |
72 |
Incorrect |
71 ms |
15004 KB |
Output isn't correct |
73 |
Incorrect |
82 ms |
15480 KB |
Output isn't correct |
74 |
Correct |
96 ms |
15432 KB |
Output is correct |
75 |
Correct |
107 ms |
15504 KB |
Output is correct |
76 |
Correct |
101 ms |
15432 KB |
Output is correct |
77 |
Correct |
113 ms |
15500 KB |
Output is correct |
78 |
Correct |
90 ms |
15564 KB |
Output is correct |
79 |
Correct |
105 ms |
15432 KB |
Output is correct |
80 |
Correct |
87 ms |
15480 KB |
Output is correct |
81 |
Correct |
95 ms |
15468 KB |
Output is correct |
82 |
Correct |
93 ms |
15480 KB |
Output is correct |
83 |
Correct |
137 ms |
15404 KB |
Output is correct |
84 |
Correct |
147 ms |
15392 KB |
Output is correct |
85 |
Correct |
123 ms |
15272 KB |
Output is correct |
86 |
Correct |
132 ms |
15308 KB |
Output is correct |
87 |
Correct |
158 ms |
15388 KB |
Output is correct |
88 |
Correct |
152 ms |
15300 KB |
Output is correct |
89 |
Correct |
160 ms |
15300 KB |
Output is correct |
90 |
Correct |
167 ms |
15296 KB |
Output is correct |
91 |
Correct |
178 ms |
15436 KB |
Output is correct |
92 |
Correct |
156 ms |
15180 KB |
Output is correct |