#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <unordered_map>
#include <iomanip>
#include <cmath>
#include <queue>
#include <bitset>
#include <numeric>
#include <array>
#include <cstring>
#include <random>
#include <chrono>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template<class T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 803;
char d[N][N];
int b[N][N];
int sx = 0, sy = 0, fx = 0, fy = 0;
int n, s;
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
char check(int tt) {
if(b[sx][sy] <= tt * s) return false;
vector<vector<int> > u(n, vector<int>(n, -1));
u[sx][sy] = tt * s;
queue<pair<int, pair<int, int> > > q;
q.push(mp(tt * s, mp(sx, sy)));
while(!q.empty()) {
auto x = q.front(); q.pop();
int xx = x.se.fi, yy = x.se.se, cd = x.fi;
if(xx == fx && yy == fy) return true;
for(int mv = 0; mv < 4; mv++) {
int i = xx + dx[mv], j = yy + dy[mv];
if(i >= 0 && i < n && j >= 0 && j < n && u[i][j] == -1 && d[i][j] != 'T') {
if(i == fx && j == fy) return true;
if(cd + 1 < b[i][j]) {
u[i][j] = cd + 1;
q.push(mp(cd + 1, mp(i, j)));
}
}
}
}
return false;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> s;
queue<pair<int, int> > bee;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) {
b[i][j] = -1;
cin >> d[i][j];
if(d[i][j] == 'H') { bee.push(mp(i, j)); b[i][j] = 0; }
}
while(!bee.empty()) {
auto x = bee.front(); bee.pop();
for(int mv = 0; mv < 4; mv++) {
int i = x.fi + dx[mv], j = x.se + dy[mv];
if(i >= 0 && i < n && j >= 0 && j < n && b[i][j] == -1 && d[i][j] != 'T' && d[i][j] != 'D') { b[i][j] = b[x.fi][x.se] + s; bee.push(mp(i, j)); }
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(d[i][j] == 'D') fx = i, fy = j;
if(d[i][j] == 'M') sx = i, sy = j;
}
}
if(!check(0)) { cout << -1; return 0; }
int l = 0, r = n * n;
while(l < r) {
int m = (l + r + 1) / 2;
if(check(m)) l = m;
else r = m - 1;
}
cout << l;
return 0;
}
/*
*/
Compilation message
mecho.cpp:3: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
3 | #pragma GCC optimization ("unroll-loops")
|
# |
결과 |
실행 시간 |
메모리 |
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 |
113 ms |
6104 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
448 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
588 KB |
Output is correct |
28 |
Correct |
1 ms |
588 KB |
Output is correct |
29 |
Correct |
1 ms |
588 KB |
Output is correct |
30 |
Correct |
1 ms |
588 KB |
Output is correct |
31 |
Correct |
1 ms |
588 KB |
Output is correct |
32 |
Correct |
1 ms |
588 KB |
Output is correct |
33 |
Correct |
7 ms |
2196 KB |
Output is correct |
34 |
Correct |
7 ms |
2188 KB |
Output is correct |
35 |
Correct |
24 ms |
2228 KB |
Output is correct |
36 |
Correct |
8 ms |
2508 KB |
Output is correct |
37 |
Correct |
9 ms |
2508 KB |
Output is correct |
38 |
Correct |
32 ms |
2480 KB |
Output is correct |
39 |
Correct |
11 ms |
2900 KB |
Output is correct |
40 |
Correct |
11 ms |
2896 KB |
Output is correct |
41 |
Correct |
42 ms |
2872 KB |
Output is correct |
42 |
Correct |
14 ms |
3340 KB |
Output is correct |
43 |
Correct |
14 ms |
3296 KB |
Output is correct |
44 |
Correct |
51 ms |
3276 KB |
Output is correct |
45 |
Correct |
21 ms |
3716 KB |
Output is correct |
46 |
Correct |
19 ms |
3680 KB |
Output is correct |
47 |
Correct |
63 ms |
3648 KB |
Output is correct |
48 |
Correct |
19 ms |
4116 KB |
Output is correct |
49 |
Correct |
22 ms |
4112 KB |
Output is correct |
50 |
Correct |
78 ms |
4112 KB |
Output is correct |
51 |
Correct |
24 ms |
4532 KB |
Output is correct |
52 |
Correct |
24 ms |
4608 KB |
Output is correct |
53 |
Correct |
94 ms |
4568 KB |
Output is correct |
54 |
Correct |
28 ms |
5020 KB |
Output is correct |
55 |
Correct |
25 ms |
5040 KB |
Output is correct |
56 |
Correct |
111 ms |
5024 KB |
Output is correct |
57 |
Correct |
33 ms |
5544 KB |
Output is correct |
58 |
Correct |
31 ms |
5540 KB |
Output is correct |
59 |
Correct |
136 ms |
5500 KB |
Output is correct |
60 |
Correct |
36 ms |
6028 KB |
Output is correct |
61 |
Correct |
40 ms |
6028 KB |
Output is correct |
62 |
Correct |
145 ms |
6012 KB |
Output is correct |
63 |
Correct |
124 ms |
6084 KB |
Output is correct |
64 |
Correct |
221 ms |
6000 KB |
Output is correct |
65 |
Correct |
189 ms |
5996 KB |
Output is correct |
66 |
Correct |
141 ms |
5996 KB |
Output is correct |
67 |
Correct |
41 ms |
5956 KB |
Output is correct |
68 |
Correct |
68 ms |
6032 KB |
Output is correct |
69 |
Correct |
63 ms |
6012 KB |
Output is correct |
70 |
Correct |
62 ms |
6200 KB |
Output is correct |
71 |
Correct |
54 ms |
6004 KB |
Output is correct |
72 |
Correct |
40 ms |
6008 KB |
Output is correct |
73 |
Correct |
46 ms |
6084 KB |
Output is correct |
74 |
Correct |
82 ms |
6016 KB |
Output is correct |
75 |
Correct |
91 ms |
6036 KB |
Output is correct |
76 |
Correct |
89 ms |
6036 KB |
Output is correct |
77 |
Correct |
92 ms |
6036 KB |
Output is correct |
78 |
Correct |
38 ms |
5964 KB |
Output is correct |
79 |
Correct |
79 ms |
6040 KB |
Output is correct |
80 |
Correct |
80 ms |
6032 KB |
Output is correct |
81 |
Correct |
88 ms |
6032 KB |
Output is correct |
82 |
Correct |
84 ms |
6136 KB |
Output is correct |
83 |
Correct |
100 ms |
6036 KB |
Output is correct |
84 |
Correct |
101 ms |
6044 KB |
Output is correct |
85 |
Correct |
98 ms |
6040 KB |
Output is correct |
86 |
Correct |
100 ms |
6028 KB |
Output is correct |
87 |
Correct |
93 ms |
6052 KB |
Output is correct |
88 |
Correct |
105 ms |
6028 KB |
Output is correct |
89 |
Correct |
137 ms |
6052 KB |
Output is correct |
90 |
Correct |
114 ms |
6028 KB |
Output is correct |
91 |
Correct |
111 ms |
6060 KB |
Output is correct |
92 |
Correct |
103 ms |
6100 KB |
Output is correct |