#include <bits/stdc++.h>
#define int long long
#define f first
#define s second
using namespace std;
const int MAXN = 810;
char grid[MAXN][MAXN];
int dr[4] = {0,0,1,-1}, dc[4] = {1,-1,0,0},N;
bool goodbees(int r, int c){
return r >= 0 && c >= 0 && r <= N-1 && c <= N-1 && grid[r][c] != 'T' && grid[r][c] != 'D';
}
bool goodmecho(int r, int c){
return r >= 0 && c >= 0 && r <= N-1 && c <= N-1 && grid[r][c] != 'T';
}
const int INF = 1e18;
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int S; cin >> N >> S;
queue<pair<int,int>> q;
vector<vector<int>> dist(N,vector<int>(N,INF));
pair<int,int> m,home;
for(int i = 0;i<N;++i){
for(int j = 0;j<N;++j){
cin >> grid[i][j];
if(grid[i][j] == 'H'){
q.push({i,j});
dist[i][j] = 0;
}
else if(grid[i][j] == 'M'){
m.f = i, m.s = j;
}
else if(grid[i][j] == 'D'){
home.f = i, home.s = j;
}
}
}
while(!q.empty()){
int r = q.front().f, c = q.front().s; q.pop();
for(int i = 0;i<4;++i){
int nr = r+dr[i], nc = c+dc[i];
if(goodbees(nr,nc)){
if(dist[nr][nc] > dist[r][c]+1){
dist[nr][nc] = dist[r][c]+1;
q.push({nr,nc});
}
}
}
}
/*vector<vector<int>> mecho(N,vector<int>(N,INF));
mecho[m.f][m.s] = 0; q.push(m);
while(!q.empty()){
int r = q.front().f, c = q.front().s; q.pop();
for(int i = 0;i<4;++i){
int nr = r+dr[i], nc = c+dc[i];
if(goodmecho(nr,nc)){
if(mecho[nr][nc] > mecho[r][c]+1){
mecho[nr][nc] = mecho[r][c]+1;
q.push({nr,nc});
}
}
}
}*/
int lo = 0, hi = INF;
if(hi < 0){
cout << "-1\n";
return 0;
}
vector<vector<int>> D(N,vector<int>(N,INF));
--lo;
while(lo < hi){
int mid = lo+(hi-lo+1)/2; //time
//cout << mid << "\n";
D = vector<vector<int>>(N,vector<int>(N,INF));
q.push(m); D[m.f][m.s] = 0;
while(!q.empty()){
int r = q.front().f, c = q.front().s; q.pop();
//cout << r << " " << c << "\n";
for(int i = 0;i<4;++i){
int nr = r+dr[i], nc = c+dc[i];
if(goodmecho(nr,nc)){
if((D[r][c]+1)/S +mid < dist[nr][nc] && D[r][c]+1 < D[nr][nc]){
D[nr][nc] = D[r][c]+1;
q.push({nr,nc});
}
}
}
}
if(D[home.f][home.s] == INF){
hi = mid-1;
}
else{
lo = mid;
}
}
cout << lo << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
181 ms |
16484 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
600 KB |
Output is correct |
33 |
Correct |
12 ms |
3776 KB |
Output is correct |
34 |
Correct |
12 ms |
3776 KB |
Output is correct |
35 |
Correct |
28 ms |
3664 KB |
Output is correct |
36 |
Correct |
16 ms |
4840 KB |
Output is correct |
37 |
Correct |
16 ms |
4904 KB |
Output is correct |
38 |
Correct |
36 ms |
4588 KB |
Output is correct |
39 |
Correct |
20 ms |
5696 KB |
Output is correct |
40 |
Correct |
20 ms |
5728 KB |
Output is correct |
41 |
Correct |
46 ms |
5632 KB |
Output is correct |
42 |
Correct |
24 ms |
6872 KB |
Output is correct |
43 |
Correct |
24 ms |
6912 KB |
Output is correct |
44 |
Correct |
74 ms |
6864 KB |
Output is correct |
45 |
Correct |
29 ms |
8128 KB |
Output is correct |
46 |
Correct |
29 ms |
8240 KB |
Output is correct |
47 |
Correct |
70 ms |
8256 KB |
Output is correct |
48 |
Correct |
34 ms |
9516 KB |
Output is correct |
49 |
Correct |
34 ms |
9544 KB |
Output is correct |
50 |
Correct |
86 ms |
9532 KB |
Output is correct |
51 |
Correct |
42 ms |
11080 KB |
Output is correct |
52 |
Correct |
41 ms |
11340 KB |
Output is correct |
53 |
Correct |
99 ms |
11040 KB |
Output is correct |
54 |
Correct |
45 ms |
12804 KB |
Output is correct |
55 |
Correct |
45 ms |
12804 KB |
Output is correct |
56 |
Correct |
121 ms |
12752 KB |
Output is correct |
57 |
Correct |
53 ms |
14520 KB |
Output is correct |
58 |
Correct |
53 ms |
14524 KB |
Output is correct |
59 |
Correct |
154 ms |
14468 KB |
Output is correct |
60 |
Correct |
61 ms |
16372 KB |
Output is correct |
61 |
Correct |
60 ms |
16404 KB |
Output is correct |
62 |
Correct |
167 ms |
16456 KB |
Output is correct |
63 |
Correct |
152 ms |
16836 KB |
Output is correct |
64 |
Correct |
191 ms |
16324 KB |
Output is correct |
65 |
Correct |
163 ms |
16416 KB |
Output is correct |
66 |
Correct |
155 ms |
16464 KB |
Output is correct |
67 |
Correct |
140 ms |
16332 KB |
Output is correct |
68 |
Correct |
87 ms |
16536 KB |
Output is correct |
69 |
Correct |
79 ms |
16356 KB |
Output is correct |
70 |
Correct |
79 ms |
16624 KB |
Output is correct |
71 |
Correct |
79 ms |
16444 KB |
Output is correct |
72 |
Incorrect |
74 ms |
16396 KB |
Output isn't correct |
73 |
Incorrect |
76 ms |
16340 KB |
Output isn't correct |
74 |
Correct |
109 ms |
16520 KB |
Output is correct |
75 |
Correct |
120 ms |
16532 KB |
Output is correct |
76 |
Correct |
103 ms |
16380 KB |
Output is correct |
77 |
Correct |
108 ms |
16376 KB |
Output is correct |
78 |
Correct |
123 ms |
16372 KB |
Output is correct |
79 |
Correct |
106 ms |
16684 KB |
Output is correct |
80 |
Correct |
106 ms |
16368 KB |
Output is correct |
81 |
Correct |
127 ms |
16380 KB |
Output is correct |
82 |
Correct |
103 ms |
16448 KB |
Output is correct |
83 |
Correct |
132 ms |
16580 KB |
Output is correct |
84 |
Correct |
114 ms |
16392 KB |
Output is correct |
85 |
Correct |
123 ms |
16476 KB |
Output is correct |
86 |
Correct |
167 ms |
16308 KB |
Output is correct |
87 |
Correct |
112 ms |
16376 KB |
Output is correct |
88 |
Correct |
127 ms |
16420 KB |
Output is correct |
89 |
Correct |
136 ms |
16396 KB |
Output is correct |
90 |
Correct |
129 ms |
16344 KB |
Output is correct |
91 |
Correct |
145 ms |
16356 KB |
Output is correct |
92 |
Correct |
140 ms |
16380 KB |
Output is correct |