//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;
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 && !(nr == target.first && nc == target.second)){
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
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 |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
420 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Incorrect |
3 ms |
448 KB |
Output isn't correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Execution timed out |
1073 ms |
12640 KB |
Time limit exceeded |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
768 KB |
Output is correct |
13 |
Incorrect |
4 ms |
640 KB |
Output isn't correct |
14 |
Correct |
19 ms |
768 KB |
Output is correct |
15 |
Correct |
4 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
512 KB |
Output is correct |
18 |
Correct |
3 ms |
512 KB |
Output is correct |
19 |
Correct |
3 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 |
3 ms |
640 KB |
Output is correct |
23 |
Correct |
7 ms |
640 KB |
Output is correct |
24 |
Correct |
5 ms |
768 KB |
Output is correct |
25 |
Correct |
7 ms |
768 KB |
Output is correct |
26 |
Correct |
6 ms |
768 KB |
Output is correct |
27 |
Correct |
8 ms |
768 KB |
Output is correct |
28 |
Correct |
6 ms |
768 KB |
Output is correct |
29 |
Correct |
9 ms |
896 KB |
Output is correct |
30 |
Correct |
7 ms |
768 KB |
Output is correct |
31 |
Correct |
13 ms |
896 KB |
Output is correct |
32 |
Correct |
7 ms |
896 KB |
Output is correct |
33 |
Correct |
778 ms |
3688 KB |
Output is correct |
34 |
Correct |
512 ms |
3424 KB |
Output is correct |
35 |
Execution timed out |
1051 ms |
4076 KB |
Time limit exceeded |
36 |
Execution timed out |
1072 ms |
4120 KB |
Time limit exceeded |
37 |
Correct |
725 ms |
4112 KB |
Output is correct |
38 |
Execution timed out |
1067 ms |
4616 KB |
Time limit exceeded |
39 |
Execution timed out |
1059 ms |
4812 KB |
Time limit exceeded |
40 |
Execution timed out |
1073 ms |
4704 KB |
Time limit exceeded |
41 |
Execution timed out |
1063 ms |
4832 KB |
Time limit exceeded |
42 |
Execution timed out |
1059 ms |
5248 KB |
Time limit exceeded |
43 |
Execution timed out |
1064 ms |
5448 KB |
Time limit exceeded |
44 |
Execution timed out |
1066 ms |
5260 KB |
Time limit exceeded |
45 |
Execution timed out |
1061 ms |
5976 KB |
Time limit exceeded |
46 |
Execution timed out |
1080 ms |
5960 KB |
Time limit exceeded |
47 |
Execution timed out |
1072 ms |
6440 KB |
Time limit exceeded |
48 |
Execution timed out |
1077 ms |
6668 KB |
Time limit exceeded |
49 |
Execution timed out |
1074 ms |
6692 KB |
Time limit exceeded |
50 |
Execution timed out |
1027 ms |
7236 KB |
Time limit exceeded |
51 |
Execution timed out |
1054 ms |
7332 KB |
Time limit exceeded |
52 |
Execution timed out |
1079 ms |
7272 KB |
Time limit exceeded |
53 |
Execution timed out |
1060 ms |
7936 KB |
Time limit exceeded |
54 |
Execution timed out |
1074 ms |
8000 KB |
Time limit exceeded |
55 |
Execution timed out |
1010 ms |
7916 KB |
Time limit exceeded |
56 |
Execution timed out |
1050 ms |
8748 KB |
Time limit exceeded |
57 |
Execution timed out |
1053 ms |
8876 KB |
Time limit exceeded |
58 |
Execution timed out |
1057 ms |
8848 KB |
Time limit exceeded |
59 |
Execution timed out |
1070 ms |
9380 KB |
Time limit exceeded |
60 |
Execution timed out |
1075 ms |
9640 KB |
Time limit exceeded |
61 |
Execution timed out |
1064 ms |
9692 KB |
Time limit exceeded |
62 |
Execution timed out |
1069 ms |
10084 KB |
Time limit exceeded |
63 |
Execution timed out |
1052 ms |
7496 KB |
Time limit exceeded |
64 |
Execution timed out |
1081 ms |
10396 KB |
Time limit exceeded |
65 |
Execution timed out |
1077 ms |
10184 KB |
Time limit exceeded |
66 |
Execution timed out |
1063 ms |
7912 KB |
Time limit exceeded |
67 |
Execution timed out |
1076 ms |
10308 KB |
Time limit exceeded |
68 |
Execution timed out |
1070 ms |
9796 KB |
Time limit exceeded |
69 |
Execution timed out |
1060 ms |
9548 KB |
Time limit exceeded |
70 |
Execution timed out |
1056 ms |
9440 KB |
Time limit exceeded |
71 |
Execution timed out |
1079 ms |
9488 KB |
Time limit exceeded |
72 |
Correct |
548 ms |
9860 KB |
Output is correct |
73 |
Execution timed out |
1067 ms |
9836 KB |
Time limit exceeded |
74 |
Execution timed out |
1066 ms |
12932 KB |
Time limit exceeded |
75 |
Execution timed out |
1058 ms |
12792 KB |
Time limit exceeded |
76 |
Execution timed out |
1056 ms |
12940 KB |
Time limit exceeded |
77 |
Execution timed out |
1058 ms |
12792 KB |
Time limit exceeded |
78 |
Execution timed out |
1028 ms |
12788 KB |
Time limit exceeded |
79 |
Execution timed out |
1066 ms |
13080 KB |
Time limit exceeded |
80 |
Execution timed out |
1067 ms |
12996 KB |
Time limit exceeded |
81 |
Execution timed out |
1074 ms |
12928 KB |
Time limit exceeded |
82 |
Execution timed out |
1071 ms |
13040 KB |
Time limit exceeded |
83 |
Execution timed out |
1074 ms |
12692 KB |
Time limit exceeded |
84 |
Execution timed out |
1070 ms |
13068 KB |
Time limit exceeded |
85 |
Execution timed out |
1057 ms |
11684 KB |
Time limit exceeded |
86 |
Execution timed out |
1069 ms |
13304 KB |
Time limit exceeded |
87 |
Execution timed out |
1059 ms |
12952 KB |
Time limit exceeded |
88 |
Execution timed out |
1064 ms |
13068 KB |
Time limit exceeded |
89 |
Execution timed out |
1057 ms |
11768 KB |
Time limit exceeded |
90 |
Execution timed out |
1029 ms |
12772 KB |
Time limit exceeded |
91 |
Execution timed out |
1054 ms |
13004 KB |
Time limit exceeded |
92 |
Execution timed out |
1081 ms |
13032 KB |
Time limit exceeded |