#include <bits/stdc++.h>
#define pii pair<int,int>
#define iii pair<int,pii>
#define fi first
#define se second
#define vi vector<int>
#define vii vector<pii>
#define pb(i) push_back(i)
#define all(x) x.begin(),x.end()
#define TEXT "a"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int inf = 1e9 + 7;
const ld eps = 1e-8;
const double pi = acos(-1);
const int N = 805;
int n,s;
int f[N][N], g[N][N];
char a[N][N];
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
bool check(int val){
queue<pii> q;
memset(f,-1,sizeof(f));
memset(g,-1,sizeof(g));
pii M, D;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
if (a[i][j] == 'M') M = pii(i,j);
if (a[i][j] == 'D') D = pii(i,j);
if (a[i][j] == 'H'){
q.push(pii(i,j));
f[i][j] = 0;
}
}
}
while(!q.empty()){
pii u = q.front();
q.pop();
if (f[u.fi][u.se] >= val) continue;
for (int i = 0; i < 4; i++){
int x = u.fi + dx[i];
int y = u.se + dy[i];
if (x == 0 || x > n || y == 0 || y > n) continue;
if (a[x][y] == 'T' || a[x][y] == 'D') continue;
if (f[x][y] != -1) continue;
f[x][y] = f[u.fi][u.se] + 1;
q.push(pii(x,y));
}
}
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
if (f[i][j] == -1) continue;
f[i][j] = 0;
q.push(pii(i,j));
}
}
while(!q.empty()){
pii u = q.front();
q.pop();
for (int i = 0; i < 4; i++){
int x = u.fi + dx[i];
int y = u.se + dy[i];
if (x == 0 || x > n || y == 0 || y > n) continue;
if (a[x][y] == 'T' || a[x][y] == 'D') continue;
if (f[x][y] != -1) continue;
f[x][y] = f[u.fi][u.se] + s;
q.push(pii(x,y));
}
}
f[D.fi][D.se] = inf;
g[M.fi][M.se] = 0;
q.push(M);
while(!q.empty()){
pii u = q.front();
q.pop();
for (int i = 0; i < 4; i++){
int x = u.fi + dx[i];
int y = u.se + dy[i];
if (x == 0 || x > n || y == 0 || y > n) continue;
if (a[x][y] == 'T') continue;
if (g[x][y] != -1) continue;
if (f[x][y] < g[u.fi][u.se]) continue;
g[x][y] = g[u.fi][u.se] + 1;
q.push(pii(x,y));
}
}
return g[D.fi][D.se] != -1;
}
int main(){
cin.tie(0) -> sync_with_stdio(0);
if (fopen(TEXT".inp","r")){
freopen(TEXT".inp","r",stdin);
freopen(TEXT".out","w",stdout);
}
cin >> n >> s;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++) cin >> a[i][j];
}
int res = -1;
int l = 0;
int r = inf;
// cout << check(1) << "\n";
// for (int i = 1; i <= n; i++){
// for (int j = 1; j <= n; j++){
// cout << g[i][j] << " ";
// }
// cout << "\n";
// }
while(l <= r){
int mid = (l + r) / 2;
if (check(mid)){
res = mid;
l = mid + 1;
}
else r = mid - 1;
}
cout << res << "\n";
return 0;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen(TEXT".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen(TEXT".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
5980 KB |
Output isn't correct |
2 |
Incorrect |
6 ms |
5984 KB |
Output isn't correct |
3 |
Incorrect |
4 ms |
5992 KB |
Output isn't correct |
4 |
Incorrect |
4 ms |
5980 KB |
Output isn't correct |
5 |
Incorrect |
5 ms |
5980 KB |
Output isn't correct |
6 |
Correct |
4 ms |
5980 KB |
Output is correct |
7 |
Correct |
769 ms |
11388 KB |
Output is correct |
8 |
Incorrect |
6 ms |
5468 KB |
Output isn't correct |
9 |
Correct |
5 ms |
5548 KB |
Output is correct |
10 |
Incorrect |
5 ms |
5468 KB |
Output isn't correct |
11 |
Incorrect |
6 ms |
5468 KB |
Output isn't correct |
12 |
Incorrect |
8 ms |
6060 KB |
Output isn't correct |
13 |
Incorrect |
6 ms |
5980 KB |
Output isn't correct |
14 |
Correct |
7 ms |
6068 KB |
Output is correct |
15 |
Incorrect |
5 ms |
5980 KB |
Output isn't correct |
16 |
Correct |
5 ms |
5468 KB |
Output is correct |
17 |
Incorrect |
6 ms |
6188 KB |
Output isn't correct |
18 |
Correct |
5 ms |
5980 KB |
Output is correct |
19 |
Incorrect |
6 ms |
5468 KB |
Output isn't correct |
20 |
Correct |
6 ms |
5464 KB |
Output is correct |
21 |
Incorrect |
6 ms |
5468 KB |
Output isn't correct |
22 |
Correct |
8 ms |
5468 KB |
Output is correct |
23 |
Incorrect |
8 ms |
5468 KB |
Output isn't correct |
24 |
Correct |
7 ms |
5468 KB |
Output is correct |
25 |
Incorrect |
8 ms |
5604 KB |
Output isn't correct |
26 |
Correct |
10 ms |
5464 KB |
Output is correct |
27 |
Incorrect |
7 ms |
5608 KB |
Output isn't correct |
28 |
Correct |
6 ms |
5976 KB |
Output is correct |
29 |
Incorrect |
8 ms |
5980 KB |
Output isn't correct |
30 |
Correct |
9 ms |
5468 KB |
Output is correct |
31 |
Incorrect |
11 ms |
5468 KB |
Output isn't correct |
32 |
Correct |
12 ms |
5468 KB |
Output is correct |
33 |
Incorrect |
69 ms |
6332 KB |
Output isn't correct |
34 |
Correct |
68 ms |
6316 KB |
Output is correct |
35 |
Correct |
98 ms |
6924 KB |
Output is correct |
36 |
Incorrect |
89 ms |
6844 KB |
Output isn't correct |
37 |
Correct |
89 ms |
6840 KB |
Output is correct |
38 |
Correct |
122 ms |
7128 KB |
Output is correct |
39 |
Incorrect |
111 ms |
6716 KB |
Output isn't correct |
40 |
Correct |
109 ms |
6720 KB |
Output is correct |
41 |
Correct |
182 ms |
7408 KB |
Output is correct |
42 |
Incorrect |
137 ms |
7196 KB |
Output isn't correct |
43 |
Correct |
183 ms |
7168 KB |
Output is correct |
44 |
Correct |
197 ms |
7696 KB |
Output is correct |
45 |
Incorrect |
166 ms |
7428 KB |
Output isn't correct |
46 |
Correct |
158 ms |
7348 KB |
Output is correct |
47 |
Correct |
224 ms |
8024 KB |
Output is correct |
48 |
Incorrect |
230 ms |
7648 KB |
Output isn't correct |
49 |
Correct |
216 ms |
7608 KB |
Output is correct |
50 |
Correct |
292 ms |
8352 KB |
Output is correct |
51 |
Incorrect |
232 ms |
7904 KB |
Output isn't correct |
52 |
Correct |
225 ms |
7884 KB |
Output is correct |
53 |
Correct |
328 ms |
8736 KB |
Output is correct |
54 |
Incorrect |
280 ms |
8168 KB |
Output isn't correct |
55 |
Correct |
246 ms |
8140 KB |
Output is correct |
56 |
Correct |
392 ms |
9100 KB |
Output is correct |
57 |
Incorrect |
320 ms |
8420 KB |
Output isn't correct |
58 |
Correct |
307 ms |
8536 KB |
Output is correct |
59 |
Correct |
412 ms |
9568 KB |
Output is correct |
60 |
Incorrect |
360 ms |
8768 KB |
Output isn't correct |
61 |
Correct |
345 ms |
8792 KB |
Output is correct |
62 |
Correct |
475 ms |
10080 KB |
Output is correct |
63 |
Incorrect |
680 ms |
9276 KB |
Output isn't correct |
64 |
Correct |
735 ms |
9280 KB |
Output is correct |
65 |
Incorrect |
672 ms |
9268 KB |
Output isn't correct |
66 |
Incorrect |
717 ms |
9308 KB |
Output isn't correct |
67 |
Correct |
688 ms |
9264 KB |
Output is correct |
68 |
Correct |
759 ms |
9344 KB |
Output is correct |
69 |
Correct |
759 ms |
9264 KB |
Output is correct |
70 |
Correct |
727 ms |
9268 KB |
Output is correct |
71 |
Correct |
744 ms |
9256 KB |
Output is correct |
72 |
Incorrect |
806 ms |
9284 KB |
Output isn't correct |
73 |
Incorrect |
704 ms |
11356 KB |
Output isn't correct |
74 |
Correct |
772 ms |
11344 KB |
Output is correct |
75 |
Correct |
737 ms |
11348 KB |
Output is correct |
76 |
Correct |
784 ms |
11376 KB |
Output is correct |
77 |
Correct |
785 ms |
11340 KB |
Output is correct |
78 |
Correct |
696 ms |
11400 KB |
Output is correct |
79 |
Correct |
691 ms |
11352 KB |
Output is correct |
80 |
Correct |
696 ms |
11356 KB |
Output is correct |
81 |
Correct |
698 ms |
11348 KB |
Output is correct |
82 |
Correct |
736 ms |
11336 KB |
Output is correct |
83 |
Correct |
657 ms |
11124 KB |
Output is correct |
84 |
Correct |
698 ms |
11360 KB |
Output is correct |
85 |
Correct |
667 ms |
11544 KB |
Output is correct |
86 |
Correct |
713 ms |
11336 KB |
Output is correct |
87 |
Correct |
678 ms |
11332 KB |
Output is correct |
88 |
Correct |
628 ms |
11604 KB |
Output is correct |
89 |
Correct |
711 ms |
11436 KB |
Output is correct |
90 |
Correct |
700 ms |
11344 KB |
Output is correct |
91 |
Correct |
661 ms |
11596 KB |
Output is correct |
92 |
Correct |
652 ms |
11356 KB |
Output is correct |