#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] + 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: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 |
Correct |
5 ms |
5468 KB |
Output is correct |
2 |
Correct |
6 ms |
5468 KB |
Output is correct |
3 |
Correct |
5 ms |
5540 KB |
Output is correct |
4 |
Correct |
5 ms |
5536 KB |
Output is correct |
5 |
Correct |
5 ms |
5468 KB |
Output is correct |
6 |
Correct |
5 ms |
5468 KB |
Output is correct |
7 |
Correct |
659 ms |
12036 KB |
Output is correct |
8 |
Correct |
6 ms |
5464 KB |
Output is correct |
9 |
Correct |
5 ms |
5468 KB |
Output is correct |
10 |
Correct |
6 ms |
5468 KB |
Output is correct |
11 |
Correct |
6 ms |
5468 KB |
Output is correct |
12 |
Incorrect |
7 ms |
5576 KB |
Output isn't correct |
13 |
Incorrect |
9 ms |
5468 KB |
Output isn't correct |
14 |
Correct |
11 ms |
5468 KB |
Output is correct |
15 |
Correct |
6 ms |
5468 KB |
Output is correct |
16 |
Correct |
6 ms |
5464 KB |
Output is correct |
17 |
Correct |
6 ms |
5468 KB |
Output is correct |
18 |
Correct |
6 ms |
5468 KB |
Output is correct |
19 |
Correct |
6 ms |
5720 KB |
Output is correct |
20 |
Correct |
6 ms |
5564 KB |
Output is correct |
21 |
Correct |
6 ms |
5468 KB |
Output is correct |
22 |
Correct |
7 ms |
5468 KB |
Output is correct |
23 |
Correct |
6 ms |
5592 KB |
Output is correct |
24 |
Correct |
7 ms |
5468 KB |
Output is correct |
25 |
Correct |
7 ms |
5468 KB |
Output is correct |
26 |
Correct |
7 ms |
5468 KB |
Output is correct |
27 |
Correct |
8 ms |
5724 KB |
Output is correct |
28 |
Correct |
9 ms |
5468 KB |
Output is correct |
29 |
Correct |
8 ms |
5420 KB |
Output is correct |
30 |
Correct |
8 ms |
5468 KB |
Output is correct |
31 |
Correct |
8 ms |
5632 KB |
Output is correct |
32 |
Correct |
9 ms |
5468 KB |
Output is correct |
33 |
Correct |
75 ms |
6448 KB |
Output is correct |
34 |
Correct |
66 ms |
6440 KB |
Output is correct |
35 |
Correct |
95 ms |
6660 KB |
Output is correct |
36 |
Correct |
88 ms |
6644 KB |
Output is correct |
37 |
Correct |
97 ms |
6640 KB |
Output is correct |
38 |
Correct |
121 ms |
6964 KB |
Output is correct |
39 |
Correct |
107 ms |
6904 KB |
Output is correct |
40 |
Correct |
103 ms |
6896 KB |
Output is correct |
41 |
Correct |
161 ms |
7272 KB |
Output is correct |
42 |
Correct |
134 ms |
7168 KB |
Output is correct |
43 |
Correct |
130 ms |
7320 KB |
Output is correct |
44 |
Correct |
184 ms |
7676 KB |
Output is correct |
45 |
Correct |
169 ms |
7516 KB |
Output is correct |
46 |
Correct |
149 ms |
7476 KB |
Output is correct |
47 |
Correct |
227 ms |
8116 KB |
Output is correct |
48 |
Correct |
191 ms |
7848 KB |
Output is correct |
49 |
Correct |
180 ms |
7776 KB |
Output is correct |
50 |
Correct |
267 ms |
8560 KB |
Output is correct |
51 |
Correct |
227 ms |
8176 KB |
Output is correct |
52 |
Correct |
212 ms |
8188 KB |
Output is correct |
53 |
Correct |
317 ms |
9048 KB |
Output is correct |
54 |
Correct |
249 ms |
8536 KB |
Output is correct |
55 |
Correct |
249 ms |
8592 KB |
Output is correct |
56 |
Correct |
376 ms |
9560 KB |
Output is correct |
57 |
Correct |
288 ms |
8980 KB |
Output is correct |
58 |
Correct |
274 ms |
8968 KB |
Output is correct |
59 |
Correct |
414 ms |
10116 KB |
Output is correct |
60 |
Correct |
351 ms |
9408 KB |
Output is correct |
61 |
Correct |
314 ms |
9440 KB |
Output is correct |
62 |
Correct |
461 ms |
10708 KB |
Output is correct |
63 |
Correct |
669 ms |
9892 KB |
Output is correct |
64 |
Correct |
701 ms |
9912 KB |
Output is correct |
65 |
Correct |
699 ms |
10152 KB |
Output is correct |
66 |
Correct |
679 ms |
9908 KB |
Output is correct |
67 |
Correct |
645 ms |
9912 KB |
Output is correct |
68 |
Correct |
726 ms |
9980 KB |
Output is correct |
69 |
Correct |
726 ms |
9916 KB |
Output is correct |
70 |
Correct |
695 ms |
9900 KB |
Output is correct |
71 |
Correct |
713 ms |
9904 KB |
Output is correct |
72 |
Incorrect |
813 ms |
9912 KB |
Output isn't correct |
73 |
Incorrect |
645 ms |
11888 KB |
Output isn't correct |
74 |
Correct |
704 ms |
11984 KB |
Output is correct |
75 |
Correct |
716 ms |
11956 KB |
Output is correct |
76 |
Correct |
710 ms |
11972 KB |
Output is correct |
77 |
Correct |
723 ms |
11972 KB |
Output is correct |
78 |
Correct |
626 ms |
11976 KB |
Output is correct |
79 |
Correct |
669 ms |
12352 KB |
Output is correct |
80 |
Correct |
676 ms |
11976 KB |
Output is correct |
81 |
Correct |
661 ms |
11952 KB |
Output is correct |
82 |
Correct |
659 ms |
11956 KB |
Output is correct |
83 |
Correct |
645 ms |
12068 KB |
Output is correct |
84 |
Correct |
724 ms |
11964 KB |
Output is correct |
85 |
Correct |
669 ms |
12120 KB |
Output is correct |
86 |
Correct |
678 ms |
11960 KB |
Output is correct |
87 |
Correct |
700 ms |
11952 KB |
Output is correct |
88 |
Correct |
654 ms |
11988 KB |
Output is correct |
89 |
Correct |
655 ms |
11980 KB |
Output is correct |
90 |
Correct |
651 ms |
11984 KB |
Output is correct |
91 |
Correct |
667 ms |
11972 KB |
Output is correct |
92 |
Correct |
643 ms |
11992 KB |
Output is correct |