#include<bits/stdc++.h>
#define fi first
#define sc second
#define fr(i,a,b) for(int i=a;i<=b;i++)
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int H[] = {-1,-1,1,1};
const int M = 805;
int n,s,f[M][M];
bool cx[M][M];
pair<int,int> st,fn;
string a[M];
struct dota{
int x,y,d;
};
void BFS_hive(){
queue<dota> q;
memset(cx,false,sizeof(cx));
fo(i,1,n)
fo(j,1,n) {
if(a[i][j] != 'T') cx[i][j] = true;
if(a[i][j] == 'H') q.push({i,j,0}), f[i][j] = 0, cx[i][j] = false;
if(a[i][j] == 'D') cx[i][j] = false;
}
while(!q.empty()){
int x = q.front().x, y = q.front().y, d = q.front().d;
q.pop();
f[x][y] = d;
int xi,yi;
fo(k,0,3){
if(k % 2) xi = x + H[k], yi = y;
else xi = x, yi = y + H[k];
if(cx[xi][yi]) q.push({xi,yi,d+1}), cx[xi][yi] = false;
}
}
}
bool check(int t){
queue<dota> q;
memset(cx,false,sizeof(cx));
fo(i,1,n)
fo(j,1,n) if(a[i][j] != 'T') cx[i][j] = true;
q.push({st.fi,st.sc,0});
cx[st.fi][st.sc] = false;
while(!q.empty()){
int x = q.front().x, y = q.front().y, d = q.front().d;
q.pop();
if(x == fn.fi && y == fn.sc) return true;
int xi,yi;
fo(k,0,3){
if(k % 2) xi = x + H[k], yi = y;
else xi = x, yi = y + H[k];
if(cx[xi][yi] && (d + 1) / s < f[xi][yi] - t) q.push({xi,yi,d+1}), cx[xi][yi] = false;
}
}
return false;
}
int main()
{
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n >> s;
fo(i,1,n) cin >> a[i], a[i] = "*" + a[i];
fo(i,1,n)
fo(j,1,n)
if(a[i][j] == 'M') st = {i,j};
else if(a[i][j] == 'D') fn = {i,j};
BFS_hive();
f[fn.fi][fn.sc] = INT_MAX;
int l = 0, r = f[st.fi][st.sc]-1, mid,res=-1;
while(l <= r){
mid = (l + r) / 2;
if(check(mid)) l = mid+1, res = mid;
else r = mid-1;
}
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
53 ms |
4320 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
1 ms |
972 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
1 ms |
972 KB |
Output is correct |
21 |
Correct |
1 ms |
1100 KB |
Output is correct |
22 |
Correct |
1 ms |
1100 KB |
Output is correct |
23 |
Correct |
1 ms |
1100 KB |
Output is correct |
24 |
Correct |
1 ms |
1100 KB |
Output is correct |
25 |
Correct |
1 ms |
1100 KB |
Output is correct |
26 |
Correct |
1 ms |
1100 KB |
Output is correct |
27 |
Correct |
1 ms |
1100 KB |
Output is correct |
28 |
Correct |
1 ms |
1100 KB |
Output is correct |
29 |
Correct |
1 ms |
1100 KB |
Output is correct |
30 |
Correct |
1 ms |
1100 KB |
Output is correct |
31 |
Correct |
1 ms |
1228 KB |
Output is correct |
32 |
Correct |
1 ms |
1228 KB |
Output is correct |
33 |
Correct |
4 ms |
2124 KB |
Output is correct |
34 |
Correct |
5 ms |
2124 KB |
Output is correct |
35 |
Correct |
12 ms |
2224 KB |
Output is correct |
36 |
Correct |
5 ms |
2380 KB |
Output is correct |
37 |
Correct |
5 ms |
2380 KB |
Output is correct |
38 |
Correct |
15 ms |
2420 KB |
Output is correct |
39 |
Correct |
6 ms |
2500 KB |
Output is correct |
40 |
Correct |
6 ms |
2508 KB |
Output is correct |
41 |
Correct |
20 ms |
2628 KB |
Output is correct |
42 |
Correct |
7 ms |
2788 KB |
Output is correct |
43 |
Correct |
7 ms |
2764 KB |
Output is correct |
44 |
Correct |
26 ms |
2816 KB |
Output is correct |
45 |
Correct |
8 ms |
3036 KB |
Output is correct |
46 |
Correct |
8 ms |
3020 KB |
Output is correct |
47 |
Correct |
31 ms |
3024 KB |
Output is correct |
48 |
Correct |
9 ms |
3148 KB |
Output is correct |
49 |
Correct |
10 ms |
3176 KB |
Output is correct |
50 |
Correct |
37 ms |
3148 KB |
Output is correct |
51 |
Correct |
11 ms |
3404 KB |
Output is correct |
52 |
Correct |
11 ms |
3404 KB |
Output is correct |
53 |
Correct |
49 ms |
3484 KB |
Output is correct |
54 |
Correct |
12 ms |
3704 KB |
Output is correct |
55 |
Correct |
13 ms |
3660 KB |
Output is correct |
56 |
Correct |
54 ms |
3696 KB |
Output is correct |
57 |
Correct |
15 ms |
3880 KB |
Output is correct |
58 |
Correct |
15 ms |
3936 KB |
Output is correct |
59 |
Correct |
59 ms |
3932 KB |
Output is correct |
60 |
Correct |
16 ms |
4044 KB |
Output is correct |
61 |
Correct |
17 ms |
4172 KB |
Output is correct |
62 |
Correct |
68 ms |
4172 KB |
Output is correct |
63 |
Correct |
95 ms |
4152 KB |
Output is correct |
64 |
Correct |
134 ms |
4044 KB |
Output is correct |
65 |
Correct |
132 ms |
4100 KB |
Output is correct |
66 |
Correct |
101 ms |
4172 KB |
Output is correct |
67 |
Correct |
94 ms |
4172 KB |
Output is correct |
68 |
Correct |
51 ms |
4172 KB |
Output is correct |
69 |
Correct |
45 ms |
4172 KB |
Output is correct |
70 |
Correct |
43 ms |
4204 KB |
Output is correct |
71 |
Correct |
40 ms |
4196 KB |
Output is correct |
72 |
Correct |
42 ms |
4088 KB |
Output is correct |
73 |
Correct |
25 ms |
4556 KB |
Output is correct |
74 |
Correct |
42 ms |
4492 KB |
Output is correct |
75 |
Correct |
40 ms |
4576 KB |
Output is correct |
76 |
Correct |
41 ms |
4576 KB |
Output is correct |
77 |
Correct |
43 ms |
4556 KB |
Output is correct |
78 |
Correct |
44 ms |
4552 KB |
Output is correct |
79 |
Correct |
38 ms |
4492 KB |
Output is correct |
80 |
Correct |
41 ms |
4428 KB |
Output is correct |
81 |
Correct |
44 ms |
4500 KB |
Output is correct |
82 |
Correct |
40 ms |
4468 KB |
Output is correct |
83 |
Correct |
55 ms |
4456 KB |
Output is correct |
84 |
Correct |
46 ms |
4428 KB |
Output is correct |
85 |
Correct |
47 ms |
4344 KB |
Output is correct |
86 |
Correct |
55 ms |
4444 KB |
Output is correct |
87 |
Correct |
45 ms |
4428 KB |
Output is correct |
88 |
Correct |
52 ms |
4396 KB |
Output is correct |
89 |
Correct |
50 ms |
4300 KB |
Output is correct |
90 |
Correct |
55 ms |
4320 KB |
Output is correct |
91 |
Correct |
54 ms |
4388 KB |
Output is correct |
92 |
Correct |
54 ms |
4380 KB |
Output is correct |