#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define sz(s) ((int)s.size())
#define all(v) begin(v), end(v)
#ifdef LOCAL
void debug_print(string s) {
cerr << "\"" << s << "\"";
}
void debug_print(const char* s) {
debug_print((string)s);
}
void debug_print(bool val) {
cerr << (val ? "true" : "false");
}
void debug_print(int val) {
cerr << val;
}
void debug_print(ll val) {
cerr << val;
}
void debug_print(double val) {
cerr << val;
}
template<typename F, typename S>
void debug_print(pair<F, S> val) {
cerr << "(";
debug_print(val.first);
cerr << ", ";
debug_print(val.second);
cerr << ")";
}
void debug_print(vector<bool> val) {
cerr << "{";
bool first = true;
for (bool x : val) {
if (!first) {
cerr << ", ";
} else {
first = false;
}
debug_print(x);
}
cerr << "}";
}
template<typename T>
void debug_print(T val) {
cerr << "{";
bool first = true;
for (const auto &x : val) {
if (!first) {
cerr << ", ";
} else {
first = false;
}
debug_print(x);
}
cerr << "}";
}
void debug_print_collection() {
cerr << endl;
}
template<typename First, typename... Args>
void debug_print_collection(First val, Args... args) {
cerr << " ";
debug_print(val);
debug_print_collection(args...);
}
#define debug(...) cerr << "@@@ " << #__VA_ARGS__ << " ="; debug_print_collection(__VA_ARGS__);
#else
#define debug(...)
#endif
typedef long double ld;
const int MOD = 1000000007;
#define ff first
#define ss second
const int inf = (int)1e9;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
using pll = pair<ll, ll>;
// *-> KISS*
int solve() {
int n, s; cin >> n >> s;
vector<string> v(n);
for(int i = 0; i < n; ++i) cin >> v[i];
pll start;
vector<pll> bees;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
if(v[i][j] == 'M') start = {i, j};
else if(v[i][j] == 'H') bees.push_back({i, j});
}
}
// Valid function for mecho
auto mvalid = [&](int i, int j) {
return i >= 0 && j >= 0 && i < n && j < n && v[i][j] != 'T';
};
// valid function for bees
auto bvalid = [&](int i, int j) {
return i >= 0 && j >= 0 && i < n && j < n && v[i][j] != 'T' && v[i][j] != 'D';
};
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int ans = -1;
int si = 0, ei = 1000000;
while(si <= ei) {
int mid = (si + ei) >> 1;
// We are assuming that Mecho will stay at it's place for mid minutes and then start moving towards
// his home
vector<vector<bool>> bvis(n, vector<bool>(n, false)),
mvis(n, vector<bool>(n, false));
vector<pll> last;
vector<vector<int>> steps(n, vector<int>(n, inf));
{
queue<array<ll, 3>> q;
for(int i = 0; i < sz(bees); ++i) {
q.push({bees[i].ff, bees[i].ss, 0});
bvis[bees[i].ff][bees[i].ss] = true;
}
while(!q.empty()) {
array<ll, 3> t = q.front(); q.pop();
steps[t[0]][t[1]] = 0;
if(t[2] == mid) {
last.push_back({t[0], t[1]});
continue;
}
int x = t[0], y = t[1], step = t[2];
for(int i = 0; i < 4; ++i) {
int ni = x + dx[i], nj = y + dy[i];
if(bvalid(ni, nj) && !bvis[ni][nj]) {
bvis[ni][nj] = true;
q.push({ni, nj, step + 1});
}
}
}
}
queue<array<ll, 3>> q;
for(int i = 0; i < sz(last); ++i) {
q.push({last[i].ff, last[i].ss, 0});
}
while(!q.empty()) {
array<ll, 3> _ = q.front(); q.pop();
int x = _[0], y = _[1], step = _[2];
steps[x][y] = step * s;
for(int i = 0; i < 4; ++i) {
int ni = x + dx[i], nj = y + dy[i];
if(bvalid(ni, nj) && !bvis[ni][nj]) {
q.push({ni, nj, step + 1});
bvis[ni][nj] = true;
}
}
}
/* debug(steps); */
/* q = queue<array<ll, 3>> (); */
while(!q.empty()) q.pop();
if(steps[start.ff][start.ss] != 0) q.push({start.ff, start.ss, 0LL});
mvis[start.ff][start.ss] = true;
ll minutes = -1; // -1 means unreachability
while(!q.empty()) {
array<ll, 3> t = q.front(); q.pop();
int x = t[0], y = t[1], step = t[2];
if(v[x][y] == 'D') {
minutes = step;
break;
}
for(int i = 0; i < 4; ++i) {
int ni = x + dx[i], nj = y + dy[i];
if(mvalid(ni, nj) && !mvis[ni][nj] && steps[ni][nj] > (step + 1)) {
mvis[ni][nj] = true;
q.push({ni, nj, step + 1});
}
}
}
if(minutes != -1) {
ans = mid; si = mid + 1;
}
else ei = mid - 1;
}
cout << ans;
return 0;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
bool test = false;
int TET = 1;
if(test) cin >> TET;
cout << fixed << setprecision(6);
for (int i = 1; i <= TET; i++) {
#ifdef LOCAL
cout << "##################" << '\n';
#endif
if (solve()) {
break;
}
cout << '\n';
}
#ifdef LOCAL
cout << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
return 0;
}
// -> Keep It Simple Stupid!
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
405 ms |
4512 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
356 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
368 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
36 ms |
1028 KB |
Output is correct |
34 |
Correct |
34 ms |
1076 KB |
Output is correct |
35 |
Correct |
64 ms |
1028 KB |
Output is correct |
36 |
Correct |
46 ms |
1224 KB |
Output is correct |
37 |
Correct |
41 ms |
1236 KB |
Output is correct |
38 |
Correct |
82 ms |
1236 KB |
Output is correct |
39 |
Correct |
57 ms |
1444 KB |
Output is correct |
40 |
Correct |
52 ms |
1492 KB |
Output is correct |
41 |
Correct |
101 ms |
1476 KB |
Output is correct |
42 |
Correct |
75 ms |
1816 KB |
Output is correct |
43 |
Correct |
67 ms |
1728 KB |
Output is correct |
44 |
Correct |
131 ms |
1724 KB |
Output is correct |
45 |
Correct |
87 ms |
1972 KB |
Output is correct |
46 |
Correct |
84 ms |
1980 KB |
Output is correct |
47 |
Correct |
162 ms |
2008 KB |
Output is correct |
48 |
Correct |
102 ms |
2308 KB |
Output is correct |
49 |
Correct |
91 ms |
2312 KB |
Output is correct |
50 |
Correct |
190 ms |
2344 KB |
Output is correct |
51 |
Correct |
121 ms |
2656 KB |
Output is correct |
52 |
Correct |
106 ms |
2644 KB |
Output is correct |
53 |
Correct |
217 ms |
2644 KB |
Output is correct |
54 |
Correct |
144 ms |
2988 KB |
Output is correct |
55 |
Correct |
134 ms |
3004 KB |
Output is correct |
56 |
Correct |
257 ms |
3020 KB |
Output is correct |
57 |
Correct |
161 ms |
3376 KB |
Output is correct |
58 |
Correct |
144 ms |
3384 KB |
Output is correct |
59 |
Correct |
297 ms |
3432 KB |
Output is correct |
60 |
Correct |
173 ms |
3796 KB |
Output is correct |
61 |
Correct |
160 ms |
3784 KB |
Output is correct |
62 |
Correct |
344 ms |
3836 KB |
Output is correct |
63 |
Correct |
443 ms |
3796 KB |
Output is correct |
64 |
Correct |
502 ms |
3820 KB |
Output is correct |
65 |
Correct |
488 ms |
3812 KB |
Output is correct |
66 |
Correct |
481 ms |
3804 KB |
Output is correct |
67 |
Correct |
428 ms |
3800 KB |
Output is correct |
68 |
Correct |
418 ms |
4000 KB |
Output is correct |
69 |
Correct |
436 ms |
4032 KB |
Output is correct |
70 |
Correct |
405 ms |
3992 KB |
Output is correct |
71 |
Correct |
406 ms |
3892 KB |
Output is correct |
72 |
Correct |
396 ms |
3796 KB |
Output is correct |
73 |
Correct |
403 ms |
5980 KB |
Output is correct |
74 |
Correct |
387 ms |
4664 KB |
Output is correct |
75 |
Correct |
403 ms |
4888 KB |
Output is correct |
76 |
Correct |
392 ms |
4636 KB |
Output is correct |
77 |
Correct |
407 ms |
4840 KB |
Output is correct |
78 |
Correct |
381 ms |
5284 KB |
Output is correct |
79 |
Correct |
392 ms |
4736 KB |
Output is correct |
80 |
Correct |
384 ms |
4564 KB |
Output is correct |
81 |
Correct |
403 ms |
4676 KB |
Output is correct |
82 |
Correct |
364 ms |
4564 KB |
Output is correct |
83 |
Correct |
444 ms |
5112 KB |
Output is correct |
84 |
Correct |
407 ms |
4420 KB |
Output is correct |
85 |
Correct |
422 ms |
4436 KB |
Output is correct |
86 |
Correct |
411 ms |
4720 KB |
Output is correct |
87 |
Correct |
418 ms |
4436 KB |
Output is correct |
88 |
Correct |
428 ms |
4864 KB |
Output is correct |
89 |
Correct |
411 ms |
4300 KB |
Output is correct |
90 |
Correct |
424 ms |
4908 KB |
Output is correct |
91 |
Correct |
418 ms |
4288 KB |
Output is correct |
92 |
Correct |
428 ms |
5064 KB |
Output is correct |