#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];
memset(fromhive,-1,sizeof(fromhive));
for(ll i = 0;i < N;i++){
for(ll j = 0;j < N;j++){
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] == 'H'){
continue;
}
if(fromhive[a][b] == -1){
q.push({a,b});
fromhive[a][b] = fromhive[y][x] + 1;
}
}
}
/*cout<<"FROMHIVE: "<<'\n';
for(ll i = 0;i < N;i++){
for(ll j = 0;j < N;j++){
cout<<fromhive[i][j]<<" ";
}
cout<<'\n';
}*/
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);
//cout<<"MID: "<<mid<<'\n';
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){
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});
}
}
}
}
//cout<<"CURTIME: "<<time<<'\n';
/*if(mid == 2){
cout<<"DIST: "<<'\n';
for(ll ii = 0;ii < N;ii++){
for(ll jj = 0;jj < N;jj++){
cout<<dist[ii][jj]<<" ";
}
cout<<'\n';
}
cout<<'\n';
}*/
time++;
add += S;
while(!newq.empty()){
q.push(newq.front());
//if(mid == 2) cout<<"NEWQ: "<<newq.front().first<<" "<<newq.front().second<<'\n';
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 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
7 |
Incorrect |
30 ms |
11948 KB |
Output isn't correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
336 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 |
508 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 |
336 KB |
Output is correct |
27 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
28 |
Correct |
1 ms |
336 KB |
Output is correct |
29 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
30 |
Correct |
1 ms |
504 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 |
6 ms |
2384 KB |
Output isn't correct |
34 |
Correct |
6 ms |
2384 KB |
Output is correct |
35 |
Incorrect |
12 ms |
2384 KB |
Output isn't correct |
36 |
Incorrect |
8 ms |
3152 KB |
Output isn't correct |
37 |
Correct |
8 ms |
3152 KB |
Output is correct |
38 |
Incorrect |
16 ms |
3152 KB |
Output isn't correct |
39 |
Incorrect |
9 ms |
3868 KB |
Output isn't correct |
40 |
Correct |
8 ms |
3920 KB |
Output is correct |
41 |
Incorrect |
22 ms |
3920 KB |
Output isn't correct |
42 |
Incorrect |
9 ms |
4700 KB |
Output isn't correct |
43 |
Correct |
10 ms |
4688 KB |
Output is correct |
44 |
Incorrect |
25 ms |
4872 KB |
Output isn't correct |
45 |
Incorrect |
13 ms |
5712 KB |
Output isn't correct |
46 |
Correct |
12 ms |
5712 KB |
Output is correct |
47 |
Incorrect |
33 ms |
5712 KB |
Output isn't correct |
48 |
Incorrect |
15 ms |
6548 KB |
Output isn't correct |
49 |
Correct |
16 ms |
6736 KB |
Output is correct |
50 |
Incorrect |
38 ms |
6736 KB |
Output isn't correct |
51 |
Incorrect |
17 ms |
7760 KB |
Output isn't correct |
52 |
Correct |
17 ms |
7760 KB |
Output is correct |
53 |
Incorrect |
43 ms |
7760 KB |
Output isn't correct |
54 |
Incorrect |
19 ms |
9040 KB |
Output isn't correct |
55 |
Correct |
19 ms |
9040 KB |
Output is correct |
56 |
Incorrect |
49 ms |
8844 KB |
Output isn't correct |
57 |
Incorrect |
22 ms |
10320 KB |
Output isn't correct |
58 |
Correct |
22 ms |
10312 KB |
Output is correct |
59 |
Incorrect |
69 ms |
10320 KB |
Output isn't correct |
60 |
Incorrect |
25 ms |
11684 KB |
Output isn't correct |
61 |
Correct |
23 ms |
11600 KB |
Output is correct |
62 |
Incorrect |
68 ms |
11596 KB |
Output isn't correct |
63 |
Incorrect |
31 ms |
11592 KB |
Output isn't correct |
64 |
Incorrect |
68 ms |
11592 KB |
Output isn't correct |
65 |
Incorrect |
33 ms |
11592 KB |
Output isn't correct |
66 |
Incorrect |
29 ms |
11600 KB |
Output isn't correct |
67 |
Correct |
34 ms |
11680 KB |
Output is correct |
68 |
Incorrect |
33 ms |
11592 KB |
Output isn't correct |
69 |
Incorrect |
36 ms |
11684 KB |
Output isn't correct |
70 |
Incorrect |
32 ms |
11600 KB |
Output isn't correct |
71 |
Incorrect |
32 ms |
11592 KB |
Output isn't correct |
72 |
Incorrect |
36 ms |
11592 KB |
Output isn't correct |
73 |
Incorrect |
32 ms |
12112 KB |
Output isn't correct |
74 |
Correct |
52 ms |
12112 KB |
Output is correct |
75 |
Incorrect |
33 ms |
12104 KB |
Output isn't correct |
76 |
Incorrect |
31 ms |
12200 KB |
Output isn't correct |
77 |
Incorrect |
32 ms |
12052 KB |
Output isn't correct |
78 |
Correct |
29 ms |
12104 KB |
Output is correct |
79 |
Correct |
48 ms |
12104 KB |
Output is correct |
80 |
Incorrect |
29 ms |
12112 KB |
Output isn't correct |
81 |
Incorrect |
30 ms |
12104 KB |
Output isn't correct |
82 |
Incorrect |
29 ms |
12084 KB |
Output isn't correct |
83 |
Incorrect |
31 ms |
12104 KB |
Output isn't correct |
84 |
Correct |
56 ms |
11996 KB |
Output is correct |
85 |
Incorrect |
31 ms |
12008 KB |
Output isn't correct |
86 |
Incorrect |
33 ms |
12108 KB |
Output isn't correct |
87 |
Incorrect |
32 ms |
12112 KB |
Output isn't correct |
88 |
Incorrect |
30 ms |
11856 KB |
Output isn't correct |
89 |
Correct |
65 ms |
11904 KB |
Output is correct |
90 |
Incorrect |
30 ms |
11848 KB |
Output isn't correct |
91 |
Incorrect |
33 ms |
11848 KB |
Output isn't correct |
92 |
Incorrect |
32 ms |
11944 KB |
Output isn't correct |