#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define nl cout<<"\n"
#define f first
#define s second
#define ca(v) for(auto i:v) cout<<i<<" ";
int xm[4] = {-1, 1, 0, 0};
int ym[4] = {0, 0, -1, 1};
int grid[801][801];
int dst[801][801];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
if (fopen("input.in", "r")) freopen("input.in", "r", stdin);
int n; cin>>n; int x; cin>>x;
queue<pair<int, int>> q;
pair<int, int> st;
pair<int, int> ed;
vector<vector<bool>> vis(n, vector<bool>(n, 0));
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
char c; cin>>c;
grid[i][j] = (c != 'T');
if(c == 'M') st = {i, j};
if(c == 'D') ed = {i, j};
if(c == 'H') {
q.push({i, j});
vis[i][j] = 1;
}
}
}
while(q.size()){
auto c = q.front(); q.pop();
for(int i=0; i<4; i++){
int nx = c.f + xm[i];
int ny = c.s + ym[i];
if((nx != ed.f || ny != ed.s) && nx >= 0 && nx < n && ny >= 0 && ny < n && grid[nx][ny] && !vis[nx][ny]){
vis[nx][ny] = 1;
dst[nx][ny] = dst[c.f][c.s] + 1;
q.push({nx, ny});
}
}
}
dst[ed.f][ed.s] = n*n + 5;
int l = 0;
int r = dst[st.f][st.s] - 1;
int ans = -1;
while(l <= r){
int m = (l + r) / 2;
vis = vector<vector<bool>>(n, vector<bool>(n, 0));
vector<vector<int>> d(n, vector<int>(n, 0));
vis[st.f][st.s] = 1;
d[st.f][st.s] = m * x;
q.push({st.f, st.s});
// cout<<m<<"\n";
while(q.size()){
auto c = q.front(); q.pop();
int mx = (d[c.f][c.s] + 1) / x;
for(int i=0; i<4; i++){
int nx = c.f + xm[i];
int ny = c.s + ym[i];
if(nx >= 0 && nx < n && ny >= 0 && ny < n && grid[nx][ny] && !vis[nx][ny] && mx < dst[nx][ny]){
d[nx][ny] = d[c.f][c.s] + 1;
vis[nx][ny] = 1;
q.push({nx, ny});
}
}
}
// for(int i=0; i<n; i++){
// for(int j=0; j<n; j++){
// cout<<dst[i][j]<<"|"<<d[i][j]<<" ";
// } nl;
// }
if(vis[ed.f][ed.s]){
l = m+1;
ans = m;
}
else{
r = m-1;
}
// nl;
}
cout<<ans;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:19:40: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | if (fopen("input.in", "r")) freopen("input.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
101 ms |
8004 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
0 ms |
460 KB |
Output is correct |
19 |
Correct |
0 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
0 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
716 KB |
Output is correct |
26 |
Correct |
1 ms |
716 KB |
Output is correct |
27 |
Correct |
1 ms |
716 KB |
Output is correct |
28 |
Correct |
1 ms |
716 KB |
Output is correct |
29 |
Correct |
1 ms |
716 KB |
Output is correct |
30 |
Correct |
1 ms |
716 KB |
Output is correct |
31 |
Correct |
1 ms |
716 KB |
Output is correct |
32 |
Correct |
1 ms |
716 KB |
Output is correct |
33 |
Correct |
7 ms |
3036 KB |
Output is correct |
34 |
Correct |
9 ms |
3036 KB |
Output is correct |
35 |
Correct |
33 ms |
3020 KB |
Output is correct |
36 |
Correct |
10 ms |
3512 KB |
Output is correct |
37 |
Correct |
10 ms |
3512 KB |
Output is correct |
38 |
Correct |
31 ms |
3496 KB |
Output is correct |
39 |
Correct |
12 ms |
4012 KB |
Output is correct |
40 |
Correct |
12 ms |
4024 KB |
Output is correct |
41 |
Correct |
40 ms |
4020 KB |
Output is correct |
42 |
Correct |
16 ms |
4516 KB |
Output is correct |
43 |
Correct |
16 ms |
4600 KB |
Output is correct |
44 |
Correct |
52 ms |
4512 KB |
Output is correct |
45 |
Correct |
19 ms |
5096 KB |
Output is correct |
46 |
Correct |
21 ms |
5040 KB |
Output is correct |
47 |
Correct |
60 ms |
5044 KB |
Output is correct |
48 |
Correct |
21 ms |
5600 KB |
Output is correct |
49 |
Correct |
22 ms |
5604 KB |
Output is correct |
50 |
Correct |
69 ms |
5608 KB |
Output is correct |
51 |
Correct |
24 ms |
6208 KB |
Output is correct |
52 |
Correct |
26 ms |
6168 KB |
Output is correct |
53 |
Correct |
93 ms |
6264 KB |
Output is correct |
54 |
Correct |
30 ms |
6764 KB |
Output is correct |
55 |
Correct |
32 ms |
6800 KB |
Output is correct |
56 |
Correct |
97 ms |
6748 KB |
Output is correct |
57 |
Correct |
33 ms |
7392 KB |
Output is correct |
58 |
Correct |
36 ms |
7404 KB |
Output is correct |
59 |
Correct |
114 ms |
7368 KB |
Output is correct |
60 |
Correct |
39 ms |
8024 KB |
Output is correct |
61 |
Correct |
40 ms |
8016 KB |
Output is correct |
62 |
Correct |
145 ms |
8044 KB |
Output is correct |
63 |
Correct |
118 ms |
7956 KB |
Output is correct |
64 |
Correct |
189 ms |
7988 KB |
Output is correct |
65 |
Correct |
189 ms |
7984 KB |
Output is correct |
66 |
Correct |
146 ms |
8028 KB |
Output is correct |
67 |
Correct |
128 ms |
7988 KB |
Output is correct |
68 |
Correct |
57 ms |
8008 KB |
Output is correct |
69 |
Correct |
53 ms |
8012 KB |
Output is correct |
70 |
Correct |
48 ms |
8008 KB |
Output is correct |
71 |
Correct |
40 ms |
8020 KB |
Output is correct |
72 |
Correct |
44 ms |
8004 KB |
Output is correct |
73 |
Correct |
44 ms |
7976 KB |
Output is correct |
74 |
Correct |
72 ms |
8016 KB |
Output is correct |
75 |
Correct |
69 ms |
8012 KB |
Output is correct |
76 |
Correct |
73 ms |
8016 KB |
Output is correct |
77 |
Correct |
77 ms |
8012 KB |
Output is correct |
78 |
Correct |
82 ms |
8004 KB |
Output is correct |
79 |
Correct |
70 ms |
8020 KB |
Output is correct |
80 |
Correct |
74 ms |
8000 KB |
Output is correct |
81 |
Correct |
75 ms |
8040 KB |
Output is correct |
82 |
Correct |
72 ms |
8008 KB |
Output is correct |
83 |
Correct |
93 ms |
8004 KB |
Output is correct |
84 |
Correct |
79 ms |
8004 KB |
Output is correct |
85 |
Correct |
84 ms |
8148 KB |
Output is correct |
86 |
Correct |
90 ms |
8004 KB |
Output is correct |
87 |
Correct |
86 ms |
7972 KB |
Output is correct |
88 |
Correct |
97 ms |
8008 KB |
Output is correct |
89 |
Correct |
95 ms |
8028 KB |
Output is correct |
90 |
Correct |
113 ms |
8052 KB |
Output is correct |
91 |
Correct |
96 ms |
7992 KB |
Output is correct |
92 |
Correct |
97 ms |
8012 KB |
Output is correct |