#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ar array
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define vt vector
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
#define pll pair<ll, ll>
#define pii pair<int, int>
#define f first
#define s second
#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
template<class T> bool umin(T& a, const T& b) {
return b<a?a=b, 1:0;
}
template<class T> bool umax(T& a, const T& b) {
return a<b?a=b, 1:0;
}
ll FIRSTTRUE(function<bool(ll)> f, ll lb, ll rb) {
while(lb<rb) {
ll mb=(lb+rb)/2;
f(mb)?rb=mb:lb=mb+1;
}
return lb;
}
ll LASTTRUE(function<bool(ll)> f, ll lb, ll rb) {
while(lb<rb) {
ll mb=(lb+rb+1)/2;
f(mb)?lb=mb:rb=mb-1;
}
return lb;
}
template<class A> void read(vt<A>& v);
template<class A, size_t S> void read(ar<A, S>& a);
template<class A, class B> void read(pair<A, B>& x);
template<class T> void read(T& x) {
cin >> x;
}
void read(double& d) {
string t;
read(t);
d=stod(t);
}
void read(long double& d) {
string t;
read(t);
d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
read(h);
read(t...);
}
template<class A> void read(vt<A>& x) {
EACH(a, x)
read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
EACH(a, x)
read(a);
}
template<class A, class B> void read(pair<A, B>& x) {
cin >> x.first >> x.second;
}
string to_string(char c) {
return string(1, c);
}
string to_string(bool b) {
return b?"true":"false";
}
string to_string(const char* s) {
return string(s);
}
string to_string(string s) {
return s;
}
string to_string(vt<bool> v) {
string res;
FOR(sz(v))
res+=char('0'+v[i]);
return res;
}
template<size_t S> string to_string(bitset<S> b) {
string res;
FOR(S)
res+=char('0'+b[i]);
return res;
}
template<class T> string to_string(T v) {
bool f=1;
string res;
EACH(x, v) {
if(!f)
res+=' ';
f=0;
res+=to_string(x);
}
return res;
}
template<class A, class B> string to_string(pair<A, B>& x) {
return to_string(x.first) + ' ' + to_string(x.second);
}
template<class A> void write(A x) {
cout << to_string(x);
}
template<class H, class... T> void write(const H& h, const T&... t) {
write(h);
write(t...);
}
void print() {
write("\n");
}
template<class H, class... T> void print(const H& h, const T&... t) {
write(h);
if(sizeof...(t))
write(' ');
print(t...);
}
int dr[4]={1, -1, 0, 0}, dc[4]={0, 0, 1, -1};
int n, k, m1, m2, d1, d2;
vt<vt<int>> bd;
vt<vt<char>> g;
bool check(int x) {
vt<vt<int>> dist(n, vt<int>(n, -1));
queue<pii> q;
dist[m1][m2]=0, q.push({m1, m2});
while (sz(q)) {
pii v=q.front(); q.pop();
FOR(4) {
int r=v.f+dr[i], c=v.s+dc[i];
if (r>=0 && r<n && c>=0 && c<n && g[r][c]!='T' && dist[r][c]==-1 && (dist[v.f][v.s]+1)/k+x<bd[r][c]) {
if (r==d1 && c==d2) return true;
dist[r][c]=dist[v.f][v.s]+1;
q.push({r, c});
}
}
}
return false;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
read(n, k);
g.assign(n, vt<char>(n)), bd.assign(n, vt<int>(n, -1));
vt<pii> h;
char x;
FOR(n) {
FOR(j, n) {
read(x);
g[i][j]=x;
if (x=='M') m1=i, m2=j;
else if (x=='D') d1=i, d2=j;
else if (x=='H') h.pb({i, j});
}
}
queue<pii> q;
EACH(p, h) bd[p.f][p.s]=0, q.push({p.f, p.s});
while (sz(q)) {
pii v=q.front(); q.pop();
FOR(4) {
int r=v.f+dr[i], c=v.s+dc[i];
if (r>=0 && r<n && c>=0 && c<n && g[r][c]!='T' && g[r][c]!='D' && bd[r][c]==-1) {
bd[r][c]=bd[v.f][v.s]+1;
q.push({r, c});
}
}
}
bd[d1][d2]=INT_MAX;
int l=-1, r=1e6;
for (int i=r-l; i>0; i/=2) while (l+i<=r && check(l+i)) l+=i;
print(l);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
118 ms |
6256 KB |
Output is correct |
8 |
Correct |
0 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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
396 KB |
Output isn't correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
344 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
6 ms |
1676 KB |
Output is correct |
34 |
Correct |
7 ms |
1584 KB |
Output is correct |
35 |
Correct |
23 ms |
1624 KB |
Output is correct |
36 |
Correct |
8 ms |
1884 KB |
Output is correct |
37 |
Correct |
7 ms |
1784 KB |
Output is correct |
38 |
Correct |
36 ms |
1884 KB |
Output is correct |
39 |
Correct |
11 ms |
2284 KB |
Output is correct |
40 |
Correct |
10 ms |
2276 KB |
Output is correct |
41 |
Correct |
36 ms |
2356 KB |
Output is correct |
42 |
Correct |
15 ms |
2756 KB |
Output is correct |
43 |
Correct |
14 ms |
2708 KB |
Output is correct |
44 |
Correct |
48 ms |
2732 KB |
Output is correct |
45 |
Correct |
15 ms |
3220 KB |
Output is correct |
46 |
Correct |
15 ms |
3244 KB |
Output is correct |
47 |
Correct |
60 ms |
3232 KB |
Output is correct |
48 |
Correct |
19 ms |
3796 KB |
Output is correct |
49 |
Correct |
18 ms |
3796 KB |
Output is correct |
50 |
Correct |
68 ms |
3824 KB |
Output is correct |
51 |
Correct |
22 ms |
4560 KB |
Output is correct |
52 |
Correct |
21 ms |
4328 KB |
Output is correct |
53 |
Correct |
82 ms |
4300 KB |
Output is correct |
54 |
Correct |
26 ms |
4944 KB |
Output is correct |
55 |
Correct |
25 ms |
4940 KB |
Output is correct |
56 |
Correct |
89 ms |
4908 KB |
Output is correct |
57 |
Correct |
29 ms |
5536 KB |
Output is correct |
58 |
Correct |
29 ms |
5536 KB |
Output is correct |
59 |
Correct |
106 ms |
5496 KB |
Output is correct |
60 |
Correct |
38 ms |
6232 KB |
Output is correct |
61 |
Correct |
30 ms |
6240 KB |
Output is correct |
62 |
Correct |
132 ms |
6212 KB |
Output is correct |
63 |
Correct |
104 ms |
6388 KB |
Output is correct |
64 |
Correct |
174 ms |
6212 KB |
Output is correct |
65 |
Correct |
157 ms |
6208 KB |
Output is correct |
66 |
Correct |
179 ms |
6260 KB |
Output is correct |
67 |
Correct |
101 ms |
6204 KB |
Output is correct |
68 |
Correct |
57 ms |
6288 KB |
Output is correct |
69 |
Correct |
57 ms |
6316 KB |
Output is correct |
70 |
Correct |
49 ms |
6252 KB |
Output is correct |
71 |
Correct |
42 ms |
6268 KB |
Output is correct |
72 |
Incorrect |
42 ms |
6348 KB |
Output isn't correct |
73 |
Incorrect |
51 ms |
6324 KB |
Output isn't correct |
74 |
Correct |
68 ms |
6260 KB |
Output is correct |
75 |
Correct |
75 ms |
6260 KB |
Output is correct |
76 |
Correct |
71 ms |
6268 KB |
Output is correct |
77 |
Correct |
81 ms |
6420 KB |
Output is correct |
78 |
Correct |
67 ms |
6268 KB |
Output is correct |
79 |
Correct |
64 ms |
6240 KB |
Output is correct |
80 |
Correct |
77 ms |
6248 KB |
Output is correct |
81 |
Correct |
76 ms |
6260 KB |
Output is correct |
82 |
Correct |
62 ms |
6252 KB |
Output is correct |
83 |
Correct |
79 ms |
6284 KB |
Output is correct |
84 |
Correct |
83 ms |
6244 KB |
Output is correct |
85 |
Correct |
82 ms |
6260 KB |
Output is correct |
86 |
Correct |
100 ms |
6232 KB |
Output is correct |
87 |
Correct |
80 ms |
6232 KB |
Output is correct |
88 |
Correct |
76 ms |
6240 KB |
Output is correct |
89 |
Correct |
98 ms |
6308 KB |
Output is correct |
90 |
Correct |
99 ms |
6248 KB |
Output is correct |
91 |
Correct |
97 ms |
6232 KB |
Output is correct |
92 |
Correct |
95 ms |
6252 KB |
Output is correct |