#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();
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] == 'H'){
continue;
}
if(dist[a][b] == -1 && dist[y][x] + 1 <= S + add && time < fromhive[a][b]){
dist[a][b] = dist[y][x] + 1;
if(dist[a][b] == S + add){
for(ll ind = 0;ind < 4;ind++){
ll aa = a + Hy[ind];
ll bb = b + Wx[ind];
if(aa < 0 || aa >= N || bb < 0 || bb >= N || arr[aa][bb] == 'T' || arr[aa][bb] == 'H'){
continue;
}
if(dist[aa][bb] == -1 && time + 1 < fromhive[aa][bb]){
newq.push({a,b});
break;
}
}
}else{
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 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
68 ms |
11228 KB |
Output is correct |
8 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
9 |
Correct |
1 ms |
508 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
464 KB |
Output isn't correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
24 |
Correct |
1 ms |
336 KB |
Output is correct |
25 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
26 |
Correct |
1 ms |
504 KB |
Output is correct |
27 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
28 |
Correct |
1 ms |
504 KB |
Output is correct |
29 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Incorrect |
7 ms |
2384 KB |
Output isn't correct |
34 |
Correct |
6 ms |
2384 KB |
Output is correct |
35 |
Correct |
16 ms |
2384 KB |
Output is correct |
36 |
Incorrect |
7 ms |
2896 KB |
Output isn't correct |
37 |
Correct |
7 ms |
3064 KB |
Output is correct |
38 |
Correct |
21 ms |
2896 KB |
Output is correct |
39 |
Incorrect |
9 ms |
3664 KB |
Output isn't correct |
40 |
Correct |
8 ms |
3664 KB |
Output is correct |
41 |
Correct |
25 ms |
3664 KB |
Output is correct |
42 |
Incorrect |
13 ms |
4448 KB |
Output isn't correct |
43 |
Correct |
12 ms |
4600 KB |
Output is correct |
44 |
Correct |
46 ms |
4612 KB |
Output is correct |
45 |
Incorrect |
14 ms |
5456 KB |
Output isn't correct |
46 |
Correct |
13 ms |
5456 KB |
Output is correct |
47 |
Correct |
45 ms |
5456 KB |
Output is correct |
48 |
Incorrect |
17 ms |
6224 KB |
Output isn't correct |
49 |
Correct |
15 ms |
6236 KB |
Output is correct |
50 |
Correct |
53 ms |
6224 KB |
Output is correct |
51 |
Incorrect |
18 ms |
7248 KB |
Output isn't correct |
52 |
Correct |
18 ms |
7760 KB |
Output is correct |
53 |
Correct |
67 ms |
7248 KB |
Output is correct |
54 |
Incorrect |
25 ms |
9040 KB |
Output isn't correct |
55 |
Correct |
20 ms |
8528 KB |
Output is correct |
56 |
Correct |
70 ms |
8528 KB |
Output is correct |
57 |
Incorrect |
23 ms |
10064 KB |
Output isn't correct |
58 |
Correct |
21 ms |
9592 KB |
Output is correct |
59 |
Correct |
79 ms |
9808 KB |
Output is correct |
60 |
Incorrect |
28 ms |
11088 KB |
Output isn't correct |
61 |
Correct |
28 ms |
11088 KB |
Output is correct |
62 |
Correct |
98 ms |
11600 KB |
Output is correct |
63 |
Correct |
95 ms |
11080 KB |
Output is correct |
64 |
Correct |
131 ms |
11072 KB |
Output is correct |
65 |
Correct |
132 ms |
11080 KB |
Output is correct |
66 |
Incorrect |
126 ms |
10948 KB |
Output isn't correct |
67 |
Correct |
94 ms |
10944 KB |
Output is correct |
68 |
Correct |
49 ms |
11528 KB |
Output is correct |
69 |
Correct |
48 ms |
11080 KB |
Output is correct |
70 |
Correct |
41 ms |
11016 KB |
Output is correct |
71 |
Correct |
38 ms |
11592 KB |
Output is correct |
72 |
Incorrect |
37 ms |
11008 KB |
Output isn't correct |
73 |
Incorrect |
52 ms |
11592 KB |
Output isn't correct |
74 |
Correct |
57 ms |
11524 KB |
Output is correct |
75 |
Correct |
60 ms |
11592 KB |
Output is correct |
76 |
Correct |
58 ms |
12020 KB |
Output is correct |
77 |
Correct |
59 ms |
11548 KB |
Output is correct |
78 |
Correct |
61 ms |
11592 KB |
Output is correct |
79 |
Correct |
49 ms |
11344 KB |
Output is correct |
80 |
Correct |
61 ms |
11452 KB |
Output is correct |
81 |
Correct |
62 ms |
11600 KB |
Output is correct |
82 |
Correct |
47 ms |
11600 KB |
Output is correct |
83 |
Correct |
65 ms |
11344 KB |
Output is correct |
84 |
Correct |
59 ms |
11340 KB |
Output is correct |
85 |
Correct |
76 ms |
11592 KB |
Output is correct |
86 |
Correct |
70 ms |
11372 KB |
Output is correct |
87 |
Correct |
73 ms |
12208 KB |
Output is correct |
88 |
Correct |
66 ms |
11848 KB |
Output is correct |
89 |
Correct |
68 ms |
11344 KB |
Output is correct |
90 |
Correct |
71 ms |
11344 KB |
Output is correct |
91 |
Correct |
67 ms |
11856 KB |
Output is correct |
92 |
Correct |
78 ms |
11336 KB |
Output is correct |