#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i=a; i<=b; i++)
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define pb push_back
#define f first
#define s second
#define nl '\n'
typedef long long ll;
// #define int ll
typedef vector<int> vi;
typedef pair<int, int> pii;
#define nl '\n'
const int N=800;
int n, s;
string grid[N];
int bee[N][N];
int dist[N][N];
bool block[N][N];
int dx[]={0, 0, 1, -1};
int dy[]={1, -1, 0, 0};
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> s;
for(int i=0; i<n; i++){
cin >> grid[i];
}
auto check=[&](int t)->bool{
queue<pair<int,int>> q;
rep(i,0,n-1) rep(j,0,n-1){
if(grid[i][j]=='M'){
if(bee[i][j]<=t) return 0;
q.push({i,j});
dist[i][j]=0;
}
else dist[i][j]=1e9;
}
while(!q.empty()){
pair<int,int> p=q.front(); q.pop();
int x=p.first, y=p.second;
if(grid[x][y]=='D') return 1;
// cout << x << y << '\n';
for(int k=0; k<4; k++){
int nx=x+dx[k];
int ny=y+dy[k];
if(nx<0 || nx==n || ny<0 || ny==n || grid[nx][ny]=='T' || dist[nx][ny]!=1e9) continue;
if(bee[nx][ny]>t+(dist[x][y]+1)/s){
dist[nx][ny]=dist[x][y]+1;
q.push({nx,ny});
}
}
}
return 0;
};
queue<pair<int,int>> q;
rep(i,0,n-1) rep(j,0,n-1){
if(grid[i][j]=='H') q.push({i,j}), bee[i][j]=0;
else bee[i][j]=1e9;
}
while(!q.empty()){
pair<int,int> p=q.front();
int x=p.first, y=p.second;
q.pop();
for(int k=0; k<4; k++){
int nx=x+dx[k];
int ny=y+dy[k];
if(nx<0 || nx==n || ny<0 || ny==n || grid[nx][ny]=='T' || grid[nx][ny]=='D' || bee[nx][ny]!=1e9) continue;
bee[nx][ny]=bee[x][y]+1;
q.push({nx,ny});
}
}
// rep(i,0,n-1){
// rep(j,0,n-1){
// cout << bee[i][j] << " ";
// }
// cout << nl;
// }
int x=0;
for(int i=20; i>=0; i--){
if(check(x+(1<<i))) x+=(1<<i);
}
cout << (check(x)?x:-1);
// rep(i,1,1) cout << check(i) << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
77 ms |
6080 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
740 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
0 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
0 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
724 KB |
Output is correct |
30 |
Correct |
1 ms |
724 KB |
Output is correct |
31 |
Correct |
1 ms |
724 KB |
Output is correct |
32 |
Correct |
1 ms |
724 KB |
Output is correct |
33 |
Correct |
5 ms |
2644 KB |
Output is correct |
34 |
Correct |
5 ms |
2644 KB |
Output is correct |
35 |
Correct |
19 ms |
2680 KB |
Output is correct |
36 |
Correct |
4 ms |
3028 KB |
Output is correct |
37 |
Correct |
6 ms |
3036 KB |
Output is correct |
38 |
Correct |
27 ms |
2932 KB |
Output is correct |
39 |
Correct |
5 ms |
3284 KB |
Output is correct |
40 |
Correct |
9 ms |
3388 KB |
Output is correct |
41 |
Correct |
30 ms |
3396 KB |
Output is correct |
42 |
Correct |
7 ms |
3668 KB |
Output is correct |
43 |
Correct |
11 ms |
3720 KB |
Output is correct |
44 |
Correct |
38 ms |
3708 KB |
Output is correct |
45 |
Correct |
8 ms |
4120 KB |
Output is correct |
46 |
Correct |
10 ms |
4056 KB |
Output is correct |
47 |
Correct |
61 ms |
4112 KB |
Output is correct |
48 |
Correct |
9 ms |
4436 KB |
Output is correct |
49 |
Correct |
14 ms |
4504 KB |
Output is correct |
50 |
Correct |
57 ms |
4436 KB |
Output is correct |
51 |
Correct |
10 ms |
4820 KB |
Output is correct |
52 |
Correct |
13 ms |
4764 KB |
Output is correct |
53 |
Correct |
74 ms |
4872 KB |
Output is correct |
54 |
Correct |
12 ms |
5204 KB |
Output is correct |
55 |
Correct |
15 ms |
5272 KB |
Output is correct |
56 |
Correct |
79 ms |
5240 KB |
Output is correct |
57 |
Correct |
14 ms |
5588 KB |
Output is correct |
58 |
Correct |
17 ms |
5656 KB |
Output is correct |
59 |
Correct |
92 ms |
5644 KB |
Output is correct |
60 |
Correct |
14 ms |
6032 KB |
Output is correct |
61 |
Correct |
21 ms |
6032 KB |
Output is correct |
62 |
Correct |
112 ms |
6024 KB |
Output is correct |
63 |
Correct |
104 ms |
6012 KB |
Output is correct |
64 |
Correct |
184 ms |
5972 KB |
Output is correct |
65 |
Correct |
170 ms |
6028 KB |
Output is correct |
66 |
Correct |
144 ms |
6076 KB |
Output is correct |
67 |
Correct |
115 ms |
5972 KB |
Output is correct |
68 |
Correct |
52 ms |
6056 KB |
Output is correct |
69 |
Correct |
46 ms |
5984 KB |
Output is correct |
70 |
Correct |
42 ms |
5972 KB |
Output is correct |
71 |
Correct |
40 ms |
6000 KB |
Output is correct |
72 |
Correct |
31 ms |
5988 KB |
Output is correct |
73 |
Correct |
31 ms |
6240 KB |
Output is correct |
74 |
Correct |
54 ms |
6228 KB |
Output is correct |
75 |
Correct |
61 ms |
6296 KB |
Output is correct |
76 |
Correct |
64 ms |
6236 KB |
Output is correct |
77 |
Correct |
58 ms |
6296 KB |
Output is correct |
78 |
Correct |
75 ms |
6260 KB |
Output is correct |
79 |
Correct |
52 ms |
6228 KB |
Output is correct |
80 |
Correct |
62 ms |
6268 KB |
Output is correct |
81 |
Correct |
71 ms |
6220 KB |
Output is correct |
82 |
Correct |
62 ms |
6228 KB |
Output is correct |
83 |
Correct |
72 ms |
6232 KB |
Output is correct |
84 |
Correct |
65 ms |
6228 KB |
Output is correct |
85 |
Correct |
65 ms |
6224 KB |
Output is correct |
86 |
Correct |
71 ms |
6228 KB |
Output is correct |
87 |
Correct |
67 ms |
6176 KB |
Output is correct |
88 |
Correct |
73 ms |
6188 KB |
Output is correct |
89 |
Correct |
73 ms |
6180 KB |
Output is correct |
90 |
Correct |
76 ms |
6132 KB |
Output is correct |
91 |
Correct |
75 ms |
6092 KB |
Output is correct |
92 |
Correct |
73 ms |
6120 KB |
Output is correct |