// Source: https://usaco.guide/general/io
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ln "\n"
#define ff first
#define ss second
#define INF 2e9
using namespace std;
void solve(){
ll n, s; cin >> n >> s;
vector<vector<char>> gr(n, vector<char>(n));
vector<pair<ll, ll>> hvs;
pair<ll, ll> bcor, hcor;
for (ll i=0; i<n; i++){
for (ll j=0; j<n; j++){
cin >> gr[i][j];
if (gr[i][j]=='H'){
hvs.push_back({i, j});
}else if (gr[i][j]=='M'){
bcor = {i, j};
}else if (gr[i][j]=='D'){
hcor = {i, j};
}
}
}
vector<vector<ll>> hpath(n, vector<ll>(n, INF));
//Generate HPATH in O(N^2)
queue<pair<ll, ll>> q;
for (auto hive:hvs){
hpath[hive.ff][hive.ss]=1;
q.push({hive.ff, hive.ss});
}
while (!q.empty()){
pair<ll, ll> hive = q.front();
q.pop();
long long xsh, ysh;
xsh=1; ysh=0;
if ((hive.ff+xsh<n and hive.ff+xsh>=0) and hpath[hive.ff+xsh][hive.ss+ysh]==INF and (hive.ss+ysh<n and hive.ss+ysh>=0) and (gr[hive.ff+xsh][hive.ss+ysh]=='G' or gr[hive.ff+xsh][hive.ss+ysh]=='M')){
q.push({hive.ff+xsh, hive.ss+ysh});
hpath[hive.ff+xsh][hive.ss+ysh]=hpath[hive.ff][hive.ss]+1;
}
xsh=-1, ysh=0;
if ((hive.ff+xsh<n and hive.ff+xsh>=0) and hpath[hive.ff+xsh][hive.ss+ysh]==INF and (hive.ss+ysh<n and hive.ss+ysh>=0) and (gr[hive.ff+xsh][hive.ss+ysh]=='G' or gr[hive.ff+xsh][hive.ss+ysh]=='M')){
q.push({hive.ff+xsh, hive.ss+ysh});
hpath[hive.ff+xsh][hive.ss+ysh]=hpath[hive.ff][hive.ss]+1;
}
xsh=0; ysh=1;
if ((hive.ff+xsh<n and hive.ff+xsh>=0) and hpath[hive.ff+xsh][hive.ss+ysh]==INF and (hive.ss+ysh<n and hive.ss+ysh>=0) and (gr[hive.ff+xsh][hive.ss+ysh]=='G' or gr[hive.ff+xsh][hive.ss+ysh]=='M')){
q.push({hive.ff+xsh, hive.ss+ysh});
hpath[hive.ff+xsh][hive.ss+ysh]=hpath[hive.ff][hive.ss]+1;
}
xsh=0; ysh=-1;
if ((hive.ff+xsh<n and hive.ff+xsh>=0) and hpath[hive.ff+xsh][hive.ss+ysh]==INF and (hive.ss+ysh<n and hive.ss+ysh>=0) and (gr[hive.ff+xsh][hive.ss+ysh]=='G' or gr[hive.ff+xsh][hive.ss+ysh]=='M')){
q.push({hive.ff+xsh, hive.ss+ysh});
hpath[hive.ff+xsh][hive.ss+ysh]=hpath[hive.ff][hive.ss]+1;
}
}
auto bfs2 = [&](ll wt) -> bool{
pair<ll, ll> cord = bcor;
if (hpath[cord.ff][cord.ss]-1<wt) {
// cout << "Fail at start: " << hpath[cord.ff][cord.ss] << " <: " << cord.ff << "," << cord.ss << " -> " << wt << ln;
return 0;
}
vector<vector<ll>> vi(n, vector<ll>(n, INF));
queue<pair<ll, ll>> q;
q.push(cord);
vi[cord.ff][cord.ss]=wt;
while (!q.empty()){
pair<ll, ll> cur = q.front();
q.pop();
// cout << "(" << cur.ss << "," << cur.ff << ") -> hpath=" << hpath[cur.ff][cur.ss] << "; mdist=" << vi[cur.ff][cur.ss]-wt << "; enters at=" << (ceil((ld)(vi[cur.ff][cur.ss]-wt)/(ld)s)+wt) << "; " << " bee comes in=" << hpath[cur.ff][cur.ss] << ln;
if (cur.ff>0 and vi[cur.ff-1][cur.ss]==INF and gr[cur.ff-1][cur.ss]!='T' and gr[cur.ff-1][cur.ss]!='H' and (ceil((ld)(vi[cur.ff][cur.ss]-wt+1)/(ld)s)+wt)<(hpath[cur.ff-1][cur.ss])){
// cout << (hpath[cur.ff-1][cur.ss]-(vi[cur.ff][cur.ss]-wt)/s-1) << " <- " << cur.ss << "," << cur.ff << " to U: cmx " << vi[cur.ff][cur.ss] << " Dist from bear: " << vi[cur.ff][cur.ss]-wt+1 << ln;
vi[cur.ff-1][cur.ss]=vi[cur.ff][cur.ss]+1;
q.push({cur.ff-1, cur.ss});
}
if (cur.ss>0 and vi[cur.ff][cur.ss-1]==INF and gr[cur.ff][cur.ss-1]!='T' and gr[cur.ff][cur.ss-1]!='H' and (ceil((ld)(vi[cur.ff][cur.ss]-wt+1)/(ld)s)+wt)<(hpath[cur.ff][cur.ss-1])){
// cout << (hpath[cur.ff][cur.ss-1]-(vi[cur.ff][cur.ss]-wt)/s-1) << " <- " << cur.ss << "," << cur.ff << " to L: cmx " << vi[cur.ff][cur.ss] << " Dist from bear: " << vi[cur.ff][cur.ss]-wt+1 << ln;
vi[cur.ff][cur.ss-1]=vi[cur.ff][cur.ss]+1;
q.push({cur.ff, cur.ss-1});
}
if (cur.ff+1<n and vi[cur.ff+1][cur.ss]==INF and gr[cur.ff+1][cur.ss]!='T' and gr[cur.ff+1][cur.ss]!='H' and (ceil((ld)(vi[cur.ff][cur.ss]-wt+1)/(ld)s)+wt)<(hpath[cur.ff+1][cur.ss])){
// cout << (hpath[cur.ff+1][cur.ss]-(vi[cur.ff][cur.ss]-wt)/s-1) << " <- " << cur.ss << "," << cur.ff << " to D: cmx " << vi[cur.ff][cur.ss] << " Dist from bear: " << vi[cur.ff][cur.ss]-wt+1 << ln;
vi[cur.ff+1][cur.ss]=vi[cur.ff][cur.ss]+1;
q.push({cur.ff+1, cur.ss});
}
if (cur.ss+1<n and vi[cur.ff][cur.ss+1]==INF and gr[cur.ff][cur.ss+1]!='T' and gr[cur.ff][cur.ss+1]!='H' and (ceil((ld)(vi[cur.ff][cur.ss]-wt+1)/(ld)s)+wt)<(hpath[cur.ff][cur.ss+1])){
// cout << (hpath[cur.ff][cur.ss+1]-(vi[cur.ff][cur.ss]+1-wt)/s-1) << " <- " << cur.ss << "," << cur.ff << " to R: cmx " << vi[cur.ff][cur.ss] << " Dist from bear: " << vi[cur.ff][cur.ss]-wt+1 << ln;
vi[cur.ff][cur.ss+1]=vi[cur.ff][cur.ss]+1;
q.push({cur.ff, cur.ss+1});
}
}
if (vi[hcor.ff][hcor.ss]<INF) return 1;
else return 0;
};
ll l=-1, r=n*n+1;
while (l+1<r){
ll mid = (l+r)/2;
if (bfs2(mid)) {
l=mid;
// cout << mid << " with T" << ln;
}
else {
r=mid;
// cout << mid << " with F" << ln;
}
}
cout << l << ln;
}
int main() {
ll t=1;
// cin >> t;
while (t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
304 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
81 ms |
11728 KB |
Output is correct |
8 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
16 |
Correct |
1 ms |
296 KB |
Output is correct |
17 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Incorrect |
1 ms |
300 KB |
Output isn't correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
26 |
Correct |
1 ms |
296 KB |
Output is correct |
27 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
30 |
Correct |
1 ms |
304 KB |
Output is correct |
31 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Incorrect |
10 ms |
2492 KB |
Output isn't correct |
34 |
Correct |
9 ms |
2496 KB |
Output is correct |
35 |
Correct |
19 ms |
2492 KB |
Output is correct |
36 |
Incorrect |
12 ms |
3160 KB |
Output isn't correct |
37 |
Correct |
14 ms |
3348 KB |
Output is correct |
38 |
Correct |
25 ms |
3176 KB |
Output is correct |
39 |
Incorrect |
16 ms |
3908 KB |
Output isn't correct |
40 |
Correct |
15 ms |
3912 KB |
Output is correct |
41 |
Correct |
34 ms |
3924 KB |
Output is correct |
42 |
Incorrect |
19 ms |
4788 KB |
Output isn't correct |
43 |
Correct |
19 ms |
4808 KB |
Output is correct |
44 |
Correct |
41 ms |
4772 KB |
Output is correct |
45 |
Incorrect |
24 ms |
5712 KB |
Output isn't correct |
46 |
Correct |
22 ms |
5692 KB |
Output is correct |
47 |
Correct |
60 ms |
5700 KB |
Output is correct |
48 |
Incorrect |
29 ms |
6724 KB |
Output isn't correct |
49 |
Correct |
27 ms |
6744 KB |
Output is correct |
50 |
Correct |
57 ms |
6716 KB |
Output is correct |
51 |
Incorrect |
39 ms |
7844 KB |
Output isn't correct |
52 |
Correct |
33 ms |
7804 KB |
Output is correct |
53 |
Correct |
82 ms |
7836 KB |
Output is correct |
54 |
Incorrect |
43 ms |
9024 KB |
Output isn't correct |
55 |
Correct |
39 ms |
9076 KB |
Output is correct |
56 |
Correct |
83 ms |
9024 KB |
Output is correct |
57 |
Incorrect |
45 ms |
10380 KB |
Output isn't correct |
58 |
Correct |
45 ms |
10412 KB |
Output is correct |
59 |
Correct |
119 ms |
10280 KB |
Output is correct |
60 |
Incorrect |
50 ms |
11768 KB |
Output isn't correct |
61 |
Correct |
47 ms |
11712 KB |
Output is correct |
62 |
Correct |
126 ms |
11792 KB |
Output is correct |
63 |
Correct |
171 ms |
11684 KB |
Output is correct |
64 |
Correct |
265 ms |
11688 KB |
Output is correct |
65 |
Correct |
200 ms |
11764 KB |
Output is correct |
66 |
Incorrect |
172 ms |
11684 KB |
Output isn't correct |
67 |
Correct |
157 ms |
11732 KB |
Output is correct |
68 |
Correct |
82 ms |
11728 KB |
Output is correct |
69 |
Correct |
70 ms |
11732 KB |
Output is correct |
70 |
Correct |
55 ms |
11784 KB |
Output is correct |
71 |
Correct |
62 ms |
11740 KB |
Output is correct |
72 |
Incorrect |
46 ms |
11740 KB |
Output isn't correct |
73 |
Incorrect |
45 ms |
11756 KB |
Output isn't correct |
74 |
Correct |
62 ms |
11764 KB |
Output is correct |
75 |
Correct |
75 ms |
11788 KB |
Output is correct |
76 |
Correct |
66 ms |
11752 KB |
Output is correct |
77 |
Correct |
81 ms |
11772 KB |
Output is correct |
78 |
Correct |
77 ms |
11772 KB |
Output is correct |
79 |
Correct |
60 ms |
11804 KB |
Output is correct |
80 |
Correct |
65 ms |
11756 KB |
Output is correct |
81 |
Correct |
72 ms |
11752 KB |
Output is correct |
82 |
Correct |
70 ms |
11760 KB |
Output is correct |
83 |
Correct |
90 ms |
11748 KB |
Output is correct |
84 |
Correct |
75 ms |
11768 KB |
Output is correct |
85 |
Correct |
95 ms |
11756 KB |
Output is correct |
86 |
Correct |
81 ms |
11732 KB |
Output is correct |
87 |
Correct |
106 ms |
11740 KB |
Output is correct |
88 |
Correct |
105 ms |
11712 KB |
Output is correct |
89 |
Correct |
81 ms |
11732 KB |
Output is correct |
90 |
Correct |
111 ms |
11724 KB |
Output is correct |
91 |
Correct |
90 ms |
11740 KB |
Output is correct |
92 |
Correct |
92 ms |
11744 KB |
Output is correct |