#include "bits/stdc++.h"
using namespace std;
const int maxn = 805;
char s[maxn][maxn];
int n, S;
int vis[maxn][maxn], dis[maxn][maxn], dis1[maxn][maxn];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int sx, sy, fx, fy;
void precalc(){
queue<pair<int, int>> q;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
dis[i][j] = n * n;
vis[i][j] = 0;
if(s[i][j] == 'H'){
vis[i][j] = 1;
q.push({i, j});
dis[i][j] = 0;
}
}
}
while(!q.empty()){
pair<int, int> src = q.front();
q.pop();
for(int i = 0; i < 4; ++i){
int newx = src.first + dx[i], newy = src.second + dy[i];
if(newx <= n && newx > 0 && newy <= n && newy > 0){
if(vis[newx][newy] == 0 && s[newx][newy] != 'T'){
vis[newx][newy] = 1;
dis[newx][newy] = min(dis[newx][newy], dis[src.first][src.second] + 1);
q.push({newx, newy});
}
}
}
}
}
bool can(int x){
queue<pair<int, int>> q;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
dis1[i][j] = n * n;
if(s[i][j] == 'D'){
vis[i][j] = 0;
}else if(s[i][j] == 'T')vis[i][j] = 1;
else if(s[i][j] == 'H'){
vis[i][j] = 1;
}else if(s[i][j] == 'M'){
vis[i][j] = 1;
dis1[i][j] = 0;
q.push({i, j});
}else {
vis[i][j] = 0;
}
}
}
while(!q.empty()){
pair<int, int> src = q.front();
q.pop();
for(int i = 0; i < 4; ++i){
int newx = src.first + dx[i], newy = src.second + dy[i];
if(newx <= n && newx > 0 && newy <= n && newy > 0){
if(s[newx][newy] != 'T' && !vis[newx][newy]){
int cost = (dis1[src.first][src.second] + 1) / S + x;
if(cost < dis[newx][newy]){
vis[newx][newy] = 1;
dis1[newx][newy] = dis1[src.first][src.second] + 1;
q.push({newx, newy});
}
}
}
}
}
if(vis[fx][fy])return 1;
return 0;
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> S;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
cin >> s[i][j];
if(s[i][j] == 'M'){
sx = i, sy = j;
}else if(s[i][j] == 'D'){
fx = i, fy = j;
}
}
}
precalc();
int lo = 0, hi = n * n, ans = -1;
while(lo <= hi){
int mid = (lo + hi) >> 1;
if(can(mid)){
ans = mid;
lo = mid + 1;
}else {
hi = mid - 1;
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
7 |
Incorrect |
127 ms |
8684 KB |
Output isn't correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Incorrect |
1 ms |
1004 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
876 KB |
Output isn't correct |
14 |
Correct |
2 ms |
1004 KB |
Output is correct |
15 |
Correct |
1 ms |
620 KB |
Output is correct |
16 |
Correct |
1 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
620 KB |
Output is correct |
18 |
Correct |
1 ms |
620 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
1 ms |
620 KB |
Output is correct |
21 |
Correct |
1 ms |
748 KB |
Output is correct |
22 |
Correct |
1 ms |
748 KB |
Output is correct |
23 |
Correct |
1 ms |
876 KB |
Output is correct |
24 |
Correct |
2 ms |
876 KB |
Output is correct |
25 |
Correct |
1 ms |
1004 KB |
Output is correct |
26 |
Correct |
2 ms |
1004 KB |
Output is correct |
27 |
Correct |
2 ms |
1004 KB |
Output is correct |
28 |
Correct |
2 ms |
1004 KB |
Output is correct |
29 |
Correct |
2 ms |
1132 KB |
Output is correct |
30 |
Correct |
2 ms |
1132 KB |
Output is correct |
31 |
Correct |
2 ms |
1132 KB |
Output is correct |
32 |
Correct |
2 ms |
1132 KB |
Output is correct |
33 |
Correct |
10 ms |
3948 KB |
Output is correct |
34 |
Correct |
13 ms |
4084 KB |
Output is correct |
35 |
Correct |
31 ms |
3948 KB |
Output is correct |
36 |
Correct |
12 ms |
4480 KB |
Output is correct |
37 |
Correct |
13 ms |
4460 KB |
Output is correct |
38 |
Correct |
41 ms |
4460 KB |
Output is correct |
39 |
Correct |
15 ms |
4972 KB |
Output is correct |
40 |
Correct |
16 ms |
4972 KB |
Output is correct |
41 |
Correct |
54 ms |
5100 KB |
Output is correct |
42 |
Correct |
18 ms |
5484 KB |
Output is correct |
43 |
Correct |
27 ms |
5484 KB |
Output is correct |
44 |
Correct |
89 ms |
5484 KB |
Output is correct |
45 |
Correct |
22 ms |
5996 KB |
Output is correct |
46 |
Correct |
23 ms |
5996 KB |
Output is correct |
47 |
Correct |
76 ms |
5996 KB |
Output is correct |
48 |
Correct |
25 ms |
6508 KB |
Output is correct |
49 |
Correct |
27 ms |
6508 KB |
Output is correct |
50 |
Correct |
96 ms |
6508 KB |
Output is correct |
51 |
Correct |
30 ms |
7020 KB |
Output is correct |
52 |
Correct |
32 ms |
7020 KB |
Output is correct |
53 |
Correct |
115 ms |
7020 KB |
Output is correct |
54 |
Correct |
34 ms |
7532 KB |
Output is correct |
55 |
Correct |
36 ms |
7532 KB |
Output is correct |
56 |
Correct |
137 ms |
7532 KB |
Output is correct |
57 |
Correct |
38 ms |
8044 KB |
Output is correct |
58 |
Correct |
41 ms |
8044 KB |
Output is correct |
59 |
Correct |
155 ms |
8172 KB |
Output is correct |
60 |
Correct |
43 ms |
8556 KB |
Output is correct |
61 |
Correct |
46 ms |
8556 KB |
Output is correct |
62 |
Correct |
177 ms |
8616 KB |
Output is correct |
63 |
Correct |
184 ms |
8684 KB |
Output is correct |
64 |
Correct |
297 ms |
8684 KB |
Output is correct |
65 |
Correct |
242 ms |
8684 KB |
Output is correct |
66 |
Correct |
211 ms |
8684 KB |
Output is correct |
67 |
Correct |
198 ms |
8684 KB |
Output is correct |
68 |
Correct |
164 ms |
8684 KB |
Output is correct |
69 |
Correct |
136 ms |
8556 KB |
Output is correct |
70 |
Correct |
112 ms |
8684 KB |
Output is correct |
71 |
Correct |
121 ms |
8556 KB |
Output is correct |
72 |
Incorrect |
108 ms |
8684 KB |
Output isn't correct |
73 |
Incorrect |
75 ms |
8812 KB |
Output isn't correct |
74 |
Correct |
102 ms |
8940 KB |
Output is correct |
75 |
Correct |
113 ms |
8812 KB |
Output is correct |
76 |
Correct |
109 ms |
8940 KB |
Output is correct |
77 |
Correct |
114 ms |
8812 KB |
Output is correct |
78 |
Correct |
146 ms |
8940 KB |
Output is correct |
79 |
Correct |
84 ms |
8812 KB |
Output is correct |
80 |
Correct |
96 ms |
8812 KB |
Output is correct |
81 |
Correct |
107 ms |
8812 KB |
Output is correct |
82 |
Correct |
104 ms |
8940 KB |
Output is correct |
83 |
Correct |
115 ms |
8812 KB |
Output is correct |
84 |
Correct |
109 ms |
8812 KB |
Output is correct |
85 |
Correct |
115 ms |
8824 KB |
Output is correct |
86 |
Correct |
114 ms |
8940 KB |
Output is correct |
87 |
Correct |
116 ms |
8832 KB |
Output is correct |
88 |
Correct |
121 ms |
8812 KB |
Output is correct |
89 |
Correct |
120 ms |
8812 KB |
Output is correct |
90 |
Correct |
122 ms |
8684 KB |
Output is correct |
91 |
Correct |
127 ms |
8684 KB |
Output is correct |
92 |
Correct |
130 ms |
8860 KB |
Output is correct |