#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) {
vector<vector<char> > u(n, vector<char>(n));
queue<pair<int, pair<int, int> > > q;
u[sx][sy] = true;
q.push(mp(0, 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] && d[i][j] != 'T') {
if(i == fx && j == fy) { return true; }
int cl = cd / s + tt;
if(b[i][j] > cl) { q.push(mp(cd + 1, mp(i, j))); u[i][j] = true; }
}
}
}
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] + 1; 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 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Incorrect |
1 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 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
101 ms |
4136 KB |
Output is correct |
8 |
Incorrect |
1 ms |
332 KB |
Output isn't 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 |
Incorrect |
1 ms |
512 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
28 |
Correct |
1 ms |
588 KB |
Output is correct |
29 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
30 |
Correct |
1 ms |
588 KB |
Output is correct |
31 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
32 |
Correct |
1 ms |
588 KB |
Output is correct |
33 |
Incorrect |
6 ms |
1836 KB |
Output isn't correct |
34 |
Correct |
7 ms |
1896 KB |
Output is correct |
35 |
Correct |
29 ms |
1740 KB |
Output is correct |
36 |
Incorrect |
7 ms |
1996 KB |
Output isn't correct |
37 |
Correct |
7 ms |
2032 KB |
Output is correct |
38 |
Correct |
33 ms |
2068 KB |
Output is correct |
39 |
Incorrect |
8 ms |
2252 KB |
Output isn't correct |
40 |
Correct |
8 ms |
2252 KB |
Output is correct |
41 |
Correct |
47 ms |
2236 KB |
Output is correct |
42 |
Incorrect |
9 ms |
2488 KB |
Output isn't correct |
43 |
Correct |
9 ms |
2564 KB |
Output is correct |
44 |
Correct |
53 ms |
2508 KB |
Output is correct |
45 |
Incorrect |
11 ms |
2764 KB |
Output isn't correct |
46 |
Correct |
11 ms |
2764 KB |
Output is correct |
47 |
Correct |
63 ms |
2800 KB |
Output is correct |
48 |
Incorrect |
15 ms |
2968 KB |
Output isn't correct |
49 |
Correct |
13 ms |
3020 KB |
Output is correct |
50 |
Correct |
78 ms |
2952 KB |
Output is correct |
51 |
Incorrect |
15 ms |
3276 KB |
Output isn't correct |
52 |
Correct |
18 ms |
3272 KB |
Output is correct |
53 |
Correct |
91 ms |
3272 KB |
Output is correct |
54 |
Incorrect |
18 ms |
3532 KB |
Output isn't correct |
55 |
Correct |
17 ms |
3592 KB |
Output is correct |
56 |
Correct |
112 ms |
3604 KB |
Output is correct |
57 |
Incorrect |
20 ms |
3868 KB |
Output isn't correct |
58 |
Correct |
19 ms |
3796 KB |
Output is correct |
59 |
Correct |
134 ms |
3856 KB |
Output is correct |
60 |
Incorrect |
22 ms |
4112 KB |
Output isn't correct |
61 |
Correct |
22 ms |
4116 KB |
Output is correct |
62 |
Correct |
140 ms |
4124 KB |
Output is correct |
63 |
Correct |
122 ms |
4116 KB |
Output is correct |
64 |
Correct |
225 ms |
4196 KB |
Output is correct |
65 |
Correct |
166 ms |
4116 KB |
Output is correct |
66 |
Incorrect |
141 ms |
4124 KB |
Output isn't correct |
67 |
Correct |
40 ms |
4044 KB |
Output is correct |
68 |
Correct |
61 ms |
4120 KB |
Output is correct |
69 |
Correct |
57 ms |
4092 KB |
Output is correct |
70 |
Correct |
54 ms |
4120 KB |
Output is correct |
71 |
Correct |
52 ms |
4116 KB |
Output is correct |
72 |
Incorrect |
39 ms |
4016 KB |
Output isn't correct |
73 |
Incorrect |
45 ms |
4020 KB |
Output isn't correct |
74 |
Correct |
77 ms |
4020 KB |
Output is correct |
75 |
Correct |
89 ms |
4208 KB |
Output is correct |
76 |
Correct |
79 ms |
4120 KB |
Output is correct |
77 |
Correct |
90 ms |
4132 KB |
Output is correct |
78 |
Correct |
38 ms |
4036 KB |
Output is correct |
79 |
Correct |
66 ms |
4044 KB |
Output is correct |
80 |
Correct |
71 ms |
4068 KB |
Output is correct |
81 |
Correct |
83 ms |
4120 KB |
Output is correct |
82 |
Correct |
73 ms |
4044 KB |
Output is correct |
83 |
Correct |
90 ms |
4060 KB |
Output is correct |
84 |
Correct |
94 ms |
4044 KB |
Output is correct |
85 |
Correct |
90 ms |
4036 KB |
Output is correct |
86 |
Correct |
97 ms |
4136 KB |
Output is correct |
87 |
Correct |
92 ms |
4128 KB |
Output is correct |
88 |
Correct |
94 ms |
4136 KB |
Output is correct |
89 |
Correct |
107 ms |
4164 KB |
Output is correct |
90 |
Correct |
106 ms |
4128 KB |
Output is correct |
91 |
Correct |
99 ms |
4036 KB |
Output is correct |
92 |
Correct |
94 ms |
4012 KB |
Output is correct |