#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << " "
#define nl cout << "\n"
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int dx[] = {-1, +1, 0, 0};
int dy[] = {0, 0, -1, +1};
const int MAXN = 1e3+100;
int N, S, sx, sy, ex, ey, d[MAXN][MAXN], D[MAXN][MAXN];
char mat[MAXN][MAXN];
bool valid(int x, int y){
return (x >= 0 && x < N && y >= 0 && y < N && mat[x][y] != 'T');
}
bool check(int mid){
if(mid >= d[sx][sy]){
return false;
}
rep(i, 0, N){
rep(j, 0, N){
D[i][j] = INT_MAX;
}
}
D[sx][sy] = 0;
queue<pii> q;
q.push({sx, sy});
while(!q.empty()){
auto [x, y] = q.front();
q.pop();
int dist = (D[x][y] + 1) / S;
rep(i, 0, 4){
int x2 = x + dx[i], y2 = y + dy[i];
if(valid(x2, y2) && D[x2][y2] == INT_MAX && d[x2][y2] > dist + mid){
D[x2][y2] = D[x][y] + 1;
q.push({x2, y2});
}
}
}
if(D[ex][ey] != INT_MAX)
return true;
return false;
}
void solve(){
cin >> N >> S;
queue<pii> q;
rep(i, 0, N){
string s; cin >> s;
rep(j, 0, N){
char c = mat[i][j] = s[j];
d[i][j] = INT_MAX;
if(c == 'H'){
d[i][j] = 0;
q.push({i, j});
}
if(c == 'M'){
sx = i, sy = j;
}
if(c == 'D'){
ex = i, ey = j;
}
}
}
while(!q.empty()){
auto [x, y] = q.front();
q.pop();
rep(i, 0, 4){
int x2 = x + dx[i], y2 = y + dy[i];
if(valid(x2, y2) && d[x2][y2] == INT_MAX && mat[x2][y2] != 'D'){
d[x2][y2] = d[x][y] + 1;
q.push({x2, y2});
}
}
}
int l = -1, r = 1e9;
while(r - l > 1){
int m = (l + r) / 2;
if(check(m)) l = m;
else r = m;
}
cout << l;
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr);
solve();
return 0;
}
Compilation message
mecho.cpp: In function 'bool check(int)':
mecho.cpp:41:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
41 | auto [x, y] = q.front();
| ^
mecho.cpp: In function 'void solve()':
mecho.cpp:78:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
78 | auto [x, y] = q.front();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
106 ms |
8740 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
452 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
824 KB |
Output is correct |
13 |
Correct |
1 ms |
708 KB |
Output is correct |
14 |
Correct |
2 ms |
844 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
456 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
464 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
716 KB |
Output is correct |
23 |
Correct |
1 ms |
716 KB |
Output is correct |
24 |
Correct |
1 ms |
716 KB |
Output is correct |
25 |
Correct |
1 ms |
844 KB |
Output is correct |
26 |
Correct |
1 ms |
844 KB |
Output is correct |
27 |
Correct |
1 ms |
972 KB |
Output is correct |
28 |
Correct |
1 ms |
844 KB |
Output is correct |
29 |
Correct |
1 ms |
972 KB |
Output is correct |
30 |
Correct |
1 ms |
972 KB |
Output is correct |
31 |
Correct |
1 ms |
972 KB |
Output is correct |
32 |
Correct |
1 ms |
972 KB |
Output is correct |
33 |
Correct |
6 ms |
3788 KB |
Output is correct |
34 |
Correct |
6 ms |
3764 KB |
Output is correct |
35 |
Correct |
31 ms |
3916 KB |
Output is correct |
36 |
Correct |
6 ms |
4300 KB |
Output is correct |
37 |
Correct |
6 ms |
4300 KB |
Output is correct |
38 |
Correct |
36 ms |
4300 KB |
Output is correct |
39 |
Correct |
9 ms |
4812 KB |
Output is correct |
40 |
Correct |
9 ms |
4856 KB |
Output is correct |
41 |
Correct |
43 ms |
4896 KB |
Output is correct |
42 |
Correct |
8 ms |
5324 KB |
Output is correct |
43 |
Correct |
7 ms |
5404 KB |
Output is correct |
44 |
Correct |
58 ms |
5324 KB |
Output is correct |
45 |
Correct |
11 ms |
5976 KB |
Output is correct |
46 |
Correct |
12 ms |
5964 KB |
Output is correct |
47 |
Correct |
71 ms |
5928 KB |
Output is correct |
48 |
Correct |
13 ms |
6476 KB |
Output is correct |
49 |
Correct |
13 ms |
6468 KB |
Output is correct |
50 |
Correct |
85 ms |
6484 KB |
Output is correct |
51 |
Correct |
18 ms |
6968 KB |
Output is correct |
52 |
Correct |
19 ms |
7076 KB |
Output is correct |
53 |
Correct |
114 ms |
7064 KB |
Output is correct |
54 |
Correct |
19 ms |
7536 KB |
Output is correct |
55 |
Correct |
22 ms |
7620 KB |
Output is correct |
56 |
Correct |
151 ms |
7628 KB |
Output is correct |
57 |
Correct |
17 ms |
8180 KB |
Output is correct |
58 |
Correct |
15 ms |
8152 KB |
Output is correct |
59 |
Correct |
138 ms |
8180 KB |
Output is correct |
60 |
Correct |
22 ms |
8652 KB |
Output is correct |
61 |
Correct |
20 ms |
8624 KB |
Output is correct |
62 |
Correct |
196 ms |
8780 KB |
Output is correct |
63 |
Correct |
115 ms |
8628 KB |
Output is correct |
64 |
Correct |
196 ms |
8644 KB |
Output is correct |
65 |
Correct |
193 ms |
8776 KB |
Output is correct |
66 |
Correct |
131 ms |
8616 KB |
Output is correct |
67 |
Correct |
117 ms |
8720 KB |
Output is correct |
68 |
Correct |
51 ms |
8648 KB |
Output is correct |
69 |
Correct |
54 ms |
8660 KB |
Output is correct |
70 |
Correct |
34 ms |
8652 KB |
Output is correct |
71 |
Correct |
42 ms |
8648 KB |
Output is correct |
72 |
Correct |
40 ms |
8748 KB |
Output is correct |
73 |
Correct |
49 ms |
8900 KB |
Output is correct |
74 |
Correct |
70 ms |
9004 KB |
Output is correct |
75 |
Correct |
88 ms |
8896 KB |
Output is correct |
76 |
Correct |
72 ms |
8908 KB |
Output is correct |
77 |
Correct |
79 ms |
8896 KB |
Output is correct |
78 |
Correct |
87 ms |
8968 KB |
Output is correct |
79 |
Correct |
65 ms |
8908 KB |
Output is correct |
80 |
Correct |
71 ms |
8908 KB |
Output is correct |
81 |
Correct |
81 ms |
8880 KB |
Output is correct |
82 |
Correct |
66 ms |
8872 KB |
Output is correct |
83 |
Correct |
103 ms |
8908 KB |
Output is correct |
84 |
Correct |
83 ms |
8900 KB |
Output is correct |
85 |
Correct |
81 ms |
8900 KB |
Output is correct |
86 |
Correct |
90 ms |
8900 KB |
Output is correct |
87 |
Correct |
101 ms |
8936 KB |
Output is correct |
88 |
Correct |
125 ms |
8884 KB |
Output is correct |
89 |
Correct |
96 ms |
8776 KB |
Output is correct |
90 |
Correct |
102 ms |
8884 KB |
Output is correct |
91 |
Correct |
99 ms |
8884 KB |
Output is correct |
92 |
Correct |
127 ms |
8884 KB |
Output is correct |