//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};
void reset(){
rep(i,0,n){
rep(j,0,n){
z[i][j] = b[i][j];
}
}
}
void simulate(int steps){
if(steps == 0) return;
queue<pair<pair<int,int>,int>> q;
rep(i,0,n){
rep(j,0,n){
if(z[i][j] == 1){
q.push({{i,j},0});
}
}
}
while(!q.empty()){
auto t = q.front(); q.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){
z[nr][nc] = 1;
if(t.second != steps-1){
q.push({{nr,nc},t.second+1});
}
}
}
}
}
}
int simulate2(int steps){
queue<pair<pair<int,int>,int>> q;
rep(i,0,n){
rep(j,0,n){
if(z[i][j] == 2){
q.push({{i,j},0});
}
}
}
if(q.size() == 0) return -1;
while(!q.empty()){
auto t = q.front(); q.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] == 0){
z[nr][nc] = 2;
if(t.second != steps-1){
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
pair<int,int> target;
cin >> n >> s;
rep(i,0,n){
string q;
cin >> q;
map<char,int> pw = {{'T',-1},{'G',0},
{'H',1},{'M',2},{'D',0}};
rep(j,0,n){
if(q[j] == 'D') target = {i,j};
b[i][j] = pw[q[j]];
}
}
int l = 1,r = n*n,best = -1;
while(l <= r){
int mid = (l+r)/2;
reset();
simulate(mid);
bool good = false;
while(simulate2(s) > 0){
if(z[target.first][target.second] == 2){
good = true;
break;
}
if(z[target.first][target.second] == 1){
good = false;
break;
}
simulate(1);
if(z[target.first][target.second] == 1){
good = false;
break;
}
}
if(good){
best = mid;
l = mid+1;
}else{
r = mid-1;
}
}
cout << best;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |
5 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |
6 |
Incorrect |
3 ms |
384 KB |
Output isn't correct |
7 |
Execution timed out |
1027 ms |
9636 KB |
Time limit exceeded |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Incorrect |
3 ms |
688 KB |
Output isn't correct |
13 |
Incorrect |
4 ms |
640 KB |
Output isn't correct |
14 |
Correct |
7 ms |
768 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
3 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 |
512 KB |
Output is correct |
21 |
Correct |
4 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
4 ms |
640 KB |
Output is correct |
24 |
Correct |
4 ms |
640 KB |
Output is correct |
25 |
Correct |
4 ms |
768 KB |
Output is correct |
26 |
Correct |
4 ms |
768 KB |
Output is correct |
27 |
Correct |
5 ms |
768 KB |
Output is correct |
28 |
Correct |
4 ms |
768 KB |
Output is correct |
29 |
Correct |
5 ms |
896 KB |
Output is correct |
30 |
Correct |
5 ms |
896 KB |
Output is correct |
31 |
Correct |
6 ms |
896 KB |
Output is correct |
32 |
Correct |
7 ms |
896 KB |
Output is correct |
33 |
Correct |
89 ms |
3392 KB |
Output is correct |
34 |
Correct |
58 ms |
3376 KB |
Output is correct |
35 |
Correct |
53 ms |
3380 KB |
Output is correct |
36 |
Correct |
79 ms |
3876 KB |
Output is correct |
37 |
Correct |
83 ms |
3872 KB |
Output is correct |
38 |
Correct |
66 ms |
3848 KB |
Output is correct |
39 |
Correct |
130 ms |
4492 KB |
Output is correct |
40 |
Correct |
146 ms |
4452 KB |
Output is correct |
41 |
Correct |
121 ms |
4528 KB |
Output is correct |
42 |
Correct |
160 ms |
5076 KB |
Output is correct |
43 |
Correct |
116 ms |
5056 KB |
Output is correct |
44 |
Correct |
101 ms |
5064 KB |
Output is correct |
45 |
Correct |
183 ms |
5696 KB |
Output is correct |
46 |
Correct |
230 ms |
5624 KB |
Output is correct |
47 |
Correct |
123 ms |
5620 KB |
Output is correct |
48 |
Correct |
234 ms |
6448 KB |
Output is correct |
49 |
Correct |
238 ms |
6544 KB |
Output is correct |
50 |
Correct |
235 ms |
6372 KB |
Output is correct |
51 |
Correct |
283 ms |
7096 KB |
Output is correct |
52 |
Correct |
274 ms |
7060 KB |
Output is correct |
53 |
Correct |
258 ms |
7072 KB |
Output is correct |
54 |
Correct |
305 ms |
7896 KB |
Output is correct |
55 |
Correct |
252 ms |
7764 KB |
Output is correct |
56 |
Correct |
239 ms |
7748 KB |
Output is correct |
57 |
Correct |
414 ms |
8624 KB |
Output is correct |
58 |
Correct |
337 ms |
8772 KB |
Output is correct |
59 |
Correct |
281 ms |
8556 KB |
Output is correct |
60 |
Correct |
327 ms |
9296 KB |
Output is correct |
61 |
Correct |
452 ms |
9432 KB |
Output is correct |
62 |
Correct |
320 ms |
9252 KB |
Output is correct |
63 |
Execution timed out |
1078 ms |
7560 KB |
Time limit exceeded |
64 |
Execution timed out |
1060 ms |
6844 KB |
Time limit exceeded |
65 |
Execution timed out |
1080 ms |
6888 KB |
Time limit exceeded |
66 |
Execution timed out |
1066 ms |
7956 KB |
Time limit exceeded |
67 |
Execution timed out |
1072 ms |
7600 KB |
Time limit exceeded |
68 |
Execution timed out |
1072 ms |
9664 KB |
Time limit exceeded |
69 |
Execution timed out |
1070 ms |
9680 KB |
Time limit exceeded |
70 |
Execution timed out |
1075 ms |
9628 KB |
Time limit exceeded |
71 |
Execution timed out |
1080 ms |
9524 KB |
Time limit exceeded |
72 |
Correct |
449 ms |
9824 KB |
Output is correct |
73 |
Execution timed out |
1074 ms |
9740 KB |
Time limit exceeded |
74 |
Correct |
494 ms |
11468 KB |
Output is correct |
75 |
Execution timed out |
1084 ms |
11200 KB |
Time limit exceeded |
76 |
Execution timed out |
1080 ms |
11648 KB |
Time limit exceeded |
77 |
Execution timed out |
1085 ms |
11212 KB |
Time limit exceeded |
78 |
Execution timed out |
1067 ms |
10240 KB |
Time limit exceeded |
79 |
Correct |
444 ms |
11332 KB |
Output is correct |
80 |
Execution timed out |
1083 ms |
11452 KB |
Time limit exceeded |
81 |
Execution timed out |
1071 ms |
10924 KB |
Time limit exceeded |
82 |
Execution timed out |
1079 ms |
11392 KB |
Time limit exceeded |
83 |
Execution timed out |
1008 ms |
10116 KB |
Time limit exceeded |
84 |
Correct |
590 ms |
10440 KB |
Output is correct |
85 |
Execution timed out |
1066 ms |
10544 KB |
Time limit exceeded |
86 |
Execution timed out |
1069 ms |
10460 KB |
Time limit exceeded |
87 |
Execution timed out |
1072 ms |
10284 KB |
Time limit exceeded |
88 |
Execution timed out |
1076 ms |
9812 KB |
Time limit exceeded |
89 |
Correct |
501 ms |
9632 KB |
Output is correct |
90 |
Execution timed out |
1075 ms |
9692 KB |
Time limit exceeded |
91 |
Execution timed out |
1070 ms |
9792 KB |
Time limit exceeded |
92 |
Execution timed out |
1072 ms |
9948 KB |
Time limit exceeded |