/*
* Author: Nonoze
* Created: Wednesday 07/08/2024
*/
#include <bits/stdc++.h>
using namespace std;
#ifndef _IN_LOCAL
#define dbg(...)
#endif
#define endl '\n'
#define endlfl '\n' << flush
#define quit(x) return (void)(cout << x << endl)
template<typename T> void read(T& x, bool write=0, bool write2=1, bool nothing=1) { if (write && write2) cout << x << endl; else if (write) cout << x << ' '; else cin >> x;}
template<typename T1, typename T2> void read(pair<T1, T2>& p, bool write=0, bool nothing=1, bool write2=1) { read(p.first, write, 0), read(p.second, write, 0); if (write && write2) cout << endl;}
template<typename T> void read(vector<T>& v, bool write=0) { for (auto& x : v) read(x, write, 0, 1); if (write) cout << endl; }
template<typename T1, typename T2> void read(T1& x, T2& y, bool write=0) { read(x, write), read(y, write); if (write) cout << endl; }
template<typename T1, typename T2, typename T3> void read(T1& x, T2& y, T3& z, bool write=0) { read(x, write), read(y, write), read(z, write); if (write) cout << endl; }
template<typename T1, typename T2, typename T3, typename T4> void read(T1& x, T2& y, T3& z, T4& zz, bool write=0) { read(x, write), read(y, write), read(z, write), read(zz, write); if (write) cout << endl; }
template<typename T> void print(T x) { read(x, 1); }
template<typename T1, typename T2> void print(T1 x, T2 y) { read(x, y, 1); }
template<typename T1, typename T2, typename T3> void print(T1 x, T2 y, T3 z) { read(x, y, z, 1); }
template<typename T1, typename T2, typename T3, typename T4> void print(T1 x, T2 y, T3 z, T4 zz) { read(x, y, z, zz, 1); }
#define sz(x) (int)(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b)
#define int long long
const int inf = numeric_limits<int>::max() / 4;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7, LOG=25;
void solve();
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tt=1;
// cin >> tt;
while(tt--) solve();
return 0;
}
int n, k, s, t;
vector<int> h;
vector<vector<int>> adj;
bool ok(int wait) {
vector<bool> bees(n, 0), visited(n, 0); visited[s]=1;
for (auto u: h) bees[u]=1;
queue<pair<int, pair<int, int>>> qbees;
queue<pair<int, pair<int, int>>> q2; q2.push({s, {0, 0}});
for (auto u: h) qbees.push({u, {0, 0}});
while (!q2.empty()) {
bool bee=1;
if (qbees.empty() || qbees.front().se.se>=q2.front().se.se) bee=0, swap(qbees, q2);
int u=qbees.front().fi, nb=qbees.front().se.fi, time=qbees.front().se.se; qbees.pop();
if (!bee) swap(qbees, q2);
if (!bee && time<wait) {
if (!bees[u]) q2.push({u, {0, time+1}});
continue;
}
if (bees[u] && !bee) continue;
for (auto v: adj[u]) {
if (bees[v] || (!bee && visited[v])) continue;
if (bee && t==v) continue;
if (bee) bees[v]=1;
else visited[v]=1;
if (!bee && v==t) return 1;
if (!bee) {
if (nb+1>=k) q2.push({v, {0, time+1}});
else q2.push({v, {nb+1, time}});
} else qbees.push({v, {0, time+1}});
}
}
return 0;
}
void solve() {
read(n, k);
vector<string> a(n); read(a);
adj.clear(), adj.resize(n*n);
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
if (a[i][j]=='T') continue;
int act=i*n+j;
if (i && a[i-1][j]!='T') {
int nxt=(i-1)*n+j;
adj[act].pb(nxt), adj[nxt].pb(act);
}
if (j && a[i][j-1]!='T') {
int nxt=i*n+j-1;
adj[act].pb(nxt), adj[nxt].pb(act);
}
if (a[i][j]=='M') s=act;
if (a[i][j]=='D') t=act;
if (a[i][j]=='H') h.pb(act);
}
}
n*=n;
int ans=-1, l=0, r=n;
while (l<=r) {
int mid=(l+r)/2;
if (ok(mid)) ans=mid, l=mid+1;
else r=mid-1;
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
677 ms |
46700 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
664 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
32 ms |
5420 KB |
Output is correct |
34 |
Correct |
22 ms |
5212 KB |
Output is correct |
35 |
Correct |
34 ms |
7260 KB |
Output is correct |
36 |
Correct |
42 ms |
6748 KB |
Output is correct |
37 |
Correct |
31 ms |
6744 KB |
Output is correct |
38 |
Correct |
37 ms |
9304 KB |
Output is correct |
39 |
Correct |
53 ms |
8540 KB |
Output is correct |
40 |
Correct |
37 ms |
8540 KB |
Output is correct |
41 |
Correct |
51 ms |
11612 KB |
Output is correct |
42 |
Correct |
68 ms |
10536 KB |
Output is correct |
43 |
Correct |
47 ms |
10588 KB |
Output is correct |
44 |
Correct |
58 ms |
14468 KB |
Output is correct |
45 |
Correct |
88 ms |
12636 KB |
Output is correct |
46 |
Correct |
72 ms |
12632 KB |
Output is correct |
47 |
Correct |
69 ms |
17244 KB |
Output is correct |
48 |
Correct |
102 ms |
14940 KB |
Output is correct |
49 |
Correct |
70 ms |
14940 KB |
Output is correct |
50 |
Correct |
83 ms |
20572 KB |
Output is correct |
51 |
Correct |
120 ms |
17496 KB |
Output is correct |
52 |
Correct |
85 ms |
17496 KB |
Output is correct |
53 |
Correct |
107 ms |
23896 KB |
Output is correct |
54 |
Correct |
140 ms |
20312 KB |
Output is correct |
55 |
Correct |
108 ms |
20056 KB |
Output is correct |
56 |
Correct |
134 ms |
27728 KB |
Output is correct |
57 |
Correct |
160 ms |
23132 KB |
Output is correct |
58 |
Correct |
110 ms |
23132 KB |
Output is correct |
59 |
Correct |
169 ms |
31824 KB |
Output is correct |
60 |
Correct |
209 ms |
26212 KB |
Output is correct |
61 |
Correct |
128 ms |
26200 KB |
Output is correct |
62 |
Correct |
172 ms |
36316 KB |
Output is correct |
63 |
Correct |
206 ms |
29788 KB |
Output is correct |
64 |
Correct |
314 ms |
29776 KB |
Output is correct |
65 |
Correct |
286 ms |
29808 KB |
Output is correct |
66 |
Correct |
228 ms |
29776 KB |
Output is correct |
67 |
Correct |
277 ms |
29692 KB |
Output is correct |
68 |
Correct |
235 ms |
29784 KB |
Output is correct |
69 |
Correct |
210 ms |
29776 KB |
Output is correct |
70 |
Correct |
210 ms |
29968 KB |
Output is correct |
71 |
Correct |
284 ms |
29784 KB |
Output is correct |
72 |
Correct |
305 ms |
29896 KB |
Output is correct |
73 |
Correct |
91 ms |
46744 KB |
Output is correct |
74 |
Correct |
522 ms |
46928 KB |
Output is correct |
75 |
Correct |
633 ms |
46928 KB |
Output is correct |
76 |
Correct |
615 ms |
47060 KB |
Output is correct |
77 |
Correct |
573 ms |
46928 KB |
Output is correct |
78 |
Correct |
620 ms |
46880 KB |
Output is correct |
79 |
Correct |
609 ms |
46832 KB |
Output is correct |
80 |
Correct |
618 ms |
46924 KB |
Output is correct |
81 |
Correct |
657 ms |
47188 KB |
Output is correct |
82 |
Correct |
639 ms |
46832 KB |
Output is correct |
83 |
Correct |
760 ms |
46668 KB |
Output is correct |
84 |
Correct |
763 ms |
46608 KB |
Output is correct |
85 |
Correct |
753 ms |
46600 KB |
Output is correct |
86 |
Correct |
845 ms |
46628 KB |
Output is correct |
87 |
Correct |
777 ms |
46600 KB |
Output is correct |
88 |
Correct |
728 ms |
46752 KB |
Output is correct |
89 |
Correct |
589 ms |
46580 KB |
Output is correct |
90 |
Correct |
635 ms |
46604 KB |
Output is correct |
91 |
Correct |
607 ms |
46564 KB |
Output is correct |
92 |
Correct |
725 ms |
46608 KB |
Output is correct |