#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long int
const ll N = 800+5 , INF = 1e18 , MOD = 1e9+7;
ll sx,sy,ex,ey;
vector<ll> dx = {0,0,-1,1};
vector<ll> dy = {-1,1,0,0};
vector<vector<ll>> bee(N,vector<ll>(N,INF));
vector<vector<char>> grid(N,vector<char>(N));
ll n,s;
bool check(ll start){
vector<vector<ll>> dist(n+5,vector<ll>(n+5,INF));
dist[sx][sy] = start;
queue<pair<ll,ll>> q;
q.push(make_pair(sx,sy));
while(!q.empty()){
ll a = q.front().first;
ll b = q.front().second;
q.pop();
for(ll t = 0; t <= 3; t++){
ll x = a+dx[t];
ll y = b+dy[t];
if(x < 1 || x > n || y < 1 || y > n || grid[x][y] == 'T') continue;
if(dist[x][y] != INF) continue;
ll d = dist[a][b]+1;
if(bee[x][y] < ((d-start)%s == 0)+start+ceil(double(d-start)/(double)s)) continue;
dist[x][y] = d;
q.push(make_pair(x,y));
}
}
return (dist[ex][ey] != INF);
}
void solve(){
cin >> n >> s;
queue<pair<ll,ll>> q;
for(ll i = 1; i <= n; i++){
for(ll j = 1; j <= n; j++){
cin >> grid[i][j];
if(grid[i][j] == 'M'){
sx = i;
sy = j;
}
else if(grid[i][j] == 'H'){
q.push(make_pair(i,j));
bee[i][j] = 0;
}
else if(grid[i][j] == 'D'){
ex = i;
ey = j;
}
}
}
while(!q.empty()){
ll a = q.front().first;
ll b = q.front().second;
q.pop();
for(ll t = 0; t <= 3; t++){
ll x = a+dx[t];
ll y = b+dy[t];
if(x < 1 || x > n || y < 1 || y > n || grid[x][y] == 'D' || grid[x][y] == 'T') continue;
if(bee[x][y] != INF) continue;
bee[x][y] = bee[a][b]+1;
q.push(make_pair(x,y));
}
}
ll low = 0 , high = bee[sx][sy]-1;
ll ans = INF;
while(low <= high){
ll mid = (low+high)/2;
if(check(mid)){
ans = mid;
low = mid+1;
}
else high = mid-1;
}
if(ans == INF) ans = -1;
cout << ans;
}
int main(){
fast;
ll tc = 1;
// cin >> tc;
while(tc--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
4 ms |
5972 KB |
Output is correct |
4 |
Correct |
4 ms |
5972 KB |
Output is correct |
5 |
Correct |
5 ms |
5972 KB |
Output is correct |
6 |
Correct |
3 ms |
5972 KB |
Output is correct |
7 |
Correct |
137 ms |
11212 KB |
Output is correct |
8 |
Correct |
4 ms |
5972 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
4 ms |
5972 KB |
Output is correct |
11 |
Correct |
4 ms |
5972 KB |
Output is correct |
12 |
Correct |
3 ms |
6100 KB |
Output is correct |
13 |
Correct |
3 ms |
6100 KB |
Output is correct |
14 |
Correct |
4 ms |
6100 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
3 ms |
5972 KB |
Output is correct |
17 |
Correct |
3 ms |
5972 KB |
Output is correct |
18 |
Correct |
3 ms |
5972 KB |
Output is correct |
19 |
Correct |
3 ms |
5972 KB |
Output is correct |
20 |
Correct |
4 ms |
5972 KB |
Output is correct |
21 |
Correct |
4 ms |
6100 KB |
Output is correct |
22 |
Correct |
3 ms |
6100 KB |
Output is correct |
23 |
Correct |
4 ms |
6100 KB |
Output is correct |
24 |
Correct |
4 ms |
6100 KB |
Output is correct |
25 |
Correct |
3 ms |
6100 KB |
Output is correct |
26 |
Correct |
4 ms |
6100 KB |
Output is correct |
27 |
Correct |
4 ms |
6100 KB |
Output is correct |
28 |
Correct |
4 ms |
6100 KB |
Output is correct |
29 |
Correct |
5 ms |
6100 KB |
Output is correct |
30 |
Correct |
4 ms |
6100 KB |
Output is correct |
31 |
Correct |
4 ms |
6100 KB |
Output is correct |
32 |
Correct |
3 ms |
6100 KB |
Output is correct |
33 |
Correct |
12 ms |
7080 KB |
Output is correct |
34 |
Correct |
12 ms |
7132 KB |
Output is correct |
35 |
Correct |
29 ms |
7064 KB |
Output is correct |
36 |
Correct |
14 ms |
7428 KB |
Output is correct |
37 |
Correct |
15 ms |
7384 KB |
Output is correct |
38 |
Correct |
39 ms |
7408 KB |
Output is correct |
39 |
Correct |
19 ms |
7760 KB |
Output is correct |
40 |
Correct |
19 ms |
7708 KB |
Output is correct |
41 |
Correct |
74 ms |
7732 KB |
Output is correct |
42 |
Correct |
26 ms |
8136 KB |
Output is correct |
43 |
Correct |
23 ms |
8160 KB |
Output is correct |
44 |
Correct |
59 ms |
8088 KB |
Output is correct |
45 |
Correct |
27 ms |
8544 KB |
Output is correct |
46 |
Correct |
30 ms |
8580 KB |
Output is correct |
47 |
Correct |
82 ms |
8424 KB |
Output is correct |
48 |
Correct |
31 ms |
9008 KB |
Output is correct |
49 |
Correct |
31 ms |
9008 KB |
Output is correct |
50 |
Correct |
84 ms |
8976 KB |
Output is correct |
51 |
Correct |
42 ms |
9508 KB |
Output is correct |
52 |
Correct |
52 ms |
9548 KB |
Output is correct |
53 |
Correct |
111 ms |
9492 KB |
Output is correct |
54 |
Correct |
41 ms |
10100 KB |
Output is correct |
55 |
Correct |
44 ms |
10104 KB |
Output is correct |
56 |
Correct |
131 ms |
10000 KB |
Output is correct |
57 |
Correct |
47 ms |
10604 KB |
Output is correct |
58 |
Correct |
53 ms |
10636 KB |
Output is correct |
59 |
Correct |
156 ms |
10716 KB |
Output is correct |
60 |
Correct |
58 ms |
11244 KB |
Output is correct |
61 |
Correct |
59 ms |
11312 KB |
Output is correct |
62 |
Correct |
188 ms |
11296 KB |
Output is correct |
63 |
Correct |
150 ms |
11188 KB |
Output is correct |
64 |
Correct |
271 ms |
11232 KB |
Output is correct |
65 |
Correct |
220 ms |
11200 KB |
Output is correct |
66 |
Correct |
176 ms |
11188 KB |
Output is correct |
67 |
Correct |
162 ms |
11236 KB |
Output is correct |
68 |
Correct |
78 ms |
11228 KB |
Output is correct |
69 |
Correct |
63 ms |
11224 KB |
Output is correct |
70 |
Correct |
60 ms |
11244 KB |
Output is correct |
71 |
Correct |
55 ms |
11204 KB |
Output is correct |
72 |
Correct |
55 ms |
11240 KB |
Output is correct |
73 |
Correct |
50 ms |
11212 KB |
Output is correct |
74 |
Correct |
110 ms |
11216 KB |
Output is correct |
75 |
Correct |
94 ms |
11248 KB |
Output is correct |
76 |
Correct |
89 ms |
11264 KB |
Output is correct |
77 |
Correct |
97 ms |
11228 KB |
Output is correct |
78 |
Correct |
99 ms |
11232 KB |
Output is correct |
79 |
Correct |
92 ms |
11212 KB |
Output is correct |
80 |
Correct |
100 ms |
11216 KB |
Output is correct |
81 |
Correct |
92 ms |
11220 KB |
Output is correct |
82 |
Correct |
94 ms |
11228 KB |
Output is correct |
83 |
Correct |
121 ms |
11228 KB |
Output is correct |
84 |
Correct |
113 ms |
11232 KB |
Output is correct |
85 |
Correct |
124 ms |
11272 KB |
Output is correct |
86 |
Correct |
122 ms |
11220 KB |
Output is correct |
87 |
Correct |
118 ms |
11212 KB |
Output is correct |
88 |
Correct |
127 ms |
11204 KB |
Output is correct |
89 |
Correct |
123 ms |
11220 KB |
Output is correct |
90 |
Correct |
146 ms |
11228 KB |
Output is correct |
91 |
Correct |
143 ms |
11208 KB |
Output is correct |
92 |
Correct |
133 ms |
11232 KB |
Output is correct |