// Source: https://usaco.guide/general/io
#include <bits/stdc++.h>
#define ll int
#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));
auto bfs1 = [&](pair<ll, ll> cord){
hpath[cord.ff][cord.ss]=1;
vector<vector<ll>> vi(n, vector<ll>(n, INF));
queue<pair<ll, ll>> q;
q.push(cord);
vi[cord.ff][cord.ss]=1;
while (!q.empty()){
pair<ll, ll> cur = q.front();
q.pop();
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]!='D' and gr[cur.ff-1][cur.ss]!='H'){
vi[cur.ff-1][cur.ss]=vi[cur.ff][cur.ss]+1;
hpath[cur.ff-1][cur.ss]=min(hpath[cur.ff-1][cur.ss], vi[cur.ff-1][cur.ss]);
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]!='D' and gr[cur.ff][cur.ss-1]!='H'){
vi[cur.ff][cur.ss-1]=vi[cur.ff][cur.ss]+1;
hpath[cur.ff][cur.ss-1]=min(hpath[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]!='D' and gr[cur.ff+1][cur.ss]!='H'){
vi[cur.ff+1][cur.ss]=vi[cur.ff][cur.ss]+1;
hpath[cur.ff+1][cur.ss]=min(hpath[cur.ff+1][cur.ss], vi[cur.ff+1][cur.ss]);
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]!='D' and gr[cur.ff][cur.ss+1]!='H'){
vi[cur.ff][cur.ss+1]=vi[cur.ff][cur.ss]+1;
hpath[cur.ff][cur.ss+1]=min(hpath[cur.ff][cur.ss+1], vi[cur.ff][cur.ss+1]);
q.push({cur.ff, cur.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;
};
for (auto hive:hvs){
bfs1(hive);
}
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 |
212 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
300 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Execution timed out |
1063 ms |
6656 KB |
Time limit exceeded |
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 |
0 ms |
212 KB |
Output is correct |
12 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
13 |
Incorrect |
6 ms |
212 KB |
Output isn't correct |
14 |
Correct |
9 ms |
340 KB |
Output is correct |
15 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
16 |
Correct |
1 ms |
300 KB |
Output is correct |
17 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Incorrect |
1 ms |
212 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 |
244 KB |
Output is correct |
23 |
Incorrect |
1 ms |
304 KB |
Output isn't correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
26 |
Correct |
1 ms |
296 KB |
Output is correct |
27 |
Incorrect |
1 ms |
300 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 |
340 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 |
8 ms |
1520 KB |
Output isn't correct |
34 |
Correct |
11 ms |
1548 KB |
Output is correct |
35 |
Correct |
23 ms |
1492 KB |
Output is correct |
36 |
Incorrect |
12 ms |
1844 KB |
Output isn't correct |
37 |
Correct |
13 ms |
1876 KB |
Output is correct |
38 |
Correct |
23 ms |
1912 KB |
Output is correct |
39 |
Incorrect |
18 ms |
2348 KB |
Output isn't correct |
40 |
Correct |
18 ms |
2316 KB |
Output is correct |
41 |
Correct |
33 ms |
2316 KB |
Output is correct |
42 |
Incorrect |
16 ms |
2836 KB |
Output isn't correct |
43 |
Correct |
20 ms |
2884 KB |
Output is correct |
44 |
Correct |
46 ms |
2796 KB |
Output is correct |
45 |
Incorrect |
20 ms |
3292 KB |
Output isn't correct |
46 |
Correct |
23 ms |
3336 KB |
Output is correct |
47 |
Correct |
47 ms |
3300 KB |
Output is correct |
48 |
Incorrect |
24 ms |
3880 KB |
Output isn't correct |
49 |
Correct |
28 ms |
3868 KB |
Output is correct |
50 |
Correct |
52 ms |
3796 KB |
Output is correct |
51 |
Incorrect |
27 ms |
4540 KB |
Output isn't correct |
52 |
Correct |
32 ms |
4536 KB |
Output is correct |
53 |
Correct |
71 ms |
4484 KB |
Output is correct |
54 |
Incorrect |
32 ms |
5176 KB |
Output isn't correct |
55 |
Correct |
38 ms |
5188 KB |
Output is correct |
56 |
Correct |
75 ms |
5120 KB |
Output is correct |
57 |
Incorrect |
44 ms |
5904 KB |
Output isn't correct |
58 |
Correct |
49 ms |
5928 KB |
Output is correct |
59 |
Correct |
86 ms |
5872 KB |
Output is correct |
60 |
Incorrect |
41 ms |
6680 KB |
Output isn't correct |
61 |
Correct |
48 ms |
6708 KB |
Output is correct |
62 |
Correct |
98 ms |
6644 KB |
Output is correct |
63 |
Execution timed out |
1084 ms |
6660 KB |
Time limit exceeded |
64 |
Execution timed out |
1065 ms |
6656 KB |
Time limit exceeded |
65 |
Execution timed out |
1083 ms |
6784 KB |
Time limit exceeded |
66 |
Execution timed out |
1080 ms |
6712 KB |
Time limit exceeded |
67 |
Execution timed out |
1090 ms |
6772 KB |
Time limit exceeded |
68 |
Execution timed out |
1076 ms |
6680 KB |
Time limit exceeded |
69 |
Execution timed out |
1078 ms |
6700 KB |
Time limit exceeded |
70 |
Execution timed out |
1058 ms |
6756 KB |
Time limit exceeded |
71 |
Execution timed out |
1034 ms |
6676 KB |
Time limit exceeded |
72 |
Execution timed out |
1084 ms |
6656 KB |
Time limit exceeded |
73 |
Execution timed out |
1075 ms |
6916 KB |
Time limit exceeded |
74 |
Execution timed out |
1065 ms |
6708 KB |
Time limit exceeded |
75 |
Execution timed out |
1077 ms |
6696 KB |
Time limit exceeded |
76 |
Execution timed out |
1075 ms |
6772 KB |
Time limit exceeded |
77 |
Execution timed out |
1076 ms |
6736 KB |
Time limit exceeded |
78 |
Execution timed out |
1037 ms |
6676 KB |
Time limit exceeded |
79 |
Execution timed out |
1037 ms |
6824 KB |
Time limit exceeded |
80 |
Execution timed out |
1083 ms |
6704 KB |
Time limit exceeded |
81 |
Execution timed out |
1079 ms |
6732 KB |
Time limit exceeded |
82 |
Execution timed out |
1067 ms |
6768 KB |
Time limit exceeded |
83 |
Execution timed out |
1076 ms |
6796 KB |
Time limit exceeded |
84 |
Execution timed out |
1080 ms |
6824 KB |
Time limit exceeded |
85 |
Execution timed out |
1044 ms |
6672 KB |
Time limit exceeded |
86 |
Execution timed out |
1080 ms |
6668 KB |
Time limit exceeded |
87 |
Execution timed out |
1074 ms |
6560 KB |
Time limit exceeded |
88 |
Execution timed out |
1065 ms |
6612 KB |
Time limit exceeded |
89 |
Execution timed out |
1070 ms |
6604 KB |
Time limit exceeded |
90 |
Execution timed out |
1078 ms |
6788 KB |
Time limit exceeded |
91 |
Execution timed out |
1080 ms |
6672 KB |
Time limit exceeded |
92 |
Execution timed out |
1065 ms |
6660 KB |
Time limit exceeded |