#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define xx first
#define yy second
using namespace std;
typedef long long i64;
typedef pair<int,int> pii;
int n;
int s;
char gazar[810][810];
bool vis[810][810];
int beed[810][810];
int mei, mej;
bool feasible (int x){
memset(vis, 0, sizeof vis);
queue<pii> q;
int depth = x * s;
q.push(mp(mei, mej));
vis[mei][mej] = 1;
while(!q.empty()){
int sz = q.size();
for (int cnt = 0; cnt < sz; cnt++){
pii nw = q.front();
if (gazar[nw.xx][nw.yy] == 'D') return 1;
q.pop();
if (nw.xx < n && !vis[nw.xx + 1][nw.yy] && (gazar[nw.xx + 1][nw.yy] == 'D' || (gazar[nw.xx + 1][nw.yy] == 'G' && ((depth+1) / s) < beed[nw.xx + 1][nw.yy]) ) ) q.push(mp(nw.xx + 1, nw.yy)), vis[nw.xx + 1][nw.yy] = 1;
if (nw.xx > 1 && !vis[nw.xx - 1][nw.yy] && (gazar[nw.xx - 1][nw.yy] == 'D' || (gazar[nw.xx - 1][nw.yy] == 'G' && ((depth+1) / s) < beed[nw.xx - 1][nw.yy]) ) ) q.push(mp(nw.xx - 1, nw.yy)), vis[nw.xx - 1][nw.yy] = 1;
if (nw.yy < n && !vis[nw.xx][nw.yy + 1] && (gazar[nw.xx][nw.yy + 1] == 'D' || (gazar[nw.xx][nw.yy + 1] == 'G' && ((depth+1) / s) < beed[nw.xx][nw.yy + 1]) ) ) q.push(mp(nw.xx, nw.yy + 1)), vis[nw.xx][nw.yy + 1] = 1;
if (nw.yy > 1 && !vis[nw.xx][nw.yy - 1] && (gazar[nw.xx][nw.yy - 1] == 'D' || (gazar[nw.xx][nw.yy - 1] == 'G' && ((depth+1) / s) < beed[nw.xx][nw.yy - 1]) ) ) q.push(mp(nw.xx, nw.yy - 1)), vis[nw.xx][nw.yy - 1] = 1;
}
depth ++;
}
return 0;
}
int main (){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// freopen("in.txt", "r", stdin);
cin >> n;
cin >> s;
for (int i = 1; i <= n; i++){
string tmp;
cin >> tmp;
// cout << tmp;
for (int j = 1; j <= n; j++){
gazar[i][j] = tmp[j - 1];
if (tmp[j - 1] == 'M') mei = i, mej = j;
}
}
queue<pii> q;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
if (gazar[i][j] == 'H') q.push( mp(i, j) ), vis[i][j] = 1;
}
}
int depth = 0;
// cout << "QQQ";
while (!q.empty()){
int sz = q.size();
// cout << sz << ' ';
for (int cnt = 0; cnt < sz; cnt++){
pii nw = q.front();
q.pop();
beed[nw.xx][nw.yy] = depth;
if (nw.xx < n && !vis[nw.xx + 1][nw.yy] && (gazar[nw.xx + 1][nw.yy] == 'M' || gazar[nw.xx + 1][nw.yy] == 'G') ) q.push(mp(nw.xx + 1, nw.yy)), vis[nw.xx + 1][nw.yy] = 1;
if (nw.xx > 1 && !vis[nw.xx - 1][nw.yy] && (gazar[nw.xx - 1][nw.yy] == 'M' || gazar[nw.xx - 1][nw.yy] == 'G') ) q.push(mp(nw.xx - 1, nw.yy)), vis[nw.xx - 1][nw.yy] = 1;
if (nw.yy < n && !vis[nw.xx][nw.yy + 1] && (gazar[nw.xx][nw.yy + 1] == 'M' || gazar[nw.xx][nw.yy + 1] == 'G') ) q.push(mp(nw.xx, nw.yy + 1)), vis[nw.xx][nw.yy + 1] = 1;
if (nw.yy > 1 && !vis[nw.xx][nw.yy - 1] && (gazar[nw.xx][nw.yy - 1] == 'M' || gazar[nw.xx][nw.yy - 1] == 'G') ) q.push(mp(nw.xx, nw.yy - 1)), vis[nw.xx][nw.yy - 1] = 1;
}
depth++;
}
int lo = 0, hi = beed[mei][mej] - 1;
while (lo + 1 < hi){
int mid = (lo + hi) >> 1;
if (feasible(mid)) lo = mid;
else hi = mid - 1;
}
if(feasible(lo)) cout << lo;
else cout << -1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1016 KB |
Output is correct |
2 |
Correct |
3 ms |
1016 KB |
Output is correct |
3 |
Correct |
3 ms |
1016 KB |
Output is correct |
4 |
Incorrect |
2 ms |
1016 KB |
Output isn't correct |
5 |
Correct |
2 ms |
1016 KB |
Output is correct |
6 |
Correct |
3 ms |
1016 KB |
Output is correct |
7 |
Incorrect |
64 ms |
4984 KB |
Output isn't correct |
8 |
Correct |
3 ms |
1020 KB |
Output is correct |
9 |
Incorrect |
3 ms |
1016 KB |
Output isn't correct |
10 |
Correct |
3 ms |
1016 KB |
Output is correct |
11 |
Correct |
3 ms |
1016 KB |
Output is correct |
12 |
Incorrect |
3 ms |
1272 KB |
Output isn't correct |
13 |
Correct |
3 ms |
1144 KB |
Output is correct |
14 |
Correct |
3 ms |
1272 KB |
Output is correct |
15 |
Correct |
3 ms |
1016 KB |
Output is correct |
16 |
Correct |
3 ms |
1016 KB |
Output is correct |
17 |
Correct |
3 ms |
1016 KB |
Output is correct |
18 |
Correct |
3 ms |
1016 KB |
Output is correct |
19 |
Correct |
3 ms |
1144 KB |
Output is correct |
20 |
Correct |
3 ms |
1144 KB |
Output is correct |
21 |
Correct |
3 ms |
1144 KB |
Output is correct |
22 |
Correct |
3 ms |
1144 KB |
Output is correct |
23 |
Correct |
3 ms |
1144 KB |
Output is correct |
24 |
Correct |
3 ms |
1272 KB |
Output is correct |
25 |
Correct |
3 ms |
1272 KB |
Output is correct |
26 |
Correct |
3 ms |
1272 KB |
Output is correct |
27 |
Correct |
3 ms |
1272 KB |
Output is correct |
28 |
Correct |
3 ms |
1272 KB |
Output is correct |
29 |
Correct |
3 ms |
1272 KB |
Output is correct |
30 |
Correct |
3 ms |
1272 KB |
Output is correct |
31 |
Correct |
3 ms |
1272 KB |
Output is correct |
32 |
Correct |
3 ms |
1272 KB |
Output is correct |
33 |
Correct |
6 ms |
2552 KB |
Output is correct |
34 |
Correct |
6 ms |
2552 KB |
Output is correct |
35 |
Correct |
16 ms |
2424 KB |
Output is correct |
36 |
Correct |
7 ms |
2808 KB |
Output is correct |
37 |
Correct |
7 ms |
2808 KB |
Output is correct |
38 |
Incorrect |
22 ms |
2808 KB |
Output isn't correct |
39 |
Correct |
8 ms |
2936 KB |
Output is correct |
40 |
Correct |
8 ms |
2936 KB |
Output is correct |
41 |
Correct |
28 ms |
2936 KB |
Output is correct |
42 |
Correct |
8 ms |
3192 KB |
Output is correct |
43 |
Correct |
8 ms |
3192 KB |
Output is correct |
44 |
Correct |
36 ms |
3276 KB |
Output is correct |
45 |
Correct |
10 ms |
3448 KB |
Output is correct |
46 |
Correct |
10 ms |
3448 KB |
Output is correct |
47 |
Correct |
53 ms |
3448 KB |
Output is correct |
48 |
Correct |
11 ms |
3832 KB |
Output is correct |
49 |
Correct |
10 ms |
3708 KB |
Output is correct |
50 |
Incorrect |
47 ms |
3704 KB |
Output isn't correct |
51 |
Correct |
11 ms |
3960 KB |
Output is correct |
52 |
Correct |
11 ms |
3960 KB |
Output is correct |
53 |
Correct |
56 ms |
3960 KB |
Output is correct |
54 |
Correct |
13 ms |
4220 KB |
Output is correct |
55 |
Correct |
12 ms |
4216 KB |
Output is correct |
56 |
Correct |
68 ms |
4220 KB |
Output is correct |
57 |
Correct |
14 ms |
4472 KB |
Output is correct |
58 |
Correct |
13 ms |
4472 KB |
Output is correct |
59 |
Correct |
84 ms |
4700 KB |
Output is correct |
60 |
Correct |
15 ms |
4728 KB |
Output is correct |
61 |
Correct |
14 ms |
4856 KB |
Output is correct |
62 |
Incorrect |
102 ms |
4856 KB |
Output isn't correct |
63 |
Correct |
91 ms |
4792 KB |
Output is correct |
64 |
Incorrect |
163 ms |
4856 KB |
Output isn't correct |
65 |
Correct |
162 ms |
4984 KB |
Output is correct |
66 |
Correct |
123 ms |
4728 KB |
Output is correct |
67 |
Correct |
102 ms |
4732 KB |
Output is correct |
68 |
Correct |
49 ms |
4856 KB |
Output is correct |
69 |
Correct |
48 ms |
4856 KB |
Output is correct |
70 |
Correct |
36 ms |
4856 KB |
Output is correct |
71 |
Correct |
31 ms |
4856 KB |
Output is correct |
72 |
Incorrect |
28 ms |
4856 KB |
Output isn't correct |
73 |
Incorrect |
30 ms |
4984 KB |
Output isn't correct |
74 |
Incorrect |
53 ms |
4984 KB |
Output isn't correct |
75 |
Correct |
52 ms |
5084 KB |
Output is correct |
76 |
Correct |
46 ms |
4984 KB |
Output is correct |
77 |
Correct |
51 ms |
5112 KB |
Output is correct |
78 |
Correct |
56 ms |
4984 KB |
Output is correct |
79 |
Correct |
46 ms |
4988 KB |
Output is correct |
80 |
Correct |
49 ms |
4984 KB |
Output is correct |
81 |
Correct |
56 ms |
5112 KB |
Output is correct |
82 |
Correct |
46 ms |
5112 KB |
Output is correct |
83 |
Correct |
58 ms |
4984 KB |
Output is correct |
84 |
Correct |
54 ms |
4980 KB |
Output is correct |
85 |
Correct |
60 ms |
4984 KB |
Output is correct |
86 |
Correct |
68 ms |
4984 KB |
Output is correct |
87 |
Correct |
68 ms |
4984 KB |
Output is correct |
88 |
Correct |
60 ms |
4984 KB |
Output is correct |
89 |
Correct |
66 ms |
5112 KB |
Output is correct |
90 |
Correct |
73 ms |
4984 KB |
Output is correct |
91 |
Correct |
73 ms |
4984 KB |
Output is correct |
92 |
Incorrect |
71 ms |
4992 KB |
Output isn't correct |