#include <bits/stdc++.h>
using namespace std;
#define A first
#define B second
#define MP make_pair
#define ms(a, x) memset(a, x, sizeof(a))
#define boost() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long double, long double> pld;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
const int mxN=805;
char grid[mxN][mxN];
ll dist[2][mxN][mxN];
queue <pll> q;
vector <pll> moves={{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
int n, s;
bool valid(int i, int j){
if(i >= 1 && i <= n && j >= 1 && j <= n && grid[i][j] != 'H' && grid[i][j] != 'T')
return true;
return false;
}
void bfs(){
while(!q.empty()){
pii u = q.front();
q.pop();
for(auto [x, y]:moves){
if(valid(u.A+x, u.B+y) && grid[u.A+x][u.B+y] != 'D' && dist[0][u.A+x][u.B+y] == -1){
dist[0][u.A+x][u.B+y] = dist[0][u.A][u.B]+1;
q.push({u.A+x, u.B+y});
}
}
}
}
void bfs2(ll rest){
while(!q.empty()){
pii u = q.front();
q.pop();
for(auto [x,y]:moves){
if(valid(u.A+x, u.B+y) && dist[1][u.A+x][u.B+y] == -1 && (dist[1][u.A][u.B]+1)/s < dist[0][u.A+x][u.B+y]-rest){
dist[1][u.A+x][u.B+y] = dist[1][u.A][u.B]+1;
q.push({u.A+x, u.B+y});
}
}
}
}
int main(){
ms(dist, -1);
cin >> n >> s;
pii ip, ep;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
cin >> grid[i][j];
if(grid[i][j] == 'H'){
q.push({i, j});
dist[0][i][j]=0;
}
else if(grid[i][j] == 'M')
ip={i, j};
else if(grid[i][j] == 'D')
ep={i, j};
}
}
ll st=0, en=1e9, mid, ans=-1;
bfs();
dist[0][ep.A][ep.B] = INT_MAX;
while(st<=en){
mid = (st+en)/2;
q.push(ip);
ms(dist[1], -1);
dist[1][ip.A][ip.B]=0;
bfs2(mid);
if(dist[1][ep.A][ep.B] != -1 && mid < dist[0][ip.A][ip.B]){
ans = mid;
st = mid+1;
}
else
en = mid-1;
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
10452 KB |
Output is correct |
2 |
Correct |
8 ms |
10452 KB |
Output is correct |
3 |
Correct |
8 ms |
10452 KB |
Output is correct |
4 |
Correct |
8 ms |
10452 KB |
Output is correct |
5 |
Correct |
8 ms |
10452 KB |
Output is correct |
6 |
Correct |
10 ms |
10456 KB |
Output is correct |
7 |
Correct |
125 ms |
11288 KB |
Output is correct |
8 |
Correct |
9 ms |
10452 KB |
Output is correct |
9 |
Correct |
11 ms |
10460 KB |
Output is correct |
10 |
Correct |
8 ms |
10452 KB |
Output is correct |
11 |
Correct |
8 ms |
10452 KB |
Output is correct |
12 |
Correct |
11 ms |
10496 KB |
Output is correct |
13 |
Correct |
9 ms |
10496 KB |
Output is correct |
14 |
Correct |
9 ms |
10452 KB |
Output is correct |
15 |
Correct |
8 ms |
10452 KB |
Output is correct |
16 |
Correct |
8 ms |
10452 KB |
Output is correct |
17 |
Correct |
8 ms |
10452 KB |
Output is correct |
18 |
Correct |
9 ms |
10452 KB |
Output is correct |
19 |
Correct |
9 ms |
10468 KB |
Output is correct |
20 |
Correct |
9 ms |
10452 KB |
Output is correct |
21 |
Correct |
8 ms |
10452 KB |
Output is correct |
22 |
Correct |
8 ms |
10452 KB |
Output is correct |
23 |
Correct |
8 ms |
10452 KB |
Output is correct |
24 |
Correct |
9 ms |
10452 KB |
Output is correct |
25 |
Correct |
9 ms |
10452 KB |
Output is correct |
26 |
Correct |
9 ms |
10452 KB |
Output is correct |
27 |
Correct |
8 ms |
10452 KB |
Output is correct |
28 |
Correct |
8 ms |
10500 KB |
Output is correct |
29 |
Correct |
11 ms |
10504 KB |
Output is correct |
30 |
Correct |
9 ms |
10452 KB |
Output is correct |
31 |
Correct |
10 ms |
10504 KB |
Output is correct |
32 |
Correct |
9 ms |
10452 KB |
Output is correct |
33 |
Correct |
17 ms |
10732 KB |
Output is correct |
34 |
Correct |
19 ms |
10720 KB |
Output is correct |
35 |
Correct |
37 ms |
10728 KB |
Output is correct |
36 |
Correct |
19 ms |
10708 KB |
Output is correct |
37 |
Correct |
19 ms |
10712 KB |
Output is correct |
38 |
Correct |
42 ms |
10680 KB |
Output is correct |
39 |
Correct |
25 ms |
10708 KB |
Output is correct |
40 |
Correct |
24 ms |
10708 KB |
Output is correct |
41 |
Correct |
51 ms |
10808 KB |
Output is correct |
42 |
Correct |
24 ms |
10848 KB |
Output is correct |
43 |
Correct |
24 ms |
10736 KB |
Output is correct |
44 |
Correct |
65 ms |
10836 KB |
Output is correct |
45 |
Correct |
29 ms |
10880 KB |
Output is correct |
46 |
Correct |
28 ms |
10784 KB |
Output is correct |
47 |
Correct |
79 ms |
10876 KB |
Output is correct |
48 |
Correct |
34 ms |
10836 KB |
Output is correct |
49 |
Correct |
33 ms |
10900 KB |
Output is correct |
50 |
Correct |
88 ms |
10876 KB |
Output is correct |
51 |
Correct |
38 ms |
10932 KB |
Output is correct |
52 |
Correct |
39 ms |
10900 KB |
Output is correct |
53 |
Correct |
122 ms |
10936 KB |
Output is correct |
54 |
Correct |
43 ms |
10964 KB |
Output is correct |
55 |
Correct |
48 ms |
10980 KB |
Output is correct |
56 |
Correct |
128 ms |
10992 KB |
Output is correct |
57 |
Correct |
46 ms |
11084 KB |
Output is correct |
58 |
Correct |
44 ms |
11020 KB |
Output is correct |
59 |
Correct |
150 ms |
11032 KB |
Output is correct |
60 |
Correct |
50 ms |
11060 KB |
Output is correct |
61 |
Correct |
53 ms |
11060 KB |
Output is correct |
62 |
Correct |
172 ms |
11112 KB |
Output is correct |
63 |
Correct |
141 ms |
11068 KB |
Output is correct |
64 |
Correct |
203 ms |
10956 KB |
Output is correct |
65 |
Correct |
206 ms |
11068 KB |
Output is correct |
66 |
Correct |
159 ms |
11072 KB |
Output is correct |
67 |
Correct |
139 ms |
11068 KB |
Output is correct |
68 |
Correct |
78 ms |
11084 KB |
Output is correct |
69 |
Correct |
85 ms |
11140 KB |
Output is correct |
70 |
Correct |
65 ms |
11124 KB |
Output is correct |
71 |
Correct |
71 ms |
11092 KB |
Output is correct |
72 |
Correct |
63 ms |
11252 KB |
Output is correct |
73 |
Correct |
78 ms |
11596 KB |
Output is correct |
74 |
Correct |
92 ms |
11596 KB |
Output is correct |
75 |
Correct |
118 ms |
11612 KB |
Output is correct |
76 |
Correct |
94 ms |
11620 KB |
Output is correct |
77 |
Correct |
94 ms |
11616 KB |
Output is correct |
78 |
Correct |
119 ms |
11468 KB |
Output is correct |
79 |
Correct |
87 ms |
11564 KB |
Output is correct |
80 |
Correct |
94 ms |
11664 KB |
Output is correct |
81 |
Correct |
106 ms |
11540 KB |
Output is correct |
82 |
Correct |
92 ms |
11444 KB |
Output is correct |
83 |
Correct |
115 ms |
11384 KB |
Output is correct |
84 |
Correct |
113 ms |
11480 KB |
Output is correct |
85 |
Correct |
105 ms |
11480 KB |
Output is correct |
86 |
Correct |
124 ms |
11512 KB |
Output is correct |
87 |
Correct |
108 ms |
11468 KB |
Output is correct |
88 |
Correct |
121 ms |
11284 KB |
Output is correct |
89 |
Correct |
117 ms |
11388 KB |
Output is correct |
90 |
Correct |
131 ms |
11384 KB |
Output is correct |
91 |
Correct |
113 ms |
11340 KB |
Output is correct |
92 |
Correct |
126 ms |
11388 KB |
Output is correct |