#include<bits/stdc++.h>
using namespace std;
const int N = 805, inf = 1e9;
string a[N];
int d[N][N], dis[N][N], n, s;
bool vis[N][N];
pair<int, int> st, en;
int mx = 0;
int movex[] = {1, 0, -1, 0};
int movey[] = {0, 1, 0, -1};
vector<pair<int, int>> allhives;
void fill(){
for(int i = 0; i < 801; i++){
for(int j = 0; j < 801; j++){
d[i][j] = inf;
}
}
}
bool check(int i, int j, bool which){
if(which == 1)
{
if(i >= 0 && i < n && j >= 0 && j < n && (a[i][j] == 'G' || a[i][j] == 'D') && !vis[i][j]){
return 1;
}
return 0;
}
else{
if(i >= 0 && i < n && j >= 0 && j < n && a[i][j] == 'G' && !vis[i][j]){
return 1;
}
return 0;
}
}
void bfs(){
queue<pair<int, int>> q;
for(auto x : allhives){
q.push(x);
d[x.first][x.second] = 0;
}
while(!q.empty()){
pair<int, int> u = q.front(); q.pop();
for(int i1 = 0; i1 < 4; i1++){
int x = movex[i1], y = movey[i1];
if(d[u.first + x][u.second + y] != inf)continue;
if(check(u.first + x, u.second + y, 0)){
d[u.first + x][u.second + y] = d[u.first][u.second] + 1;
q.push({u.first + x, u.second + y});
}
}
}
}
bool possible(int x){
int cur = x + 1;
memset(vis, 0, sizeof vis);
queue<pair<pair<int, int>, pair<int,int>>> q;
q.push({{st.first, st.second}, {cur, s}});
vis[st.first][st.second] = 1;
while(!q.empty()){
auto k = q.front(); q.pop();
int u = k.first.first, v = k.first.second, chargesleft = k.second.second;
int now = k.second.first;
for(int i = 0; i < 4; i++){
if(check(u + movex[i], v + movey[i], 1)){
int need;
if(chargesleft > 0){
need = now;
}else{
need = now + 1;
}
if(d[u + movex[i]][v + movey[i]] >= now){
if(chargesleft > 1){
q.push({{u + movex[i], v + movey[i]}, {now, chargesleft - 1}});
}else{
q.push({{u + movex[i], v + movey[i]}, {now + 1, s}});
}
vis[u + movex[i]][v + movey[i]] = 1;
}
}
}
}
/*cout << "x : " << x << '\n';
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cout << vis[i][j];
}
cout << '\n';
}*/
if(vis[en.first][en.second])return 1;
return 0;
}
int main(){
cin >> n >> s;
fill();
for(int i = 0; i < n; i++){
cin >> a[i];
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(a[i][j] == 'H'){
allhives.push_back({i, j});
}
if(a[i][j] == 'M'){
st.first = i;
st.second = j;
}
if(a[i][j] == 'D'){
en.first = i;
en.second = j;
}
}
}
/*for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(d[i][j] != inf)cout << d[i][j];
else cout << 0;
}
cout << '\n';
}*/
bfs();
int l = 0, r = n * n;
int ans = -1;
while(l <= r){
int mid = l + (r - l + 1) / 2;
if(possible(mid)){
l = mid + 1;
ans = mid;
}else{
r = mid - 1;
}
}
cout << ans << '\n';
}
Compilation message
mecho.cpp: In function 'bool possible(int)':
mecho.cpp:74:9: warning: variable 'need' set but not used [-Wunused-but-set-variable]
int need;
^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
3576 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
3448 KB |
Output isn't correct |
3 |
Incorrect |
5 ms |
3448 KB |
Output isn't correct |
4 |
Incorrect |
5 ms |
3448 KB |
Output isn't correct |
5 |
Correct |
6 ms |
3576 KB |
Output is correct |
6 |
Correct |
5 ms |
3448 KB |
Output is correct |
7 |
Correct |
164 ms |
4472 KB |
Output is correct |
8 |
Incorrect |
5 ms |
3576 KB |
Output isn't correct |
9 |
Correct |
5 ms |
3704 KB |
Output is correct |
10 |
Correct |
5 ms |
3576 KB |
Output is correct |
11 |
Correct |
5 ms |
3448 KB |
Output is correct |
12 |
Incorrect |
6 ms |
3576 KB |
Output isn't correct |
13 |
Incorrect |
5 ms |
3448 KB |
Output isn't correct |
14 |
Correct |
6 ms |
3576 KB |
Output is correct |
15 |
Incorrect |
5 ms |
3576 KB |
Output isn't correct |
16 |
Correct |
5 ms |
3448 KB |
Output is correct |
17 |
Incorrect |
5 ms |
3448 KB |
Output isn't correct |
18 |
Correct |
5 ms |
3448 KB |
Output is correct |
19 |
Incorrect |
5 ms |
3448 KB |
Output isn't correct |
20 |
Correct |
5 ms |
3448 KB |
Output is correct |
21 |
Incorrect |
5 ms |
3448 KB |
Output isn't correct |
22 |
Correct |
5 ms |
3448 KB |
Output is correct |
23 |
Incorrect |
6 ms |
3576 KB |
Output isn't correct |
24 |
Correct |
6 ms |
3576 KB |
Output is correct |
25 |
Incorrect |
6 ms |
3448 KB |
Output isn't correct |
26 |
Correct |
6 ms |
3448 KB |
Output is correct |
27 |
Incorrect |
6 ms |
3448 KB |
Output isn't correct |
28 |
Correct |
6 ms |
3576 KB |
Output is correct |
29 |
Incorrect |
1 ms |
3448 KB |
Output isn't correct |
30 |
Correct |
6 ms |
3448 KB |
Output is correct |
31 |
Incorrect |
6 ms |
3576 KB |
Output isn't correct |
32 |
Correct |
6 ms |
3576 KB |
Output is correct |
33 |
Incorrect |
16 ms |
3704 KB |
Output isn't correct |
34 |
Correct |
18 ms |
3704 KB |
Output is correct |
35 |
Correct |
44 ms |
3704 KB |
Output is correct |
36 |
Incorrect |
19 ms |
3704 KB |
Output isn't correct |
37 |
Correct |
22 ms |
3704 KB |
Output is correct |
38 |
Correct |
56 ms |
3832 KB |
Output is correct |
39 |
Incorrect |
22 ms |
3704 KB |
Output isn't correct |
40 |
Correct |
26 ms |
3708 KB |
Output is correct |
41 |
Correct |
83 ms |
3776 KB |
Output is correct |
42 |
Incorrect |
26 ms |
3960 KB |
Output isn't correct |
43 |
Correct |
29 ms |
3960 KB |
Output is correct |
44 |
Correct |
103 ms |
3960 KB |
Output is correct |
45 |
Incorrect |
31 ms |
4188 KB |
Output isn't correct |
46 |
Correct |
34 ms |
4088 KB |
Output is correct |
47 |
Correct |
111 ms |
4088 KB |
Output is correct |
48 |
Incorrect |
35 ms |
4088 KB |
Output isn't correct |
49 |
Correct |
39 ms |
4088 KB |
Output is correct |
50 |
Correct |
149 ms |
4176 KB |
Output is correct |
51 |
Incorrect |
40 ms |
4088 KB |
Output isn't correct |
52 |
Correct |
42 ms |
4216 KB |
Output is correct |
53 |
Correct |
157 ms |
4216 KB |
Output is correct |
54 |
Incorrect |
46 ms |
4216 KB |
Output isn't correct |
55 |
Correct |
51 ms |
4128 KB |
Output is correct |
56 |
Correct |
179 ms |
4236 KB |
Output is correct |
57 |
Incorrect |
51 ms |
4216 KB |
Output isn't correct |
58 |
Correct |
57 ms |
4152 KB |
Output is correct |
59 |
Correct |
205 ms |
4288 KB |
Output is correct |
60 |
Incorrect |
59 ms |
4324 KB |
Output isn't correct |
61 |
Correct |
63 ms |
4216 KB |
Output is correct |
62 |
Correct |
255 ms |
4216 KB |
Output is correct |
63 |
Correct |
195 ms |
4352 KB |
Output is correct |
64 |
Correct |
291 ms |
4364 KB |
Output is correct |
65 |
Correct |
280 ms |
4396 KB |
Output is correct |
66 |
Incorrect |
219 ms |
4420 KB |
Output isn't correct |
67 |
Correct |
207 ms |
4216 KB |
Output is correct |
68 |
Correct |
103 ms |
4344 KB |
Output is correct |
69 |
Correct |
87 ms |
4344 KB |
Output is correct |
70 |
Correct |
83 ms |
4344 KB |
Output is correct |
71 |
Correct |
76 ms |
4344 KB |
Output is correct |
72 |
Incorrect |
75 ms |
4344 KB |
Output isn't correct |
73 |
Incorrect |
149 ms |
4480 KB |
Output isn't correct |
74 |
Correct |
110 ms |
4480 KB |
Output is correct |
75 |
Correct |
119 ms |
4480 KB |
Output is correct |
76 |
Correct |
114 ms |
4596 KB |
Output is correct |
77 |
Correct |
120 ms |
4608 KB |
Output is correct |
78 |
Correct |
151 ms |
4492 KB |
Output is correct |
79 |
Correct |
96 ms |
4364 KB |
Output is correct |
80 |
Correct |
114 ms |
4460 KB |
Output is correct |
81 |
Correct |
135 ms |
4492 KB |
Output is correct |
82 |
Correct |
118 ms |
4592 KB |
Output is correct |
83 |
Correct |
171 ms |
4404 KB |
Output is correct |
84 |
Correct |
134 ms |
4532 KB |
Output is correct |
85 |
Correct |
129 ms |
4404 KB |
Output is correct |
86 |
Correct |
140 ms |
4376 KB |
Output is correct |
87 |
Correct |
139 ms |
4528 KB |
Output is correct |
88 |
Correct |
158 ms |
4512 KB |
Output is correct |
89 |
Correct |
150 ms |
4468 KB |
Output is correct |
90 |
Correct |
155 ms |
4572 KB |
Output is correct |
91 |
Correct |
181 ms |
4448 KB |
Output is correct |
92 |
Correct |
181 ms |
4444 KB |
Output is correct |