#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 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
7 |
Incorrect |
104 ms |
8024 KB |
Output isn't correct |
8 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
11 |
Incorrect |
0 ms |
332 KB |
Output isn't 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 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
17 |
Incorrect |
0 ms |
460 KB |
Output isn't correct |
18 |
Incorrect |
0 ms |
460 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
30 |
Incorrect |
2 ms |
716 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
33 |
Incorrect |
12 ms |
3036 KB |
Output isn't correct |
34 |
Incorrect |
8 ms |
3028 KB |
Output isn't correct |
35 |
Incorrect |
24 ms |
3040 KB |
Output isn't correct |
36 |
Incorrect |
10 ms |
3572 KB |
Output isn't correct |
37 |
Incorrect |
13 ms |
3520 KB |
Output isn't correct |
38 |
Incorrect |
44 ms |
3472 KB |
Output isn't correct |
39 |
Incorrect |
16 ms |
4012 KB |
Output isn't correct |
40 |
Incorrect |
17 ms |
4032 KB |
Output isn't correct |
41 |
Incorrect |
41 ms |
4016 KB |
Output isn't correct |
42 |
Incorrect |
16 ms |
4556 KB |
Output isn't correct |
43 |
Incorrect |
16 ms |
4512 KB |
Output isn't correct |
44 |
Incorrect |
49 ms |
4512 KB |
Output isn't correct |
45 |
Incorrect |
18 ms |
5036 KB |
Output isn't correct |
46 |
Incorrect |
25 ms |
5080 KB |
Output isn't correct |
47 |
Incorrect |
63 ms |
5008 KB |
Output isn't correct |
48 |
Incorrect |
22 ms |
5640 KB |
Output isn't correct |
49 |
Incorrect |
30 ms |
5600 KB |
Output isn't correct |
50 |
Incorrect |
73 ms |
5612 KB |
Output isn't correct |
51 |
Incorrect |
24 ms |
6188 KB |
Output isn't correct |
52 |
Incorrect |
27 ms |
6204 KB |
Output isn't correct |
53 |
Incorrect |
88 ms |
6164 KB |
Output isn't correct |
54 |
Incorrect |
33 ms |
6776 KB |
Output isn't correct |
55 |
Incorrect |
34 ms |
6808 KB |
Output isn't correct |
56 |
Incorrect |
131 ms |
6804 KB |
Output isn't correct |
57 |
Incorrect |
34 ms |
7408 KB |
Output isn't correct |
58 |
Incorrect |
36 ms |
7408 KB |
Output isn't correct |
59 |
Incorrect |
125 ms |
7356 KB |
Output isn't correct |
60 |
Incorrect |
39 ms |
8028 KB |
Output isn't correct |
61 |
Incorrect |
48 ms |
8052 KB |
Output isn't correct |
62 |
Incorrect |
160 ms |
7984 KB |
Output isn't correct |
63 |
Incorrect |
135 ms |
8012 KB |
Output isn't correct |
64 |
Incorrect |
202 ms |
7980 KB |
Output isn't correct |
65 |
Incorrect |
216 ms |
7992 KB |
Output isn't correct |
66 |
Incorrect |
151 ms |
7988 KB |
Output isn't correct |
67 |
Correct |
128 ms |
7984 KB |
Output is correct |
68 |
Incorrect |
65 ms |
8008 KB |
Output isn't correct |
69 |
Incorrect |
55 ms |
8016 KB |
Output isn't correct |
70 |
Incorrect |
55 ms |
8024 KB |
Output isn't correct |
71 |
Incorrect |
48 ms |
8024 KB |
Output isn't correct |
72 |
Correct |
55 ms |
8020 KB |
Output is correct |
73 |
Correct |
44 ms |
8000 KB |
Output is correct |
74 |
Incorrect |
61 ms |
8020 KB |
Output isn't correct |
75 |
Incorrect |
76 ms |
8012 KB |
Output isn't correct |
76 |
Incorrect |
67 ms |
8020 KB |
Output isn't correct |
77 |
Incorrect |
71 ms |
8024 KB |
Output isn't correct |
78 |
Correct |
83 ms |
8196 KB |
Output is correct |
79 |
Incorrect |
73 ms |
8032 KB |
Output isn't correct |
80 |
Incorrect |
74 ms |
8004 KB |
Output isn't correct |
81 |
Incorrect |
87 ms |
8016 KB |
Output isn't correct |
82 |
Incorrect |
73 ms |
8028 KB |
Output isn't correct |
83 |
Incorrect |
102 ms |
8000 KB |
Output isn't correct |
84 |
Incorrect |
89 ms |
8084 KB |
Output isn't correct |
85 |
Incorrect |
89 ms |
8016 KB |
Output isn't correct |
86 |
Incorrect |
91 ms |
8004 KB |
Output isn't correct |
87 |
Incorrect |
119 ms |
7968 KB |
Output isn't correct |
88 |
Incorrect |
106 ms |
8000 KB |
Output isn't correct |
89 |
Incorrect |
97 ms |
8040 KB |
Output isn't correct |
90 |
Incorrect |
109 ms |
8012 KB |
Output isn't correct |
91 |
Incorrect |
106 ms |
8008 KB |
Output isn't correct |
92 |
Incorrect |
94 ms |
8000 KB |
Output isn't correct |