#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 |
6 ms |
5468 KB |
Output isn't correct |
2 |
Incorrect |
7 ms |
5468 KB |
Output isn't correct |
3 |
Incorrect |
5 ms |
5468 KB |
Output isn't correct |
4 |
Incorrect |
5 ms |
5468 KB |
Output isn't correct |
5 |
Correct |
5 ms |
5464 KB |
Output is correct |
6 |
Correct |
5 ms |
5468 KB |
Output is correct |
7 |
Correct |
721 ms |
11996 KB |
Output is correct |
8 |
Incorrect |
7 ms |
5464 KB |
Output isn't correct |
9 |
Correct |
6 ms |
5468 KB |
Output is correct |
10 |
Correct |
6 ms |
5468 KB |
Output is correct |
11 |
Correct |
6 ms |
5528 KB |
Output is correct |
12 |
Incorrect |
8 ms |
5468 KB |
Output isn't correct |
13 |
Incorrect |
8 ms |
5468 KB |
Output isn't correct |
14 |
Correct |
9 ms |
5464 KB |
Output is correct |
15 |
Incorrect |
6 ms |
5464 KB |
Output isn't correct |
16 |
Correct |
5 ms |
5464 KB |
Output is correct |
17 |
Incorrect |
6 ms |
5468 KB |
Output isn't correct |
18 |
Correct |
6 ms |
5468 KB |
Output is correct |
19 |
Incorrect |
6 ms |
5468 KB |
Output isn't correct |
20 |
Correct |
6 ms |
5344 KB |
Output is correct |
21 |
Incorrect |
6 ms |
5468 KB |
Output isn't correct |
22 |
Correct |
6 ms |
5572 KB |
Output is correct |
23 |
Incorrect |
6 ms |
5592 KB |
Output isn't correct |
24 |
Correct |
6 ms |
5468 KB |
Output is correct |
25 |
Incorrect |
7 ms |
5584 KB |
Output isn't correct |
26 |
Correct |
7 ms |
5468 KB |
Output is correct |
27 |
Incorrect |
8 ms |
5468 KB |
Output isn't correct |
28 |
Correct |
8 ms |
5468 KB |
Output is correct |
29 |
Incorrect |
9 ms |
5624 KB |
Output isn't correct |
30 |
Correct |
8 ms |
5468 KB |
Output is correct |
31 |
Incorrect |
8 ms |
5464 KB |
Output isn't correct |
32 |
Correct |
11 ms |
5468 KB |
Output is correct |
33 |
Incorrect |
83 ms |
6452 KB |
Output isn't correct |
34 |
Correct |
68 ms |
6444 KB |
Output is correct |
35 |
Correct |
101 ms |
6700 KB |
Output is correct |
36 |
Incorrect |
86 ms |
6644 KB |
Output isn't correct |
37 |
Correct |
95 ms |
6644 KB |
Output is correct |
38 |
Correct |
123 ms |
7000 KB |
Output is correct |
39 |
Incorrect |
107 ms |
6896 KB |
Output isn't correct |
40 |
Correct |
105 ms |
6896 KB |
Output is correct |
41 |
Correct |
148 ms |
7276 KB |
Output is correct |
42 |
Incorrect |
133 ms |
7164 KB |
Output isn't correct |
43 |
Correct |
131 ms |
7164 KB |
Output is correct |
44 |
Correct |
187 ms |
7956 KB |
Output is correct |
45 |
Incorrect |
158 ms |
7452 KB |
Output isn't correct |
46 |
Correct |
147 ms |
7480 KB |
Output is correct |
47 |
Correct |
225 ms |
8120 KB |
Output is correct |
48 |
Incorrect |
185 ms |
7784 KB |
Output isn't correct |
49 |
Correct |
177 ms |
7804 KB |
Output is correct |
50 |
Correct |
277 ms |
8560 KB |
Output is correct |
51 |
Incorrect |
218 ms |
8184 KB |
Output isn't correct |
52 |
Correct |
224 ms |
8192 KB |
Output is correct |
53 |
Correct |
321 ms |
9048 KB |
Output is correct |
54 |
Incorrect |
267 ms |
8596 KB |
Output isn't correct |
55 |
Correct |
249 ms |
8556 KB |
Output is correct |
56 |
Correct |
366 ms |
9560 KB |
Output is correct |
57 |
Incorrect |
302 ms |
8916 KB |
Output isn't correct |
58 |
Correct |
269 ms |
8964 KB |
Output is correct |
59 |
Correct |
422 ms |
10080 KB |
Output is correct |
60 |
Incorrect |
345 ms |
9388 KB |
Output isn't correct |
61 |
Correct |
324 ms |
9408 KB |
Output is correct |
62 |
Correct |
456 ms |
10724 KB |
Output is correct |
63 |
Correct |
670 ms |
10144 KB |
Output is correct |
64 |
Correct |
706 ms |
9888 KB |
Output is correct |
65 |
Correct |
743 ms |
9892 KB |
Output is correct |
66 |
Incorrect |
682 ms |
9912 KB |
Output isn't correct |
67 |
Correct |
659 ms |
9912 KB |
Output is correct |
68 |
Correct |
732 ms |
10160 KB |
Output is correct |
69 |
Correct |
728 ms |
10004 KB |
Output is correct |
70 |
Correct |
727 ms |
9900 KB |
Output is correct |
71 |
Correct |
721 ms |
9904 KB |
Output is correct |
72 |
Incorrect |
799 ms |
9904 KB |
Output isn't correct |
73 |
Incorrect |
670 ms |
11908 KB |
Output isn't correct |
74 |
Correct |
705 ms |
11956 KB |
Output is correct |
75 |
Correct |
701 ms |
11972 KB |
Output is correct |
76 |
Correct |
710 ms |
11976 KB |
Output is correct |
77 |
Correct |
708 ms |
12204 KB |
Output is correct |
78 |
Correct |
629 ms |
11980 KB |
Output is correct |
79 |
Correct |
693 ms |
11976 KB |
Output is correct |
80 |
Correct |
665 ms |
11972 KB |
Output is correct |
81 |
Correct |
678 ms |
11968 KB |
Output is correct |
82 |
Correct |
696 ms |
12136 KB |
Output is correct |
83 |
Correct |
638 ms |
11888 KB |
Output is correct |
84 |
Correct |
742 ms |
11976 KB |
Output is correct |
85 |
Correct |
679 ms |
11960 KB |
Output is correct |
86 |
Correct |
680 ms |
11952 KB |
Output is correct |
87 |
Correct |
684 ms |
12044 KB |
Output is correct |
88 |
Correct |
708 ms |
12176 KB |
Output is correct |
89 |
Correct |
709 ms |
11996 KB |
Output is correct |
90 |
Correct |
660 ms |
11972 KB |
Output is correct |
91 |
Correct |
680 ms |
11964 KB |
Output is correct |
92 |
Correct |
639 ms |
11976 KB |
Output is correct |