#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
int R, C;
int N;
pii debut;
pii fin;
vector<vector<char>> maze;
vector<vector<bool>> vis;
pii delta[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
bool is_valid(pii pos){
if(pos.first>=R || pos.first< 0){
return false;
}
if(pos.second>=C || pos.second<0){
return false;
}
return true;
}
vector<pii> get_free(pii pos){
vector<pii> res;
for(auto d: delta){
pii c= {pos.first+d.first, pos.second+d.second};
if(is_valid(c) && maze[c.first][c.second] != '#'){
res.push_back(c);
}
}
return res;
}
vector<pii> get_paid(pii pos){
vector<pii> res;
if(abs(fin.first-pos.first)<=N && abs(fin.second-pos.second)<=N){
if(abs(fin.first-pos.first)<N || abs(fin.second-pos.second)<N){
res.push_back(fin);
}
}
for(int i= -N; i<=N; i+=2*N){
for(int j = -(N-1); j<=N-1; j++){
pii c = {pos.first+i, pos.second+j};
if(is_valid(c)){
res.push_back(c);
}
}
}
for(int i= -N; i<=N; i+=2*N){
for(int j = -(N-1); j<=N-1; j++){
pii c = {pos.first+j, pos.second+i};
if(is_valid(c)){
res.push_back(c);
}
}
}
return res;
}
int BFS(){
deque<pair<pii, int>> dq;
dq.push_back({debut, 0});
vis[debut.first][debut.second] = true;
while(dq.size()>0){
auto cur= dq.front();
dq.pop_front();
pii cur_pos =cur.first;
int dist = cur.second;
if(cur_pos == fin){
return dist;
}
for(auto e: get_free(cur_pos)){
if(!vis[e.first][e.second]){
vis[e.first][e.second] = true;
dq.push_front({e, dist});
}
}
for(auto e: get_paid(cur_pos)){
if(!vis[e.first][e.second]){
vis[e.first][e.second] = true;
dq.push_back({e, dist+1});
}
}
}
return 1e9;
}
signed main(){
cin>>R>>C;
cin>>N;
maze.resize(R, vector<char> (C));
vis.resize(R, vector<bool>(C));
cin>>debut.first>>debut.second;
debut.first--;
debut.second--;
cin>>fin.first>>fin.second;
fin.first--;
fin.second--;
for(int i = 0; i<R; i++){
for(int j = 0; j<C; j++){
cin>>maze[i][j];
}
}
cout<<BFS()<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
432 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
7 ms |
584 KB |
Output is correct |
20 |
Correct |
4 ms |
348 KB |
Output is correct |
21 |
Correct |
4 ms |
860 KB |
Output is correct |
22 |
Correct |
9 ms |
604 KB |
Output is correct |
23 |
Correct |
8 ms |
348 KB |
Output is correct |
24 |
Correct |
7 ms |
448 KB |
Output is correct |
25 |
Correct |
5 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
604 KB |
Output is correct |
28 |
Correct |
5 ms |
604 KB |
Output is correct |
29 |
Correct |
19 ms |
604 KB |
Output is correct |
30 |
Correct |
9 ms |
348 KB |
Output is correct |
31 |
Correct |
12 ms |
1884 KB |
Output is correct |
32 |
Correct |
27 ms |
804 KB |
Output is correct |
33 |
Correct |
23 ms |
604 KB |
Output is correct |
34 |
Correct |
20 ms |
708 KB |
Output is correct |
35 |
Correct |
13 ms |
708 KB |
Output is correct |
36 |
Correct |
16 ms |
860 KB |
Output is correct |
37 |
Correct |
21 ms |
856 KB |
Output is correct |
38 |
Correct |
6 ms |
604 KB |
Output is correct |
39 |
Correct |
190 ms |
3716 KB |
Output is correct |
40 |
Correct |
12 ms |
600 KB |
Output is correct |
41 |
Correct |
13 ms |
852 KB |
Output is correct |
42 |
Correct |
40 ms |
860 KB |
Output is correct |
43 |
Correct |
26 ms |
856 KB |
Output is correct |
44 |
Correct |
64 ms |
2236 KB |
Output is correct |
45 |
Correct |
61 ms |
1976 KB |
Output is correct |
46 |
Correct |
81 ms |
9808 KB |
Output is correct |
47 |
Correct |
230 ms |
3668 KB |
Output is correct |
48 |
Correct |
144 ms |
3664 KB |
Output is correct |
49 |
Correct |
154 ms |
3728 KB |
Output is correct |
50 |
Correct |
104 ms |
3728 KB |
Output is correct |
51 |
Correct |
192 ms |
4220 KB |
Output is correct |
52 |
Correct |
141 ms |
3972 KB |
Output is correct |
53 |
Correct |
162 ms |
3796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
432 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
7 ms |
584 KB |
Output is correct |
20 |
Correct |
4 ms |
348 KB |
Output is correct |
21 |
Correct |
4 ms |
860 KB |
Output is correct |
22 |
Correct |
9 ms |
604 KB |
Output is correct |
23 |
Correct |
8 ms |
348 KB |
Output is correct |
24 |
Correct |
7 ms |
448 KB |
Output is correct |
25 |
Correct |
5 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
604 KB |
Output is correct |
28 |
Correct |
5 ms |
604 KB |
Output is correct |
29 |
Correct |
19 ms |
604 KB |
Output is correct |
30 |
Correct |
9 ms |
348 KB |
Output is correct |
31 |
Correct |
12 ms |
1884 KB |
Output is correct |
32 |
Correct |
27 ms |
804 KB |
Output is correct |
33 |
Correct |
23 ms |
604 KB |
Output is correct |
34 |
Correct |
20 ms |
708 KB |
Output is correct |
35 |
Correct |
13 ms |
708 KB |
Output is correct |
36 |
Correct |
16 ms |
860 KB |
Output is correct |
37 |
Correct |
21 ms |
856 KB |
Output is correct |
38 |
Correct |
6 ms |
604 KB |
Output is correct |
39 |
Correct |
190 ms |
3716 KB |
Output is correct |
40 |
Correct |
12 ms |
600 KB |
Output is correct |
41 |
Correct |
13 ms |
852 KB |
Output is correct |
42 |
Correct |
40 ms |
860 KB |
Output is correct |
43 |
Correct |
26 ms |
856 KB |
Output is correct |
44 |
Correct |
64 ms |
2236 KB |
Output is correct |
45 |
Correct |
61 ms |
1976 KB |
Output is correct |
46 |
Correct |
81 ms |
9808 KB |
Output is correct |
47 |
Correct |
230 ms |
3668 KB |
Output is correct |
48 |
Correct |
144 ms |
3664 KB |
Output is correct |
49 |
Correct |
154 ms |
3728 KB |
Output is correct |
50 |
Correct |
104 ms |
3728 KB |
Output is correct |
51 |
Correct |
192 ms |
4220 KB |
Output is correct |
52 |
Correct |
141 ms |
3972 KB |
Output is correct |
53 |
Correct |
162 ms |
3796 KB |
Output is correct |
54 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
55 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
432 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
7 ms |
584 KB |
Output is correct |
20 |
Correct |
4 ms |
348 KB |
Output is correct |
21 |
Correct |
4 ms |
860 KB |
Output is correct |
22 |
Correct |
9 ms |
604 KB |
Output is correct |
23 |
Correct |
8 ms |
348 KB |
Output is correct |
24 |
Correct |
7 ms |
448 KB |
Output is correct |
25 |
Correct |
5 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
604 KB |
Output is correct |
28 |
Correct |
5 ms |
604 KB |
Output is correct |
29 |
Correct |
19 ms |
604 KB |
Output is correct |
30 |
Correct |
9 ms |
348 KB |
Output is correct |
31 |
Correct |
12 ms |
1884 KB |
Output is correct |
32 |
Correct |
27 ms |
804 KB |
Output is correct |
33 |
Correct |
23 ms |
604 KB |
Output is correct |
34 |
Correct |
20 ms |
708 KB |
Output is correct |
35 |
Correct |
13 ms |
708 KB |
Output is correct |
36 |
Correct |
16 ms |
860 KB |
Output is correct |
37 |
Correct |
21 ms |
856 KB |
Output is correct |
38 |
Correct |
6 ms |
604 KB |
Output is correct |
39 |
Correct |
190 ms |
3716 KB |
Output is correct |
40 |
Correct |
12 ms |
600 KB |
Output is correct |
41 |
Correct |
13 ms |
852 KB |
Output is correct |
42 |
Correct |
40 ms |
860 KB |
Output is correct |
43 |
Correct |
26 ms |
856 KB |
Output is correct |
44 |
Correct |
64 ms |
2236 KB |
Output is correct |
45 |
Correct |
61 ms |
1976 KB |
Output is correct |
46 |
Correct |
81 ms |
9808 KB |
Output is correct |
47 |
Correct |
230 ms |
3668 KB |
Output is correct |
48 |
Correct |
144 ms |
3664 KB |
Output is correct |
49 |
Correct |
154 ms |
3728 KB |
Output is correct |
50 |
Correct |
104 ms |
3728 KB |
Output is correct |
51 |
Correct |
192 ms |
4220 KB |
Output is correct |
52 |
Correct |
141 ms |
3972 KB |
Output is correct |
53 |
Correct |
162 ms |
3796 KB |
Output is correct |
54 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
55 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
432 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
7 ms |
584 KB |
Output is correct |
20 |
Correct |
4 ms |
348 KB |
Output is correct |
21 |
Correct |
4 ms |
860 KB |
Output is correct |
22 |
Correct |
9 ms |
604 KB |
Output is correct |
23 |
Correct |
8 ms |
348 KB |
Output is correct |
24 |
Correct |
7 ms |
448 KB |
Output is correct |
25 |
Correct |
5 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
604 KB |
Output is correct |
28 |
Correct |
5 ms |
604 KB |
Output is correct |
29 |
Correct |
19 ms |
604 KB |
Output is correct |
30 |
Correct |
9 ms |
348 KB |
Output is correct |
31 |
Correct |
12 ms |
1884 KB |
Output is correct |
32 |
Correct |
27 ms |
804 KB |
Output is correct |
33 |
Correct |
23 ms |
604 KB |
Output is correct |
34 |
Correct |
20 ms |
708 KB |
Output is correct |
35 |
Correct |
13 ms |
708 KB |
Output is correct |
36 |
Correct |
16 ms |
860 KB |
Output is correct |
37 |
Correct |
21 ms |
856 KB |
Output is correct |
38 |
Correct |
6 ms |
604 KB |
Output is correct |
39 |
Correct |
190 ms |
3716 KB |
Output is correct |
40 |
Correct |
12 ms |
600 KB |
Output is correct |
41 |
Correct |
13 ms |
852 KB |
Output is correct |
42 |
Correct |
40 ms |
860 KB |
Output is correct |
43 |
Correct |
26 ms |
856 KB |
Output is correct |
44 |
Correct |
64 ms |
2236 KB |
Output is correct |
45 |
Correct |
61 ms |
1976 KB |
Output is correct |
46 |
Correct |
81 ms |
9808 KB |
Output is correct |
47 |
Correct |
230 ms |
3668 KB |
Output is correct |
48 |
Correct |
144 ms |
3664 KB |
Output is correct |
49 |
Correct |
154 ms |
3728 KB |
Output is correct |
50 |
Correct |
104 ms |
3728 KB |
Output is correct |
51 |
Correct |
192 ms |
4220 KB |
Output is correct |
52 |
Correct |
141 ms |
3972 KB |
Output is correct |
53 |
Correct |
162 ms |
3796 KB |
Output is correct |
54 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
55 |
Halted |
0 ms |
0 KB |
- |