#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) && grid[nr][nc] != 'H' && !vis[nr][nc] && (nsl != 0 && bee[nr][nc] == dis || 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';
}
}
Compilation message
mecho.cpp: In function 'bool pos(int)':
mecho.cpp:58:79: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
58 | if(iv(nr, nc) && grid[nr][nc] != 'H' && !vis[nr][nc] && (nsl != 0 && bee[nr][nc] == dis || dis < bee[nr][nc])){
| ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
4 ms |
5972 KB |
Output is correct |
3 |
Correct |
5 ms |
6088 KB |
Output is correct |
4 |
Correct |
3 ms |
5972 KB |
Output is correct |
5 |
Correct |
3 ms |
5972 KB |
Output is correct |
6 |
Correct |
3 ms |
6100 KB |
Output is correct |
7 |
Correct |
131 ms |
6812 KB |
Output is correct |
8 |
Correct |
3 ms |
6100 KB |
Output is correct |
9 |
Correct |
3 ms |
6100 KB |
Output is correct |
10 |
Correct |
4 ms |
6100 KB |
Output is correct |
11 |
Correct |
3 ms |
6100 KB |
Output is correct |
12 |
Correct |
4 ms |
6100 KB |
Output is correct |
13 |
Correct |
4 ms |
6100 KB |
Output is correct |
14 |
Correct |
4 ms |
6100 KB |
Output is correct |
15 |
Correct |
4 ms |
6100 KB |
Output is correct |
16 |
Correct |
4 ms |
6100 KB |
Output is correct |
17 |
Correct |
4 ms |
6100 KB |
Output is correct |
18 |
Correct |
4 ms |
6100 KB |
Output is correct |
19 |
Correct |
4 ms |
6100 KB |
Output is correct |
20 |
Correct |
4 ms |
6004 KB |
Output is correct |
21 |
Correct |
4 ms |
6100 KB |
Output is correct |
22 |
Correct |
5 ms |
6100 KB |
Output is correct |
23 |
Correct |
4 ms |
6100 KB |
Output is correct |
24 |
Correct |
5 ms |
6100 KB |
Output is correct |
25 |
Correct |
5 ms |
6100 KB |
Output is correct |
26 |
Correct |
4 ms |
6100 KB |
Output is correct |
27 |
Correct |
5 ms |
6072 KB |
Output is correct |
28 |
Correct |
6 ms |
6100 KB |
Output is correct |
29 |
Correct |
4 ms |
6100 KB |
Output is correct |
30 |
Correct |
4 ms |
6100 KB |
Output is correct |
31 |
Correct |
6 ms |
6100 KB |
Output is correct |
32 |
Correct |
5 ms |
6100 KB |
Output is correct |
33 |
Correct |
13 ms |
6284 KB |
Output is correct |
34 |
Correct |
15 ms |
6356 KB |
Output is correct |
35 |
Correct |
32 ms |
6356 KB |
Output is correct |
36 |
Correct |
14 ms |
6308 KB |
Output is correct |
37 |
Correct |
15 ms |
6412 KB |
Output is correct |
38 |
Correct |
38 ms |
6332 KB |
Output is correct |
39 |
Correct |
17 ms |
6356 KB |
Output is correct |
40 |
Correct |
17 ms |
6412 KB |
Output is correct |
41 |
Correct |
48 ms |
6356 KB |
Output is correct |
42 |
Correct |
21 ms |
6492 KB |
Output is correct |
43 |
Correct |
22 ms |
6476 KB |
Output is correct |
44 |
Correct |
61 ms |
6468 KB |
Output is correct |
45 |
Correct |
25 ms |
6528 KB |
Output is correct |
46 |
Correct |
25 ms |
6396 KB |
Output is correct |
47 |
Correct |
73 ms |
6476 KB |
Output is correct |
48 |
Correct |
27 ms |
6540 KB |
Output is correct |
49 |
Correct |
29 ms |
6524 KB |
Output is correct |
50 |
Correct |
92 ms |
6548 KB |
Output is correct |
51 |
Correct |
33 ms |
6476 KB |
Output is correct |
52 |
Correct |
31 ms |
6548 KB |
Output is correct |
53 |
Correct |
98 ms |
6508 KB |
Output is correct |
54 |
Correct |
35 ms |
6520 KB |
Output is correct |
55 |
Correct |
37 ms |
6612 KB |
Output is correct |
56 |
Correct |
130 ms |
6632 KB |
Output is correct |
57 |
Correct |
46 ms |
6652 KB |
Output is correct |
58 |
Correct |
41 ms |
6580 KB |
Output is correct |
59 |
Correct |
141 ms |
6668 KB |
Output is correct |
60 |
Correct |
43 ms |
6640 KB |
Output is correct |
61 |
Correct |
44 ms |
6648 KB |
Output is correct |
62 |
Correct |
155 ms |
6664 KB |
Output is correct |
63 |
Correct |
151 ms |
6708 KB |
Output is correct |
64 |
Correct |
213 ms |
6672 KB |
Output is correct |
65 |
Correct |
220 ms |
6704 KB |
Output is correct |
66 |
Correct |
205 ms |
6656 KB |
Output is correct |
67 |
Correct |
65 ms |
6620 KB |
Output is correct |
68 |
Correct |
82 ms |
6732 KB |
Output is correct |
69 |
Correct |
77 ms |
6620 KB |
Output is correct |
70 |
Correct |
79 ms |
6728 KB |
Output is correct |
71 |
Correct |
73 ms |
6708 KB |
Output is correct |
72 |
Correct |
57 ms |
6640 KB |
Output is correct |
73 |
Correct |
70 ms |
6936 KB |
Output is correct |
74 |
Correct |
106 ms |
6860 KB |
Output is correct |
75 |
Correct |
126 ms |
6932 KB |
Output is correct |
76 |
Correct |
95 ms |
6944 KB |
Output is correct |
77 |
Correct |
99 ms |
6972 KB |
Output is correct |
78 |
Correct |
60 ms |
6948 KB |
Output is correct |
79 |
Correct |
83 ms |
6860 KB |
Output is correct |
80 |
Correct |
95 ms |
6860 KB |
Output is correct |
81 |
Correct |
99 ms |
6944 KB |
Output is correct |
82 |
Correct |
90 ms |
6944 KB |
Output is correct |
83 |
Correct |
106 ms |
6860 KB |
Output is correct |
84 |
Correct |
111 ms |
6840 KB |
Output is correct |
85 |
Correct |
103 ms |
6908 KB |
Output is correct |
86 |
Correct |
106 ms |
6860 KB |
Output is correct |
87 |
Correct |
112 ms |
6808 KB |
Output is correct |
88 |
Correct |
136 ms |
6824 KB |
Output is correct |
89 |
Correct |
129 ms |
6860 KB |
Output is correct |
90 |
Correct |
122 ms |
6856 KB |
Output is correct |
91 |
Correct |
109 ms |
6812 KB |
Output is correct |
92 |
Correct |
119 ms |
6848 KB |
Output is correct |