#include <bits/stdc++.h>
using namespace std;
const int nx=805;
int n, s, mn[nx][nx], dx[4]={1, 0, 0, -1}, dy[4]={0, 1, -1, 0}, dp[nx][nx];
char mp[nx][nx];
pair<int, int> st, tg;
queue<pair<int, int>> q;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>s;
for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) cin>>mp[i][j], mn[i][j]=1e9;
for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) if (mp[i][j]=='M') st={i, j};
for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) if (mp[i][j]=='H') q.push({i, j}), mn[i][j]=0;
for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) if (mp[i][j]=='D') tg={i, j};
while (!q.empty())
{
auto [x, y]=q.front();
q.pop();
for (int i=0; i<4; i++)
{
int cx=x+dx[i], cy=y+dy[i];
if (cx<1||cx>n||cy<1||cy>n||mp[cx][cy]=='T'||mp[cx][cy]=='D') continue;
if (mn[cx][cy]>mn[x][y]+1) mn[cx][cy]=mn[x][y]+1, q.push({cx, cy});
}
}
int l=0, r=2*n;
while (l<r)
{
int md=(l+r+1)/2;
for (int i=1; i<=n; i++) for (int j=1; j<=n; j++) dp[i][j]=1e9;
if (mn[st.first][st.second]<=md)
{
r=md-1;
continue;
}
dp[st.first][st.second]=0;
q.push({st.first, st.second});
while (!q.empty())
{
auto [x, y]=q.front();
q.pop();
for (int i=0; i<4; i++)
{
int cx=x+dx[i], cy=y+dy[i];
if (cx<1||cx>n||cy<1||cy>n||mp[cx][cy]=='T'||mp[cx][cy]=='H') continue;
if (dp[cx][cy]>dp[x][y]+1&&(md+(dp[x][y]+1)/s)<mn[cx][cy]) dp[cx][cy]=dp[x][y]+1, q.push({cx, cy});
}
}
if (dp[tg.first][tg.second]!=1e9) l=md;
else r=md-1;
}
if (l==0) cout<<-1;
else cout<<l;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
0 ms |
4560 KB |
Output is correct |
5 |
Incorrect |
0 ms |
4444 KB |
Output isn't correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
43 ms |
6880 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
0 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
16 |
Incorrect |
0 ms |
4444 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
19 |
Incorrect |
0 ms |
4444 KB |
Output isn't correct |
20 |
Incorrect |
0 ms |
4444 KB |
Output isn't correct |
21 |
Incorrect |
0 ms |
4444 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
4440 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
33 |
Incorrect |
8 ms |
4956 KB |
Output isn't correct |
34 |
Incorrect |
10 ms |
4700 KB |
Output isn't correct |
35 |
Correct |
11 ms |
4700 KB |
Output is correct |
36 |
Incorrect |
9 ms |
4960 KB |
Output isn't correct |
37 |
Incorrect |
12 ms |
5060 KB |
Output isn't correct |
38 |
Correct |
15 ms |
4904 KB |
Output is correct |
39 |
Incorrect |
13 ms |
5208 KB |
Output isn't correct |
40 |
Incorrect |
15 ms |
5212 KB |
Output isn't correct |
41 |
Correct |
20 ms |
5212 KB |
Output is correct |
42 |
Incorrect |
15 ms |
5212 KB |
Output isn't correct |
43 |
Incorrect |
18 ms |
5456 KB |
Output isn't correct |
44 |
Correct |
23 ms |
5212 KB |
Output is correct |
45 |
Incorrect |
19 ms |
5464 KB |
Output isn't correct |
46 |
Incorrect |
29 ms |
5468 KB |
Output isn't correct |
47 |
Correct |
28 ms |
5468 KB |
Output is correct |
48 |
Incorrect |
21 ms |
5884 KB |
Output isn't correct |
49 |
Incorrect |
28 ms |
5724 KB |
Output isn't correct |
50 |
Correct |
33 ms |
5880 KB |
Output is correct |
51 |
Incorrect |
26 ms |
6100 KB |
Output isn't correct |
52 |
Incorrect |
35 ms |
5980 KB |
Output isn't correct |
53 |
Correct |
42 ms |
5980 KB |
Output is correct |
54 |
Incorrect |
30 ms |
6236 KB |
Output isn't correct |
55 |
Incorrect |
42 ms |
6236 KB |
Output isn't correct |
56 |
Correct |
60 ms |
6236 KB |
Output is correct |
57 |
Incorrect |
35 ms |
6492 KB |
Output isn't correct |
58 |
Incorrect |
45 ms |
6492 KB |
Output isn't correct |
59 |
Correct |
58 ms |
6492 KB |
Output is correct |
60 |
Incorrect |
38 ms |
6744 KB |
Output isn't correct |
61 |
Incorrect |
51 ms |
6748 KB |
Output isn't correct |
62 |
Correct |
63 ms |
6748 KB |
Output is correct |
63 |
Incorrect |
61 ms |
6748 KB |
Output isn't correct |
64 |
Correct |
84 ms |
6752 KB |
Output is correct |
65 |
Correct |
87 ms |
6748 KB |
Output is correct |
66 |
Correct |
73 ms |
6748 KB |
Output is correct |
67 |
Correct |
72 ms |
6744 KB |
Output is correct |
68 |
Incorrect |
32 ms |
6744 KB |
Output isn't correct |
69 |
Correct |
31 ms |
6744 KB |
Output is correct |
70 |
Correct |
25 ms |
6748 KB |
Output is correct |
71 |
Correct |
24 ms |
6748 KB |
Output is correct |
72 |
Correct |
21 ms |
6816 KB |
Output is correct |
73 |
Correct |
23 ms |
7004 KB |
Output is correct |
74 |
Correct |
32 ms |
7004 KB |
Output is correct |
75 |
Correct |
42 ms |
7000 KB |
Output is correct |
76 |
Correct |
33 ms |
7004 KB |
Output is correct |
77 |
Correct |
41 ms |
7008 KB |
Output is correct |
78 |
Correct |
39 ms |
7004 KB |
Output is correct |
79 |
Correct |
40 ms |
7004 KB |
Output is correct |
80 |
Correct |
46 ms |
7004 KB |
Output is correct |
81 |
Correct |
40 ms |
7024 KB |
Output is correct |
82 |
Correct |
40 ms |
7024 KB |
Output is correct |
83 |
Incorrect |
40 ms |
6744 KB |
Output isn't correct |
84 |
Correct |
41 ms |
6748 KB |
Output is correct |
85 |
Correct |
49 ms |
6748 KB |
Output is correct |
86 |
Correct |
43 ms |
6904 KB |
Output is correct |
87 |
Correct |
46 ms |
6980 KB |
Output is correct |
88 |
Incorrect |
47 ms |
6748 KB |
Output isn't correct |
89 |
Correct |
45 ms |
6740 KB |
Output is correct |
90 |
Correct |
60 ms |
6748 KB |
Output is correct |
91 |
Correct |
47 ms |
6928 KB |
Output is correct |
92 |
Correct |
45 ms |
6928 KB |
Output is correct |