//fold
#ifndef KHALIL
#include <bits/stdc++.h>
#else
#include "header.h"
#endif
#define endl '\n'
#define mp make_pair
#define tostr(x) static_cast<ostringstream&>((ostringstream()<<dec<<x)).str()
#define rep(i,begin,end) for(auto i = begin;i < end;i++)
#define repr(i,begin,end) for(auto i = begin-1;i >= end;i--)
#define pb push_back
#define sz(a) ((int)(a).size())
#define fi first
#define se second
#define abs(a) ((a) < (0) ? (-1)*(a) : (a))
#define SQ(a) ((a)*(a))
#define eqd(a,b) (abs(a-b)<1e-9)
#define X real()
#define Y imag()
using namespace std;
typedef long long ll;
typedef long double ld;
template <typename t> t in(t q){cin >> q;return q;}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v){os << "[";for (int i = 0; i < sz(v); ++i) { os << v[i]; if (i != sz(v) - 1) os << ",";}os << "]";return os;}
template <typename T, typename S>ostream& operator<<(ostream& os, const map<T, S>& v){for (auto it : v)os << "(" << it.first << ":" << it.second << ")";return os;}
template <typename T, typename S>ostream& operator<<(ostream& os, const pair<T, S>& v){os << "(" << v.first << "," << v.second << ")";return os;}
const long double PI = acosl(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng);}
//endfold
#define N (805)
#define MOD (1000000000l + 7l)
#define OO (1050000000)
#define OOL (1100000000000000000)
int b[N][N];
int z[N][N];
int n,s;
int dr[4] = {1,-1,0,0};
int dc[4] = {0,0,1,-1};
pair<int,int> target;
queue<pair<pair<int,int>,int>> q;
queue<pair<pair<int,int>,int>> q1;
void reset(){
while(!q.empty()) q.pop();
while(!q1.empty()) q1.pop();
rep(i,0,n){
rep(j,0,n){
z[i][j] = b[i][j];
if(z[i][j] == 1) q1.push({{i,j},0});
if(z[i][j] == 2) q.push({{i,j},0});
}
}
}
void simulate(int steps){
if(steps == 0) return;
while(!q1.empty()){
if(q1.front().second == steps) break;
auto t = q1.front(); q1.pop();
int r = t.first.first;
int c = t.first.second;
rep(i,0,4){
int nr = r+dr[i];
int nc = c+dc[i];
if(nr >= 0 && nr < n && nc >= 0 && nc < n){
if(z[nr][nc] != -1 && z[nr][nc] != 1 && !(nr == target.first && nc == target.second)){
z[nr][nc] = 1;
q1.push({{nr,nc},t.second+1});
}
}
}
}
}
int simulate2(int steps){
if(q.size() == 0) return -1;
while(!q.empty()){
if(q.front().second == steps) break;
auto t = q.front(); q.pop();
int r = t.first.first;
int c = t.first.second;
if(z[r][c] != 2) continue;
rep(i,0,4){
int nr = r+dr[i];
int nc = c+dc[i];
if(nr >= 0 && nr < n && nc >= 0 && nc < n){
if(z[nr][nc] == 0){
z[nr][nc] = 2;
q.push({{nr,nc},t.second+1});
}
}
}
}
return 1;
}
int main(){
//fold
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cout << setprecision(10);
//endfold
cin >> n >> s;
rep(i,0,n){
string q;
cin >> q;
rep(j,0,n){
if(q[j] == 'D') target = {i,j};
if(q[j] == 'T') b[i][j] = -1;
if(q[j] == 'H') b[i][j] = 1;
if(q[j] == 'M') b[i][j] = 2;
}
}
int l = 0,r = n*n,best = -1;
while(l <= r){
int mid = (l+r)/2;
reset();
int we = mid;
int ew = s;
simulate(we++);
bool good = false;
while(simulate2(ew) > 0){
if(z[target.first][target.second] == 2){
good = true;
break;
}
ew += s;
simulate(we++);
}
if(good){
best = mid;
l = mid+1;
}else{
r = mid-1;
}
}
cout << best;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
433 ms |
5424 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
4 ms |
768 KB |
Output is correct |
13 |
Correct |
4 ms |
640 KB |
Output is correct |
14 |
Correct |
4 ms |
768 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
0 ms |
512 KB |
Output is correct |
18 |
Correct |
3 ms |
512 KB |
Output is correct |
19 |
Correct |
2 ms |
512 KB |
Output is correct |
20 |
Correct |
3 ms |
640 KB |
Output is correct |
21 |
Correct |
2 ms |
640 KB |
Output is correct |
22 |
Correct |
1 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
3 ms |
640 KB |
Output is correct |
25 |
Correct |
3 ms |
768 KB |
Output is correct |
26 |
Correct |
3 ms |
768 KB |
Output is correct |
27 |
Correct |
3 ms |
640 KB |
Output is correct |
28 |
Correct |
3 ms |
768 KB |
Output is correct |
29 |
Correct |
3 ms |
768 KB |
Output is correct |
30 |
Correct |
3 ms |
768 KB |
Output is correct |
31 |
Correct |
4 ms |
896 KB |
Output is correct |
32 |
Correct |
3 ms |
768 KB |
Output is correct |
33 |
Correct |
27 ms |
2560 KB |
Output is correct |
34 |
Correct |
33 ms |
2688 KB |
Output is correct |
35 |
Correct |
42 ms |
2728 KB |
Output is correct |
36 |
Correct |
40 ms |
3040 KB |
Output is correct |
37 |
Correct |
45 ms |
2992 KB |
Output is correct |
38 |
Correct |
58 ms |
3072 KB |
Output is correct |
39 |
Correct |
45 ms |
3328 KB |
Output is correct |
40 |
Correct |
73 ms |
3456 KB |
Output is correct |
41 |
Correct |
99 ms |
3328 KB |
Output is correct |
42 |
Correct |
70 ms |
3712 KB |
Output is correct |
43 |
Correct |
97 ms |
3712 KB |
Output is correct |
44 |
Correct |
145 ms |
3832 KB |
Output is correct |
45 |
Correct |
127 ms |
4068 KB |
Output is correct |
46 |
Correct |
114 ms |
4156 KB |
Output is correct |
47 |
Correct |
180 ms |
4088 KB |
Output is correct |
48 |
Correct |
134 ms |
4600 KB |
Output is correct |
49 |
Correct |
103 ms |
4480 KB |
Output is correct |
50 |
Correct |
160 ms |
4480 KB |
Output is correct |
51 |
Correct |
179 ms |
4864 KB |
Output is correct |
52 |
Correct |
99 ms |
4864 KB |
Output is correct |
53 |
Correct |
166 ms |
4864 KB |
Output is correct |
54 |
Correct |
131 ms |
5292 KB |
Output is correct |
55 |
Correct |
132 ms |
5248 KB |
Output is correct |
56 |
Correct |
244 ms |
5296 KB |
Output is correct |
57 |
Correct |
145 ms |
5676 KB |
Output is correct |
58 |
Correct |
153 ms |
5752 KB |
Output is correct |
59 |
Correct |
279 ms |
5752 KB |
Output is correct |
60 |
Correct |
203 ms |
6016 KB |
Output is correct |
61 |
Correct |
168 ms |
6016 KB |
Output is correct |
62 |
Correct |
388 ms |
6064 KB |
Output is correct |
63 |
Correct |
353 ms |
6136 KB |
Output is correct |
64 |
Correct |
378 ms |
6072 KB |
Output is correct |
65 |
Correct |
351 ms |
6136 KB |
Output is correct |
66 |
Correct |
410 ms |
6136 KB |
Output is correct |
67 |
Correct |
369 ms |
6084 KB |
Output is correct |
68 |
Correct |
444 ms |
6112 KB |
Output is correct |
69 |
Correct |
350 ms |
6016 KB |
Output is correct |
70 |
Correct |
342 ms |
6108 KB |
Output is correct |
71 |
Correct |
465 ms |
6108 KB |
Output is correct |
72 |
Correct |
410 ms |
6016 KB |
Output is correct |
73 |
Correct |
310 ms |
6400 KB |
Output is correct |
74 |
Correct |
383 ms |
6476 KB |
Output is correct |
75 |
Correct |
425 ms |
6520 KB |
Output is correct |
76 |
Correct |
445 ms |
6476 KB |
Output is correct |
77 |
Correct |
426 ms |
6480 KB |
Output is correct |
78 |
Correct |
435 ms |
6436 KB |
Output is correct |
79 |
Correct |
479 ms |
6440 KB |
Output is correct |
80 |
Correct |
540 ms |
6436 KB |
Output is correct |
81 |
Correct |
395 ms |
6440 KB |
Output is correct |
82 |
Correct |
424 ms |
6436 KB |
Output is correct |
83 |
Correct |
447 ms |
6496 KB |
Output is correct |
84 |
Correct |
466 ms |
6512 KB |
Output is correct |
85 |
Correct |
438 ms |
6480 KB |
Output is correct |
86 |
Correct |
404 ms |
6504 KB |
Output is correct |
87 |
Correct |
439 ms |
6484 KB |
Output is correct |
88 |
Correct |
398 ms |
6304 KB |
Output is correct |
89 |
Correct |
377 ms |
6324 KB |
Output is correct |
90 |
Correct |
394 ms |
6304 KB |
Output is correct |
91 |
Correct |
407 ms |
6312 KB |
Output is correct |
92 |
Correct |
580 ms |
6304 KB |
Output is correct |