#include <bits/stdc++.h>
using namespace std;
const int N = 801;
int n,s,sx,sy,tx,ty;
char c[N][N];
int bee[N][N];
bool visited[N][N];
queue<pair<pair<int,int>,int> > qb,q;
void flybee()
{
while(!qb.empty())
{
int t = qb.front().second,x = qb.front().first.first,y = qb.front().first.second;
qb.pop();
for(int i = -1;i <= 1;i++) for(int j = -1;j <= 1;j++)
{
if(i!=0 and j!=0) continue;
if(i==0 and j==0) continue;
int ai = x+i,aj = y+j;
if(ai<1 or ai>n or aj<1 or aj>n) continue;
if(c[ai][aj]=='D' or c[ai][aj]=='T') continue;
if(visited[ai][aj]) continue;
visited[ai][aj] = true;
bee[ai][aj] = t+1;
qb.push({{ai,aj},t+1});
}
}
}
bool solve(int m)
{
if(m>=bee[sx][sy]) return false;
for(int i = 1;i <= n;i++) for(int j = 1;j <= n;j++) visited[i][j] = false;
while(!q.empty()) q.pop();
q.push({{sx,sy},0});
visited[sx][sy] = true;
while(!q.empty())
{
int x = q.front().first.first,y = q.front().first.second,tm = q.front().second;
q.pop();
if(x==tx and y==ty) return true;
for(int i = -1;i <= 1;i++) for(int j = -1;j <= 1;j++)
{
if(i!=0 and j!=0) continue;
if(i==0 and j==0) continue;
int ai = x+i,aj = y+j;
if(ai<1 or ai>n or aj<1 or aj>n) continue;
if(c[ai][aj]=='T') continue;
if(visited[ai][aj]) continue;
if(1LL*tm+1>=1LL*(bee[ai][aj]-m)*s) continue;
q.push({{ai,aj},tm+1});
visited[ai][aj] = true;
}
}
return false;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> s;
for(int i = 1;i <= n;i++) for(int j = 1;j <= n;j++) cin >> c[i][j],bee[i][j] = INT_MAX;
for(int i = 1;i <= n;i++) for(int j = 1;j <= n;j++)
if(c[i][j]=='H') qb.push({{i,j},0}),bee[i][j] = 0,visited[i][j] = true;
else if(c[i][j]=='M') sx = i,sy = j;
else if(c[i][j]=='D') tx = i,ty = j;
flybee();
int l = 0,r = n*n;
while(l<r)
{
int m = (l+r+1)/2;
if(solve(m)) l = m;
else r = m-1;
}
if(solve(l)) cout << l;
else cout << "-1";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
134 ms |
4216 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
640 KB |
Output is correct |
13 |
Correct |
5 ms |
640 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
512 KB |
Output is correct |
18 |
Correct |
5 ms |
512 KB |
Output is correct |
19 |
Correct |
5 ms |
512 KB |
Output is correct |
20 |
Correct |
5 ms |
512 KB |
Output is correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
5 ms |
640 KB |
Output is correct |
24 |
Correct |
5 ms |
640 KB |
Output is correct |
25 |
Correct |
5 ms |
640 KB |
Output is correct |
26 |
Correct |
6 ms |
640 KB |
Output is correct |
27 |
Correct |
5 ms |
640 KB |
Output is correct |
28 |
Correct |
5 ms |
640 KB |
Output is correct |
29 |
Correct |
5 ms |
640 KB |
Output is correct |
30 |
Correct |
5 ms |
640 KB |
Output is correct |
31 |
Correct |
5 ms |
640 KB |
Output is correct |
32 |
Correct |
5 ms |
640 KB |
Output is correct |
33 |
Correct |
10 ms |
2048 KB |
Output is correct |
34 |
Correct |
11 ms |
2048 KB |
Output is correct |
35 |
Correct |
34 ms |
2048 KB |
Output is correct |
36 |
Correct |
11 ms |
2176 KB |
Output is correct |
37 |
Correct |
12 ms |
2176 KB |
Output is correct |
38 |
Correct |
46 ms |
2176 KB |
Output is correct |
39 |
Correct |
13 ms |
2432 KB |
Output is correct |
40 |
Correct |
14 ms |
2432 KB |
Output is correct |
41 |
Correct |
56 ms |
2560 KB |
Output is correct |
42 |
Correct |
15 ms |
2688 KB |
Output is correct |
43 |
Correct |
16 ms |
2688 KB |
Output is correct |
44 |
Correct |
71 ms |
2808 KB |
Output is correct |
45 |
Correct |
18 ms |
2944 KB |
Output is correct |
46 |
Correct |
20 ms |
2944 KB |
Output is correct |
47 |
Correct |
81 ms |
3000 KB |
Output is correct |
48 |
Correct |
19 ms |
3200 KB |
Output is correct |
49 |
Correct |
20 ms |
3200 KB |
Output is correct |
50 |
Correct |
98 ms |
3200 KB |
Output is correct |
51 |
Correct |
22 ms |
3456 KB |
Output is correct |
52 |
Correct |
22 ms |
3456 KB |
Output is correct |
53 |
Correct |
121 ms |
3456 KB |
Output is correct |
54 |
Correct |
25 ms |
3584 KB |
Output is correct |
55 |
Correct |
26 ms |
3704 KB |
Output is correct |
56 |
Correct |
128 ms |
3712 KB |
Output is correct |
57 |
Correct |
26 ms |
3840 KB |
Output is correct |
58 |
Correct |
28 ms |
3840 KB |
Output is correct |
59 |
Correct |
150 ms |
3960 KB |
Output is correct |
60 |
Correct |
30 ms |
4128 KB |
Output is correct |
61 |
Correct |
32 ms |
4088 KB |
Output is correct |
62 |
Correct |
180 ms |
4216 KB |
Output is correct |
63 |
Correct |
139 ms |
4096 KB |
Output is correct |
64 |
Correct |
252 ms |
4216 KB |
Output is correct |
65 |
Correct |
205 ms |
4096 KB |
Output is correct |
66 |
Correct |
164 ms |
4096 KB |
Output is correct |
67 |
Correct |
152 ms |
4260 KB |
Output is correct |
68 |
Correct |
79 ms |
4096 KB |
Output is correct |
69 |
Correct |
62 ms |
4096 KB |
Output is correct |
70 |
Correct |
67 ms |
4096 KB |
Output is correct |
71 |
Correct |
59 ms |
4224 KB |
Output is correct |
72 |
Correct |
45 ms |
4096 KB |
Output is correct |
73 |
Correct |
53 ms |
4480 KB |
Output is correct |
74 |
Correct |
80 ms |
4480 KB |
Output is correct |
75 |
Correct |
100 ms |
4480 KB |
Output is correct |
76 |
Correct |
96 ms |
4480 KB |
Output is correct |
77 |
Correct |
102 ms |
4480 KB |
Output is correct |
78 |
Correct |
110 ms |
4480 KB |
Output is correct |
79 |
Correct |
71 ms |
4472 KB |
Output is correct |
80 |
Correct |
84 ms |
4600 KB |
Output is correct |
81 |
Correct |
99 ms |
4472 KB |
Output is correct |
82 |
Correct |
89 ms |
4472 KB |
Output is correct |
83 |
Correct |
114 ms |
4596 KB |
Output is correct |
84 |
Correct |
109 ms |
4348 KB |
Output is correct |
85 |
Correct |
114 ms |
4532 KB |
Output is correct |
86 |
Correct |
123 ms |
4348 KB |
Output is correct |
87 |
Correct |
117 ms |
4340 KB |
Output is correct |
88 |
Correct |
125 ms |
4344 KB |
Output is correct |
89 |
Correct |
134 ms |
4352 KB |
Output is correct |
90 |
Correct |
135 ms |
4332 KB |
Output is correct |
91 |
Correct |
123 ms |
4344 KB |
Output is correct |
92 |
Correct |
126 ms |
4344 KB |
Output is correct |