#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAXN (1000005)
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);
ll N,S;
cin>>N>>S;
pair<ll,ll> start = {-1,-1};
pair<ll,ll> end = {-1,-1};
char arr[N][N];
for(ll i = 0;i < N;i++){
for(ll j = 0;j < N;j++){
cin>>arr[i][j];
if(arr[i][j] == 'M') start = {i,j};
if(arr[i][j] == 'D') end = {i,j};
}
}
queue<pair<ll,ll> > q;
ll fromhive[N][N];
for(ll i = 0;i < N;i++){
for(ll j = 0;j < N;j++){
fromhive[i][j] = 1e18;
if(arr[i][j] == 'H'){
q.push({i,j});
fromhive[i][j] = 0;
}
}
}
int Hy[] = {0,0,-1,1};
int Wx[] = {-1,1,0,0};
while(!q.empty()){
ll y = q.front().first;
ll x = q.front().second;
q.pop();
for(ll i = 0;i < 4;i++){
ll a = y + Hy[i];
ll b = x + Wx[i];
if(a < 0 || a >= N || b < 0 || b >= N || arr[a][b] == 'T' || arr[a][b] == 'D'){
continue;
}
if(fromhive[a][b] == 1e18){
q.push({a,b});
fromhive[a][b] = fromhive[y][x] + 1;
}
}
}
ll high = 1e6 + 5;
ll low = -1;
while(high - low > 1){
ll mid = (high + low) / 2;
ll dist[N][N];
memset(dist,-1,sizeof(dist));
q.push(start);
dist[start.first][start.second] = 0;
ll time = max(0ll,mid);
ll add = 0;
while(true){
queue<pair<ll,ll> > newq;
while(!q.empty()){
ll y = q.front().first;
ll x = q.front().second;
q.pop();
newq.push({y,x});
for(ll i = 0;i < 4;i++){
ll a = y + Hy[i];
ll b = x + Wx[i];
if(a < 0 || a >= N || b < 0 || b >= N || arr[a][b] == 'T'){
continue;
}
if(dist[a][b] == -1 && dist[y][x] + 1 <= S + add && time < fromhive[a][b]){
dist[a][b] = dist[y][x] + 1;
q.push({a,b});
}
}
}
time++;
add += S;
while(!newq.empty()){
q.push(newq.front());
newq.pop();
}
if(q.empty()){
break;
}
}
if(dist[end.first][end.second] == -1){
high = mid;
}else{
low = mid;
}
}
cout<<low<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1060 ms |
336 KB |
Time limit exceeded |
2 |
Execution timed out |
1052 ms |
336 KB |
Time limit exceeded |
3 |
Execution timed out |
1063 ms |
336 KB |
Time limit exceeded |
4 |
Execution timed out |
1067 ms |
336 KB |
Time limit exceeded |
5 |
Execution timed out |
1069 ms |
336 KB |
Time limit exceeded |
6 |
Execution timed out |
1049 ms |
336 KB |
Time limit exceeded |
7 |
Execution timed out |
1070 ms |
11088 KB |
Time limit exceeded |
8 |
Execution timed out |
1065 ms |
336 KB |
Time limit exceeded |
9 |
Execution timed out |
1049 ms |
336 KB |
Time limit exceeded |
10 |
Execution timed out |
1065 ms |
336 KB |
Time limit exceeded |
11 |
Execution timed out |
1057 ms |
336 KB |
Time limit exceeded |
12 |
Execution timed out |
1060 ms |
336 KB |
Time limit exceeded |
13 |
Execution timed out |
1067 ms |
336 KB |
Time limit exceeded |
14 |
Execution timed out |
1055 ms |
336 KB |
Time limit exceeded |
15 |
Execution timed out |
1071 ms |
336 KB |
Time limit exceeded |
16 |
Execution timed out |
1066 ms |
336 KB |
Time limit exceeded |
17 |
Execution timed out |
1063 ms |
336 KB |
Time limit exceeded |
18 |
Execution timed out |
1063 ms |
336 KB |
Time limit exceeded |
19 |
Execution timed out |
1052 ms |
336 KB |
Time limit exceeded |
20 |
Execution timed out |
1047 ms |
336 KB |
Time limit exceeded |
21 |
Execution timed out |
1050 ms |
336 KB |
Time limit exceeded |
22 |
Execution timed out |
1060 ms |
336 KB |
Time limit exceeded |
23 |
Execution timed out |
1051 ms |
336 KB |
Time limit exceeded |
24 |
Execution timed out |
1056 ms |
336 KB |
Time limit exceeded |
25 |
Execution timed out |
1069 ms |
336 KB |
Time limit exceeded |
26 |
Execution timed out |
1069 ms |
336 KB |
Time limit exceeded |
27 |
Execution timed out |
1077 ms |
336 KB |
Time limit exceeded |
28 |
Execution timed out |
1075 ms |
336 KB |
Time limit exceeded |
29 |
Execution timed out |
1059 ms |
336 KB |
Time limit exceeded |
30 |
Execution timed out |
1065 ms |
336 KB |
Time limit exceeded |
31 |
Execution timed out |
1057 ms |
336 KB |
Time limit exceeded |
32 |
Execution timed out |
1055 ms |
336 KB |
Time limit exceeded |
33 |
Execution timed out |
1075 ms |
2384 KB |
Time limit exceeded |
34 |
Execution timed out |
1081 ms |
2384 KB |
Time limit exceeded |
35 |
Execution timed out |
1049 ms |
2384 KB |
Time limit exceeded |
36 |
Execution timed out |
1067 ms |
2896 KB |
Time limit exceeded |
37 |
Execution timed out |
1073 ms |
2896 KB |
Time limit exceeded |
38 |
Execution timed out |
1065 ms |
2896 KB |
Time limit exceeded |
39 |
Execution timed out |
1076 ms |
3664 KB |
Time limit exceeded |
40 |
Execution timed out |
1071 ms |
3664 KB |
Time limit exceeded |
41 |
Execution timed out |
1066 ms |
3664 KB |
Time limit exceeded |
42 |
Execution timed out |
1048 ms |
4432 KB |
Time limit exceeded |
43 |
Execution timed out |
1052 ms |
4432 KB |
Time limit exceeded |
44 |
Execution timed out |
1063 ms |
4432 KB |
Time limit exceeded |
45 |
Execution timed out |
1071 ms |
5456 KB |
Time limit exceeded |
46 |
Execution timed out |
1063 ms |
5456 KB |
Time limit exceeded |
47 |
Execution timed out |
1059 ms |
5468 KB |
Time limit exceeded |
48 |
Execution timed out |
1040 ms |
6224 KB |
Time limit exceeded |
49 |
Execution timed out |
1058 ms |
6224 KB |
Time limit exceeded |
50 |
Execution timed out |
1065 ms |
6224 KB |
Time limit exceeded |
51 |
Execution timed out |
1046 ms |
7248 KB |
Time limit exceeded |
52 |
Execution timed out |
1042 ms |
7248 KB |
Time limit exceeded |
53 |
Execution timed out |
1069 ms |
7248 KB |
Time limit exceeded |
54 |
Execution timed out |
1054 ms |
8528 KB |
Time limit exceeded |
55 |
Execution timed out |
1073 ms |
8528 KB |
Time limit exceeded |
56 |
Execution timed out |
1046 ms |
8528 KB |
Time limit exceeded |
57 |
Execution timed out |
1056 ms |
9552 KB |
Time limit exceeded |
58 |
Execution timed out |
1060 ms |
9808 KB |
Time limit exceeded |
59 |
Execution timed out |
1072 ms |
9808 KB |
Time limit exceeded |
60 |
Execution timed out |
1051 ms |
11088 KB |
Time limit exceeded |
61 |
Execution timed out |
1052 ms |
11088 KB |
Time limit exceeded |
62 |
Execution timed out |
1062 ms |
11088 KB |
Time limit exceeded |
63 |
Execution timed out |
1060 ms |
11080 KB |
Time limit exceeded |
64 |
Execution timed out |
1076 ms |
11088 KB |
Time limit exceeded |
65 |
Execution timed out |
1055 ms |
11080 KB |
Time limit exceeded |
66 |
Execution timed out |
1054 ms |
11080 KB |
Time limit exceeded |
67 |
Execution timed out |
1071 ms |
11080 KB |
Time limit exceeded |
68 |
Execution timed out |
1051 ms |
11088 KB |
Time limit exceeded |
69 |
Execution timed out |
1062 ms |
11120 KB |
Time limit exceeded |
70 |
Execution timed out |
1062 ms |
11080 KB |
Time limit exceeded |
71 |
Execution timed out |
1060 ms |
11080 KB |
Time limit exceeded |
72 |
Execution timed out |
1060 ms |
11080 KB |
Time limit exceeded |
73 |
Execution timed out |
1076 ms |
11592 KB |
Time limit exceeded |
74 |
Execution timed out |
1069 ms |
11548 KB |
Time limit exceeded |
75 |
Execution timed out |
1065 ms |
11504 KB |
Time limit exceeded |
76 |
Execution timed out |
1052 ms |
11600 KB |
Time limit exceeded |
77 |
Execution timed out |
1051 ms |
11540 KB |
Time limit exceeded |
78 |
Execution timed out |
1069 ms |
11336 KB |
Time limit exceeded |
79 |
Execution timed out |
1067 ms |
11336 KB |
Time limit exceeded |
80 |
Execution timed out |
1060 ms |
11336 KB |
Time limit exceeded |
81 |
Execution timed out |
1061 ms |
11456 KB |
Time limit exceeded |
82 |
Execution timed out |
1059 ms |
11344 KB |
Time limit exceeded |
83 |
Execution timed out |
1082 ms |
11336 KB |
Time limit exceeded |
84 |
Execution timed out |
1064 ms |
11344 KB |
Time limit exceeded |
85 |
Execution timed out |
1068 ms |
11344 KB |
Time limit exceeded |
86 |
Execution timed out |
1066 ms |
11336 KB |
Time limit exceeded |
87 |
Execution timed out |
1063 ms |
11336 KB |
Time limit exceeded |
88 |
Execution timed out |
1054 ms |
11336 KB |
Time limit exceeded |
89 |
Execution timed out |
1072 ms |
11184 KB |
Time limit exceeded |
90 |
Execution timed out |
1054 ms |
11344 KB |
Time limit exceeded |
91 |
Execution timed out |
1053 ms |
11336 KB |
Time limit exceeded |
92 |
Execution timed out |
1075 ms |
11344 KB |
Time limit exceeded |