#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;
if (f[M.fi][M.se] != -1) return 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] + 1) 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(2) << "\n";
// for (int i = 1; i <= n; i++){
// for (int j = 1; j <= n; j++){
// cout << f[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: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".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(TEXT".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
5980 KB |
Output isn't correct |
2 |
Incorrect |
4 ms |
5980 KB |
Output isn't correct |
3 |
Incorrect |
4 ms |
5980 KB |
Output isn't correct |
4 |
Incorrect |
4 ms |
5980 KB |
Output isn't correct |
5 |
Incorrect |
4 ms |
5980 KB |
Output isn't correct |
6 |
Incorrect |
4 ms |
5980 KB |
Output isn't correct |
7 |
Incorrect |
602 ms |
12024 KB |
Output isn't correct |
8 |
Correct |
5 ms |
5976 KB |
Output is correct |
9 |
Incorrect |
5 ms |
5976 KB |
Output isn't correct |
10 |
Incorrect |
5 ms |
5844 KB |
Output isn't correct |
11 |
Incorrect |
5 ms |
5980 KB |
Output isn't correct |
12 |
Incorrect |
6 ms |
5468 KB |
Output isn't correct |
13 |
Incorrect |
7 ms |
5596 KB |
Output isn't correct |
14 |
Correct |
8 ms |
5980 KB |
Output is correct |
15 |
Incorrect |
5 ms |
5980 KB |
Output isn't correct |
16 |
Incorrect |
5 ms |
5980 KB |
Output isn't correct |
17 |
Incorrect |
5 ms |
5988 KB |
Output isn't correct |
18 |
Incorrect |
5 ms |
5980 KB |
Output isn't correct |
19 |
Incorrect |
5 ms |
5468 KB |
Output isn't correct |
20 |
Incorrect |
6 ms |
5984 KB |
Output isn't correct |
21 |
Incorrect |
5 ms |
5980 KB |
Output isn't correct |
22 |
Incorrect |
7 ms |
5980 KB |
Output isn't correct |
23 |
Incorrect |
5 ms |
5984 KB |
Output isn't correct |
24 |
Incorrect |
5 ms |
5980 KB |
Output isn't correct |
25 |
Incorrect |
6 ms |
5984 KB |
Output isn't correct |
26 |
Incorrect |
6 ms |
5980 KB |
Output isn't correct |
27 |
Incorrect |
6 ms |
6068 KB |
Output isn't correct |
28 |
Incorrect |
6 ms |
5984 KB |
Output isn't correct |
29 |
Incorrect |
7 ms |
5988 KB |
Output isn't correct |
30 |
Incorrect |
6 ms |
5984 KB |
Output isn't correct |
31 |
Incorrect |
7 ms |
5472 KB |
Output isn't correct |
32 |
Incorrect |
7 ms |
5980 KB |
Output isn't correct |
33 |
Incorrect |
51 ms |
6800 KB |
Output isn't correct |
34 |
Incorrect |
55 ms |
6784 KB |
Output isn't correct |
35 |
Incorrect |
81 ms |
7020 KB |
Output isn't correct |
36 |
Incorrect |
65 ms |
6984 KB |
Output isn't correct |
37 |
Incorrect |
64 ms |
6980 KB |
Output isn't correct |
38 |
Incorrect |
104 ms |
7312 KB |
Output isn't correct |
39 |
Incorrect |
89 ms |
7196 KB |
Output isn't correct |
40 |
Incorrect |
77 ms |
7204 KB |
Output isn't correct |
41 |
Incorrect |
132 ms |
7604 KB |
Output isn't correct |
42 |
Incorrect |
98 ms |
7392 KB |
Output isn't correct |
43 |
Incorrect |
94 ms |
7424 KB |
Output isn't correct |
44 |
Incorrect |
163 ms |
7960 KB |
Output isn't correct |
45 |
Incorrect |
117 ms |
7688 KB |
Output isn't correct |
46 |
Incorrect |
113 ms |
7684 KB |
Output isn't correct |
47 |
Incorrect |
200 ms |
8308 KB |
Output isn't correct |
48 |
Incorrect |
136 ms |
7984 KB |
Output isn't correct |
49 |
Incorrect |
135 ms |
7972 KB |
Output isn't correct |
50 |
Incorrect |
226 ms |
8740 KB |
Output isn't correct |
51 |
Incorrect |
156 ms |
8300 KB |
Output isn't correct |
52 |
Incorrect |
155 ms |
8308 KB |
Output isn't correct |
53 |
Incorrect |
262 ms |
9172 KB |
Output isn't correct |
54 |
Incorrect |
183 ms |
8620 KB |
Output isn't correct |
55 |
Incorrect |
176 ms |
8820 KB |
Output isn't correct |
56 |
Incorrect |
303 ms |
9632 KB |
Output isn't correct |
57 |
Incorrect |
260 ms |
9028 KB |
Output isn't correct |
58 |
Incorrect |
207 ms |
8928 KB |
Output isn't correct |
59 |
Incorrect |
361 ms |
10180 KB |
Output isn't correct |
60 |
Incorrect |
230 ms |
9396 KB |
Output isn't correct |
61 |
Incorrect |
243 ms |
9392 KB |
Output isn't correct |
62 |
Incorrect |
404 ms |
10708 KB |
Output isn't correct |
63 |
Incorrect |
605 ms |
9892 KB |
Output isn't correct |
64 |
Incorrect |
577 ms |
9896 KB |
Output isn't correct |
65 |
Incorrect |
586 ms |
9900 KB |
Output isn't correct |
66 |
Incorrect |
604 ms |
9888 KB |
Output isn't correct |
67 |
Correct |
587 ms |
9896 KB |
Output is correct |
68 |
Incorrect |
708 ms |
9916 KB |
Output isn't correct |
69 |
Incorrect |
707 ms |
9904 KB |
Output isn't correct |
70 |
Incorrect |
705 ms |
9924 KB |
Output isn't correct |
71 |
Incorrect |
688 ms |
9904 KB |
Output isn't correct |
72 |
Incorrect |
683 ms |
9900 KB |
Output isn't correct |
73 |
Incorrect |
648 ms |
11908 KB |
Output isn't correct |
74 |
Incorrect |
618 ms |
11972 KB |
Output isn't correct |
75 |
Incorrect |
625 ms |
11984 KB |
Output isn't correct |
76 |
Incorrect |
640 ms |
11992 KB |
Output isn't correct |
77 |
Incorrect |
634 ms |
11968 KB |
Output isn't correct |
78 |
Correct |
593 ms |
11968 KB |
Output is correct |
79 |
Incorrect |
596 ms |
12008 KB |
Output isn't correct |
80 |
Incorrect |
601 ms |
11984 KB |
Output isn't correct |
81 |
Incorrect |
608 ms |
11976 KB |
Output isn't correct |
82 |
Incorrect |
594 ms |
11968 KB |
Output isn't correct |
83 |
Incorrect |
601 ms |
11876 KB |
Output isn't correct |
84 |
Incorrect |
614 ms |
11940 KB |
Output isn't correct |
85 |
Incorrect |
610 ms |
11876 KB |
Output isn't correct |
86 |
Incorrect |
594 ms |
11932 KB |
Output isn't correct |
87 |
Incorrect |
612 ms |
11948 KB |
Output isn't correct |
88 |
Incorrect |
596 ms |
11964 KB |
Output isn't correct |
89 |
Incorrect |
599 ms |
11972 KB |
Output isn't correct |
90 |
Incorrect |
610 ms |
11968 KB |
Output isn't correct |
91 |
Incorrect |
612 ms |
11976 KB |
Output isn't correct |
92 |
Incorrect |
619 ms |
11968 KB |
Output isn't correct |