#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>,pair<int,int> > > q;
queue<pair<pair<int,int>,int> > qb;
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 x)
{
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},{x,0}});
visited[sx][sy] = true;
while(!q.empty())
{
int x = q.front().first.first,y = q.front().first.second,tm = q.front().second.first,st = q.front().second.second;
q.pop();
if(x==tx and y==ty) return true;
if(st==0 and tm>=bee[x][y]) continue;
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;
visited[ai][aj] = true;
q.push({{ai,aj},{tm+(int)(st==0),(st+1)%s}});
}
}
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 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
6 |
Incorrect |
5 ms |
408 KB |
Output isn't correct |
7 |
Incorrect |
143 ms |
4856 KB |
Output isn't correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
12 |
Correct |
6 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 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Incorrect |
5 ms |
512 KB |
Output isn't correct |
19 |
Correct |
5 ms |
512 KB |
Output is correct |
20 |
Incorrect |
5 ms |
512 KB |
Output isn't correct |
21 |
Correct |
5 ms |
512 KB |
Output is correct |
22 |
Incorrect |
5 ms |
512 KB |
Output isn't correct |
23 |
Correct |
5 ms |
640 KB |
Output is correct |
24 |
Incorrect |
5 ms |
640 KB |
Output isn't correct |
25 |
Correct |
5 ms |
640 KB |
Output is correct |
26 |
Incorrect |
5 ms |
640 KB |
Output isn't correct |
27 |
Correct |
5 ms |
640 KB |
Output is correct |
28 |
Incorrect |
5 ms |
640 KB |
Output isn't correct |
29 |
Correct |
5 ms |
640 KB |
Output is correct |
30 |
Incorrect |
5 ms |
640 KB |
Output isn't correct |
31 |
Correct |
5 ms |
768 KB |
Output is correct |
32 |
Incorrect |
5 ms |
768 KB |
Output isn't correct |
33 |
Correct |
10 ms |
2176 KB |
Output is correct |
34 |
Incorrect |
11 ms |
2176 KB |
Output isn't correct |
35 |
Incorrect |
25 ms |
2176 KB |
Output isn't correct |
36 |
Correct |
12 ms |
2448 KB |
Output is correct |
37 |
Incorrect |
12 ms |
2432 KB |
Output isn't correct |
38 |
Incorrect |
24 ms |
2432 KB |
Output isn't correct |
39 |
Correct |
13 ms |
2688 KB |
Output is correct |
40 |
Incorrect |
14 ms |
2688 KB |
Output isn't correct |
41 |
Incorrect |
33 ms |
2688 KB |
Output isn't correct |
42 |
Correct |
15 ms |
2944 KB |
Output is correct |
43 |
Incorrect |
17 ms |
2944 KB |
Output isn't correct |
44 |
Incorrect |
36 ms |
2944 KB |
Output isn't correct |
45 |
Correct |
17 ms |
3200 KB |
Output is correct |
46 |
Incorrect |
19 ms |
3200 KB |
Output isn't correct |
47 |
Incorrect |
65 ms |
3320 KB |
Output isn't correct |
48 |
Correct |
20 ms |
3584 KB |
Output is correct |
49 |
Incorrect |
21 ms |
3584 KB |
Output isn't correct |
50 |
Incorrect |
65 ms |
3584 KB |
Output isn't correct |
51 |
Correct |
22 ms |
3840 KB |
Output is correct |
52 |
Incorrect |
24 ms |
3840 KB |
Output isn't correct |
53 |
Incorrect |
62 ms |
3840 KB |
Output isn't correct |
54 |
Correct |
25 ms |
4096 KB |
Output is correct |
55 |
Incorrect |
26 ms |
4096 KB |
Output isn't correct |
56 |
Incorrect |
95 ms |
4096 KB |
Output isn't correct |
57 |
Correct |
28 ms |
4480 KB |
Output is correct |
58 |
Incorrect |
30 ms |
4480 KB |
Output isn't correct |
59 |
Incorrect |
109 ms |
4376 KB |
Output isn't correct |
60 |
Correct |
31 ms |
4728 KB |
Output is correct |
61 |
Incorrect |
35 ms |
4728 KB |
Output isn't correct |
62 |
Incorrect |
83 ms |
4728 KB |
Output isn't correct |
63 |
Correct |
139 ms |
4788 KB |
Output is correct |
64 |
Correct |
245 ms |
4832 KB |
Output is correct |
65 |
Incorrect |
216 ms |
4824 KB |
Output isn't correct |
66 |
Correct |
168 ms |
4856 KB |
Output is correct |
67 |
Correct |
150 ms |
4728 KB |
Output is correct |
68 |
Incorrect |
78 ms |
4736 KB |
Output isn't correct |
69 |
Correct |
85 ms |
4736 KB |
Output is correct |
70 |
Incorrect |
55 ms |
4736 KB |
Output isn't correct |
71 |
Incorrect |
51 ms |
4736 KB |
Output isn't correct |
72 |
Correct |
48 ms |
4736 KB |
Output is correct |
73 |
Correct |
57 ms |
5120 KB |
Output is correct |
74 |
Incorrect |
87 ms |
5120 KB |
Output isn't correct |
75 |
Incorrect |
92 ms |
5120 KB |
Output isn't correct |
76 |
Incorrect |
99 ms |
5200 KB |
Output isn't correct |
77 |
Incorrect |
94 ms |
5120 KB |
Output isn't correct |
78 |
Incorrect |
111 ms |
5256 KB |
Output isn't correct |
79 |
Incorrect |
132 ms |
5368 KB |
Output isn't correct |
80 |
Incorrect |
91 ms |
5112 KB |
Output isn't correct |
81 |
Incorrect |
106 ms |
5112 KB |
Output isn't correct |
82 |
Incorrect |
101 ms |
5112 KB |
Output isn't correct |
83 |
Incorrect |
122 ms |
5236 KB |
Output isn't correct |
84 |
Incorrect |
138 ms |
4988 KB |
Output isn't correct |
85 |
Incorrect |
113 ms |
4988 KB |
Output isn't correct |
86 |
Incorrect |
128 ms |
5108 KB |
Output isn't correct |
87 |
Incorrect |
123 ms |
5108 KB |
Output isn't correct |
88 |
Incorrect |
126 ms |
4992 KB |
Output isn't correct |
89 |
Incorrect |
151 ms |
4984 KB |
Output isn't correct |
90 |
Incorrect |
130 ms |
5112 KB |
Output isn't correct |
91 |
Incorrect |
120 ms |
4984 KB |
Output isn't correct |
92 |
Incorrect |
121 ms |
4992 KB |
Output isn't correct |