#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], vs[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]=0, vs[i][j]=0;
vector<pair<int, int>> cur, nxt;
vs[st.first][st.second]=1;
cur.push_back(st);
int cnt=md;
while (!cur.empty())
{
nxt.clear();
while (!q.empty()) q.pop();
for (auto x:cur) if (mn[x.first][x.second]>cnt) q.push(x), dp[x.first][x.second]=s;
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'||vs[cx][cy]) continue;
vs[cx][cy]=1;
dp[cx][cy]=dp[x][y]-1;
nxt.push_back({cx, cy});
if (dp[cx][cy]!=0) q.push({cx, cy});
}
}
cur=nxt;
cnt++;
}
if (vs[tg.first][tg.second]) l=md;
else r=md-1;
}
if (l==0) cout<<-1;
else cout<<l;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6488 KB |
Output is correct |
3 |
Correct |
1 ms |
6744 KB |
Output is correct |
4 |
Correct |
0 ms |
6492 KB |
Output is correct |
5 |
Incorrect |
0 ms |
6492 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
6492 KB |
Output isn't correct |
7 |
Incorrect |
88 ms |
9336 KB |
Output isn't correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
0 ms |
6492 KB |
Output is correct |
11 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
12 |
Incorrect |
0 ms |
6492 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
6656 KB |
Output isn't correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
18 |
Incorrect |
0 ms |
6492 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
6744 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
6492 KB |
Output isn't correct |
33 |
Incorrect |
8 ms |
7260 KB |
Output isn't correct |
34 |
Incorrect |
17 ms |
7300 KB |
Output isn't correct |
35 |
Incorrect |
23 ms |
8200 KB |
Output isn't correct |
36 |
Incorrect |
10 ms |
7260 KB |
Output isn't correct |
37 |
Incorrect |
25 ms |
7460 KB |
Output isn't correct |
38 |
Incorrect |
29 ms |
8540 KB |
Output isn't correct |
39 |
Incorrect |
12 ms |
7588 KB |
Output isn't correct |
40 |
Incorrect |
28 ms |
7516 KB |
Output isn't correct |
41 |
Incorrect |
38 ms |
9132 KB |
Output isn't correct |
42 |
Incorrect |
18 ms |
7516 KB |
Output isn't correct |
43 |
Incorrect |
36 ms |
7768 KB |
Output isn't correct |
44 |
Incorrect |
48 ms |
9472 KB |
Output isn't correct |
45 |
Incorrect |
20 ms |
7904 KB |
Output isn't correct |
46 |
Incorrect |
47 ms |
7940 KB |
Output isn't correct |
47 |
Incorrect |
60 ms |
10276 KB |
Output isn't correct |
48 |
Incorrect |
23 ms |
8024 KB |
Output isn't correct |
49 |
Incorrect |
55 ms |
8024 KB |
Output isn't correct |
50 |
Incorrect |
69 ms |
10284 KB |
Output isn't correct |
51 |
Incorrect |
26 ms |
8028 KB |
Output isn't correct |
52 |
Incorrect |
86 ms |
8024 KB |
Output isn't correct |
53 |
Incorrect |
82 ms |
11712 KB |
Output isn't correct |
54 |
Incorrect |
31 ms |
8348 KB |
Output isn't correct |
55 |
Incorrect |
75 ms |
8280 KB |
Output isn't correct |
56 |
Incorrect |
101 ms |
12416 KB |
Output isn't correct |
57 |
Incorrect |
34 ms |
8280 KB |
Output isn't correct |
58 |
Incorrect |
88 ms |
8540 KB |
Output isn't correct |
59 |
Incorrect |
122 ms |
14620 KB |
Output isn't correct |
60 |
Incorrect |
39 ms |
8536 KB |
Output isn't correct |
61 |
Incorrect |
97 ms |
8540 KB |
Output isn't correct |
62 |
Incorrect |
129 ms |
13276 KB |
Output isn't correct |
63 |
Incorrect |
75 ms |
8536 KB |
Output isn't correct |
64 |
Correct |
167 ms |
14720 KB |
Output is correct |
65 |
Incorrect |
145 ms |
8540 KB |
Output isn't correct |
66 |
Correct |
111 ms |
8540 KB |
Output is correct |
67 |
Correct |
100 ms |
8536 KB |
Output is correct |
68 |
Incorrect |
45 ms |
8800 KB |
Output isn't correct |
69 |
Correct |
37 ms |
9492 KB |
Output is correct |
70 |
Incorrect |
29 ms |
8540 KB |
Output isn't correct |
71 |
Incorrect |
24 ms |
8528 KB |
Output isn't correct |
72 |
Correct |
23 ms |
9052 KB |
Output is correct |
73 |
Correct |
24 ms |
8792 KB |
Output is correct |
74 |
Incorrect |
47 ms |
13132 KB |
Output isn't correct |
75 |
Incorrect |
66 ms |
8792 KB |
Output isn't correct |
76 |
Incorrect |
49 ms |
8796 KB |
Output isn't correct |
77 |
Incorrect |
55 ms |
8792 KB |
Output isn't correct |
78 |
Incorrect |
68 ms |
9208 KB |
Output isn't correct |
79 |
Incorrect |
71 ms |
14872 KB |
Output isn't correct |
80 |
Incorrect |
52 ms |
8796 KB |
Output isn't correct |
81 |
Incorrect |
73 ms |
8792 KB |
Output isn't correct |
82 |
Incorrect |
58 ms |
9048 KB |
Output isn't correct |
83 |
Incorrect |
85 ms |
9304 KB |
Output isn't correct |
84 |
Incorrect |
89 ms |
15152 KB |
Output isn't correct |
85 |
Incorrect |
73 ms |
9052 KB |
Output isn't correct |
86 |
Incorrect |
72 ms |
8796 KB |
Output isn't correct |
87 |
Incorrect |
69 ms |
9356 KB |
Output isn't correct |
88 |
Incorrect |
78 ms |
9084 KB |
Output isn't correct |
89 |
Incorrect |
63 ms |
15556 KB |
Output isn't correct |
90 |
Incorrect |
83 ms |
9364 KB |
Output isn't correct |
91 |
Incorrect |
74 ms |
9952 KB |
Output isn't correct |
92 |
Incorrect |
80 ms |
9364 KB |
Output isn't correct |