#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl;
typedef pair<int,int>pii;
typedef pair<pii,pii>pi2;
void solve(){
int n,k;
cin >> n >> k;
string arr[n];
for(int x=0;x<n;x++){
cin >> arr[x];
}
int l=0;
int r=n*n;
int best=-1;
int mid;
pii start;
pii done;
int dist[n+5][n+5];
memset(dist,-1,sizeof(dist));
queue<pii>q;
for(int x=0;x<n;x++){
for(int y=0;y<n;y++){
if(arr[x][y]=='H'){
q.push({x,y});
dist[x][y]=0;
}
else if(arr[x][y]=='M') start={x,y};
else if(arr[x][y]=='D') done={x,y};
}
}
pii dir[4]={
{0,1},
{0,-1},
{1,0},
{-1,0},
};
while(!q.empty()){
pii cur=q.front();
q.pop();
for(auto it:dir){
int newx=cur.first+it.first;
int newy=cur.second+it.second;
if(newx<0||newy<0||newx>=n||newy>=n) continue;
if(dist[newx][newy]!=-1) continue;
if(arr[newx][newy]=='T') continue;
dist[newx][newy]=dist[cur.first][cur.second]+1;
q.push({newx,newy});
}
}
while(l<=r){
mid=(l+r)/2;
int dist2[n][n];
memset(dist2,-1,sizeof(dist2));
dist2[start.first][start.second]=k*mid;
q.push({start.first,start.second});
bool visited[n+5][n+5];
memset(visited,0,sizeof(visited));
for(int x=0;x<n;x++){
vector<pii>storage;
int target=(x+1+mid)*k;
while(!q.empty()){
pii cur=q.front();
q.pop();
if((dist2[cur.first][cur.second]+k-1)/k==x+mid+1&&x+mid+1<dist[cur.first][cur.second]){
storage.push_back({cur.first,cur.second});
visited[cur.first][cur.second]=1;
}
if(dist2[cur.first][cur.second]>=target) continue;
for(auto it:dir){
int newx=cur.first+it.first;
int newy=cur.second+it.second;
if(newx<0||newy<0||newx>=n||newy>=n) continue;
if(dist2[newx][newy]!=-1) continue;
if(arr[newx][newy]=='T') continue;
dist2[newx][newy]=dist2[cur.first][cur.second]+1;
q.push({newx,newy});
}
}
for(auto it:storage) q.push(it);
}
if(visited[done.first][done.second]){
best=mid;
l=mid+1;
}
else r=mid-1;
}
cout << best;
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
//freopen("out.txt", "w", stdout);
int t=1;
//cin >> t;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
118 ms |
12816 KB |
Output isn't correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
10 ms |
2652 KB |
Output is correct |
34 |
Correct |
6 ms |
2652 KB |
Output is correct |
35 |
Incorrect |
33 ms |
4140 KB |
Output isn't correct |
36 |
Correct |
11 ms |
3160 KB |
Output is correct |
37 |
Correct |
7 ms |
3160 KB |
Output is correct |
38 |
Incorrect |
42 ms |
5208 KB |
Output isn't correct |
39 |
Correct |
16 ms |
3932 KB |
Output is correct |
40 |
Correct |
9 ms |
3932 KB |
Output is correct |
41 |
Incorrect |
55 ms |
6304 KB |
Output isn't correct |
42 |
Correct |
22 ms |
4696 KB |
Output is correct |
43 |
Correct |
10 ms |
4956 KB |
Output is correct |
44 |
Incorrect |
73 ms |
7772 KB |
Output isn't correct |
45 |
Correct |
26 ms |
6080 KB |
Output is correct |
46 |
Correct |
13 ms |
5724 KB |
Output is correct |
47 |
Incorrect |
89 ms |
9748 KB |
Output isn't correct |
48 |
Correct |
28 ms |
6748 KB |
Output is correct |
49 |
Correct |
15 ms |
6956 KB |
Output is correct |
50 |
Incorrect |
107 ms |
10964 KB |
Output isn't correct |
51 |
Correct |
33 ms |
7772 KB |
Output is correct |
52 |
Correct |
18 ms |
8028 KB |
Output is correct |
53 |
Incorrect |
130 ms |
13312 KB |
Output isn't correct |
54 |
Correct |
47 ms |
9052 KB |
Output is correct |
55 |
Correct |
20 ms |
9048 KB |
Output is correct |
56 |
Incorrect |
159 ms |
14100 KB |
Output isn't correct |
57 |
Correct |
50 ms |
10332 KB |
Output is correct |
58 |
Correct |
28 ms |
10588 KB |
Output is correct |
59 |
Incorrect |
197 ms |
19144 KB |
Output isn't correct |
60 |
Correct |
43 ms |
11608 KB |
Output is correct |
61 |
Correct |
31 ms |
11868 KB |
Output is correct |
62 |
Incorrect |
255 ms |
21012 KB |
Output isn't correct |
63 |
Incorrect |
159 ms |
11864 KB |
Output isn't correct |
64 |
Correct |
314 ms |
20456 KB |
Output is correct |
65 |
Incorrect |
275 ms |
11936 KB |
Output isn't correct |
66 |
Incorrect |
162 ms |
11856 KB |
Output isn't correct |
67 |
Incorrect |
176 ms |
11868 KB |
Output isn't correct |
68 |
Incorrect |
53 ms |
11864 KB |
Output isn't correct |
69 |
Correct |
86 ms |
13720 KB |
Output is correct |
70 |
Incorrect |
39 ms |
11860 KB |
Output isn't correct |
71 |
Incorrect |
33 ms |
11908 KB |
Output isn't correct |
72 |
Incorrect |
32 ms |
11952 KB |
Output isn't correct |
73 |
Incorrect |
49 ms |
12368 KB |
Output isn't correct |
74 |
Correct |
134 ms |
16088 KB |
Output is correct |
75 |
Correct |
80 ms |
12376 KB |
Output is correct |
76 |
Correct |
67 ms |
12380 KB |
Output is correct |
77 |
Correct |
69 ms |
12376 KB |
Output is correct |
78 |
Correct |
87 ms |
12124 KB |
Output is correct |
79 |
Correct |
144 ms |
16172 KB |
Output is correct |
80 |
Correct |
67 ms |
12120 KB |
Output is correct |
81 |
Correct |
74 ms |
12336 KB |
Output is correct |
82 |
Correct |
74 ms |
12404 KB |
Output is correct |
83 |
Correct |
89 ms |
12268 KB |
Output is correct |
84 |
Correct |
171 ms |
16888 KB |
Output is correct |
85 |
Correct |
105 ms |
12476 KB |
Output is correct |
86 |
Correct |
86 ms |
12304 KB |
Output is correct |
87 |
Correct |
100 ms |
12860 KB |
Output is correct |
88 |
Correct |
104 ms |
12560 KB |
Output is correct |
89 |
Correct |
208 ms |
20408 KB |
Output is correct |
90 |
Correct |
133 ms |
12836 KB |
Output is correct |
91 |
Correct |
106 ms |
13704 KB |
Output is correct |
92 |
Correct |
108 ms |
12420 KB |
Output is correct |