#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define open(g) string _name_ = g; freopen((_name_ + ".in").c_str(), "r", stdin); freopen((_name_ + ".out").c_str(), "w", stdout)
#define printArr(a, len) for(int asdf = 0; asdf < (len); ++asdf) cout << (a)[asdf] << ' '; cout << '\n';
#define boost() cin.sync_with_stdio(0); cin.tie(0)
#define forR(i, x) for(int i = 0; i < x; ++i)
#define REP(i, a, b) for(int i = (a); i < (b); ++i)
#define all(i) i.begin(), i.end()
#define pii pair<int, int>
#define vi vector<int>
#define si set<int>
#define usi unordered_set<int>
#define mii map<int, int>
#define umii unordered_map<int, int>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
const int MN = 810, INF = 1e9 + 10;
struct coord{int r, c;};
struct bear{int r, c, sl;};
bool vis[MN][MN];
int bee[MN][MN], ber[MN][MN]; // minimum # of minutes needed to end here
char grid[MN][MN];
int dir[4][2] = {{1, 0}, {0, 1}, {0, -1}, {-1, 0}};
int n, s;
int mr, mc;
bool ib(int r, int c){return 0 <= r && r < n && 0 <= c && c < n;}
bool iv(int r, int c){return ib(r, c) && grid[r][c] != 'T';}
// t = number of minutes bear eats
bool pos(int t){
if(bee[mr][mc] >= t) return false;
deque<bear> bfs = {{mr, mc, 0}};
forR(a, MN) forR(b, MN) vis[a][b] = false;
forR(a, MN) forR(b, MN) ber[a][b] = 0;
ber[mr][mc] = t;
vis[mr][mc] = true;
while(!bfs.empty()){
auto [r, c, sl] = bfs.front();
bfs.pop_front();
if(!(sl > 0 || ber[r][c] < bee[r][c])){
cout << r << ' ' << c << ' ' << sl << '\n';
cout << "bad";
}
if(grid[r][c] == 'D') {
return true;
}
for(auto [rc, cc] : dir){
auto [nr, nc] = pii{r+rc, c+cc};
int dis = ber[r][c] + (sl == 0);
int nsl = sl == 0 ? s - 1 : sl - 1;
// cout << r << ' ' << c << ' ' << sl << ' ' << nr << ' ' << nc << ' ' << dis << ' ' << bee[nr][nc] << '\n';
if(iv(nr, nc) && !vis[nr][nc] && (nsl != 0 || dis < bee[nr][nc])){
ber[nr][nc] = dis;
vis[nr][nc] = true;
bfs.push_back({nr, nc, nsl});
}
}
}
return false;
}
signed main() {
cin >> n >> s;
deque<coord> bfs;
forR(r, n) forR(c, n){
cin >> grid[r][c];
if(grid[r][c] == 'H') {
bfs.push_back({r, c});
vis[r][c] = true;
} else if(grid[r][c] == 'M'){
mr=r, mc=c;
}
}
while(!bfs.empty()){
auto [r, c] = bfs.front();
bfs.pop_front();
for(auto [cr, cc] : dir){
auto [nr, nc] = pii {r + cr, c + cc};
if(iv(nr, nc) && grid[nr][nc] != 'D' && !vis[nr][nc]){
vis[nr][nc] = true;
bee[nr][nc] = bee[r][c] + 1;
bfs.push_back({nr, nc});
}
}
}
forR(r, MN) forR(c, MN) if(!vis[r][c]) bee[r][c] = INF;
if(!pos(0)) cout << "-1\n";
else {
// TODO fix hi
int lo=0, hi=MN * MN;
while(hi - lo > 1){
int mid = (lo + hi) / 2;
if(pos(mid)) lo = mid;
else hi = mid;
}
cout << lo << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2772 KB |
Output isn't correct |
2 |
Incorrect |
2 ms |
2772 KB |
Output isn't correct |
3 |
Incorrect |
2 ms |
2772 KB |
Output isn't correct |
4 |
Incorrect |
2 ms |
2772 KB |
Output isn't correct |
5 |
Incorrect |
2 ms |
2796 KB |
Output isn't correct |
6 |
Incorrect |
2 ms |
2772 KB |
Output isn't correct |
7 |
Incorrect |
52 ms |
4216 KB |
Output isn't correct |
8 |
Correct |
3 ms |
2900 KB |
Output is correct |
9 |
Incorrect |
2 ms |
2900 KB |
Output isn't correct |
10 |
Incorrect |
2 ms |
2900 KB |
Output isn't correct |
11 |
Incorrect |
2 ms |
2900 KB |
Output isn't correct |
12 |
Incorrect |
3 ms |
2900 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
2900 KB |
Output isn't correct |
14 |
Correct |
2 ms |
2900 KB |
Output is correct |
15 |
Incorrect |
2 ms |
2900 KB |
Output isn't correct |
16 |
Incorrect |
2 ms |
2900 KB |
Output isn't correct |
17 |
Incorrect |
2 ms |
2900 KB |
Output isn't correct |
18 |
Incorrect |
2 ms |
2900 KB |
Output isn't correct |
19 |
Incorrect |
2 ms |
2900 KB |
Output isn't correct |
20 |
Incorrect |
2 ms |
2900 KB |
Output isn't correct |
21 |
Incorrect |
2 ms |
2900 KB |
Output isn't correct |
22 |
Incorrect |
3 ms |
2900 KB |
Output isn't correct |
23 |
Incorrect |
2 ms |
2900 KB |
Output isn't correct |
24 |
Incorrect |
3 ms |
2876 KB |
Output isn't correct |
25 |
Incorrect |
2 ms |
2900 KB |
Output isn't correct |
26 |
Incorrect |
3 ms |
2900 KB |
Output isn't correct |
27 |
Incorrect |
2 ms |
2900 KB |
Output isn't correct |
28 |
Incorrect |
3 ms |
2900 KB |
Output isn't correct |
29 |
Incorrect |
2 ms |
2900 KB |
Output isn't correct |
30 |
Incorrect |
2 ms |
2900 KB |
Output isn't correct |
31 |
Incorrect |
3 ms |
2900 KB |
Output isn't correct |
32 |
Incorrect |
3 ms |
2900 KB |
Output isn't correct |
33 |
Incorrect |
10 ms |
3368 KB |
Output isn't correct |
34 |
Incorrect |
10 ms |
3412 KB |
Output isn't correct |
35 |
Incorrect |
11 ms |
3384 KB |
Output isn't correct |
36 |
Incorrect |
12 ms |
3400 KB |
Output isn't correct |
37 |
Incorrect |
13 ms |
3492 KB |
Output isn't correct |
38 |
Incorrect |
13 ms |
3412 KB |
Output isn't correct |
39 |
Incorrect |
16 ms |
3472 KB |
Output isn't correct |
40 |
Incorrect |
16 ms |
3540 KB |
Output isn't correct |
41 |
Incorrect |
20 ms |
3540 KB |
Output isn't correct |
42 |
Incorrect |
21 ms |
3576 KB |
Output isn't correct |
43 |
Incorrect |
19 ms |
3668 KB |
Output isn't correct |
44 |
Incorrect |
20 ms |
3560 KB |
Output isn't correct |
45 |
Incorrect |
22 ms |
3720 KB |
Output isn't correct |
46 |
Incorrect |
24 ms |
3640 KB |
Output isn't correct |
47 |
Incorrect |
24 ms |
3668 KB |
Output isn't correct |
48 |
Incorrect |
24 ms |
3700 KB |
Output isn't correct |
49 |
Incorrect |
25 ms |
3756 KB |
Output isn't correct |
50 |
Incorrect |
27 ms |
3832 KB |
Output isn't correct |
51 |
Incorrect |
29 ms |
3892 KB |
Output isn't correct |
52 |
Incorrect |
28 ms |
3796 KB |
Output isn't correct |
53 |
Incorrect |
33 ms |
3868 KB |
Output isn't correct |
54 |
Incorrect |
35 ms |
3900 KB |
Output isn't correct |
55 |
Incorrect |
33 ms |
3968 KB |
Output isn't correct |
56 |
Incorrect |
36 ms |
3868 KB |
Output isn't correct |
57 |
Incorrect |
42 ms |
4052 KB |
Output isn't correct |
58 |
Incorrect |
38 ms |
4060 KB |
Output isn't correct |
59 |
Incorrect |
43 ms |
4112 KB |
Output isn't correct |
60 |
Incorrect |
45 ms |
4100 KB |
Output isn't correct |
61 |
Incorrect |
45 ms |
4120 KB |
Output isn't correct |
62 |
Incorrect |
47 ms |
4208 KB |
Output isn't correct |
63 |
Incorrect |
55 ms |
4104 KB |
Output isn't correct |
64 |
Incorrect |
55 ms |
4016 KB |
Output isn't correct |
65 |
Incorrect |
53 ms |
4060 KB |
Output isn't correct |
66 |
Incorrect |
59 ms |
4132 KB |
Output isn't correct |
67 |
Correct |
57 ms |
4120 KB |
Output is correct |
68 |
Incorrect |
64 ms |
4132 KB |
Output isn't correct |
69 |
Incorrect |
55 ms |
4088 KB |
Output isn't correct |
70 |
Incorrect |
54 ms |
4056 KB |
Output isn't correct |
71 |
Incorrect |
55 ms |
4044 KB |
Output isn't correct |
72 |
Incorrect |
67 ms |
4128 KB |
Output isn't correct |
73 |
Incorrect |
58 ms |
4412 KB |
Output isn't correct |
74 |
Incorrect |
55 ms |
4300 KB |
Output isn't correct |
75 |
Incorrect |
51 ms |
4372 KB |
Output isn't correct |
76 |
Incorrect |
51 ms |
4372 KB |
Output isn't correct |
77 |
Incorrect |
57 ms |
4376 KB |
Output isn't correct |
78 |
Correct |
54 ms |
4336 KB |
Output is correct |
79 |
Incorrect |
61 ms |
4428 KB |
Output isn't correct |
80 |
Incorrect |
52 ms |
4304 KB |
Output isn't correct |
81 |
Incorrect |
51 ms |
4292 KB |
Output isn't correct |
82 |
Incorrect |
50 ms |
4300 KB |
Output isn't correct |
83 |
Incorrect |
49 ms |
4316 KB |
Output isn't correct |
84 |
Incorrect |
50 ms |
4248 KB |
Output isn't correct |
85 |
Incorrect |
51 ms |
4304 KB |
Output isn't correct |
86 |
Incorrect |
58 ms |
4300 KB |
Output isn't correct |
87 |
Incorrect |
53 ms |
4256 KB |
Output isn't correct |
88 |
Incorrect |
51 ms |
4296 KB |
Output isn't correct |
89 |
Incorrect |
50 ms |
4328 KB |
Output isn't correct |
90 |
Incorrect |
48 ms |
4172 KB |
Output isn't correct |
91 |
Incorrect |
51 ms |
4240 KB |
Output isn't correct |
92 |
Incorrect |
53 ms |
4256 KB |
Output isn't correct |