#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;
for(int i = pos.first-N; i<=pos.first+N; i++){
for(int j = pos.second-N; j<=pos.second+N; j++){
pii c= {i, j};
if(is_valid(c)){
if(abs(c.first-pos.first)<=N && abs(c.second-pos.second)<=N){
if(abs(c.first-pos.first)<N || abs(c.second-pos.second)<N){
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+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;
}
void display(){
for(int i = 0; i<R; i++){
for(int j = 0; j<C; j++){
if(maze[i][j]=='.'){
if(vis[i][j]){
cout<<'o';
}
else{
cout<<'.';
}
}
else{
if(vis[i][j]){
cout<<'O';
}
else{
cout<<'#';
}
}
}
cout<<endl;
}
cout<<endl;
}
int BFS(){
deque<pair<pii, int>> dq;
dq.push_back({debut, 0});
vis[debut.first][debut.second] = true;
while(dq.size()>0){
//display();
auto cur= dq.front();
dq.pop_front();
pii cur_pos =cur.first;
int dist = cur.second;
//cout<<dist<<endl;
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, false));
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 |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
756 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
432 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
12 ms |
348 KB |
Output is correct |
20 |
Correct |
7 ms |
348 KB |
Output is correct |
21 |
Correct |
5 ms |
860 KB |
Output is correct |
22 |
Correct |
11 ms |
576 KB |
Output is correct |
23 |
Correct |
9 ms |
348 KB |
Output is correct |
24 |
Correct |
8 ms |
448 KB |
Output is correct |
25 |
Correct |
6 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
564 KB |
Output is correct |
28 |
Correct |
6 ms |
600 KB |
Output is correct |
29 |
Correct |
23 ms |
604 KB |
Output is correct |
30 |
Correct |
7 ms |
348 KB |
Output is correct |
31 |
Correct |
13 ms |
1884 KB |
Output is correct |
32 |
Correct |
32 ms |
604 KB |
Output is correct |
33 |
Correct |
27 ms |
600 KB |
Output is correct |
34 |
Correct |
24 ms |
708 KB |
Output is correct |
35 |
Correct |
16 ms |
708 KB |
Output is correct |
36 |
Correct |
18 ms |
860 KB |
Output is correct |
37 |
Correct |
24 ms |
860 KB |
Output is correct |
38 |
Correct |
6 ms |
604 KB |
Output is correct |
39 |
Correct |
233 ms |
3728 KB |
Output is correct |
40 |
Correct |
18 ms |
784 KB |
Output is correct |
41 |
Correct |
17 ms |
672 KB |
Output is correct |
42 |
Correct |
37 ms |
856 KB |
Output is correct |
43 |
Correct |
27 ms |
856 KB |
Output is correct |
44 |
Correct |
63 ms |
1884 KB |
Output is correct |
45 |
Correct |
62 ms |
2104 KB |
Output is correct |
46 |
Correct |
92 ms |
9800 KB |
Output is correct |
47 |
Correct |
285 ms |
3744 KB |
Output is correct |
48 |
Correct |
173 ms |
3696 KB |
Output is correct |
49 |
Correct |
182 ms |
3732 KB |
Output is correct |
50 |
Correct |
123 ms |
3728 KB |
Output is correct |
51 |
Correct |
235 ms |
4368 KB |
Output is correct |
52 |
Correct |
169 ms |
4220 KB |
Output is correct |
53 |
Correct |
221 ms |
3600 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 |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
756 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
432 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
12 ms |
348 KB |
Output is correct |
20 |
Correct |
7 ms |
348 KB |
Output is correct |
21 |
Correct |
5 ms |
860 KB |
Output is correct |
22 |
Correct |
11 ms |
576 KB |
Output is correct |
23 |
Correct |
9 ms |
348 KB |
Output is correct |
24 |
Correct |
8 ms |
448 KB |
Output is correct |
25 |
Correct |
6 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
564 KB |
Output is correct |
28 |
Correct |
6 ms |
600 KB |
Output is correct |
29 |
Correct |
23 ms |
604 KB |
Output is correct |
30 |
Correct |
7 ms |
348 KB |
Output is correct |
31 |
Correct |
13 ms |
1884 KB |
Output is correct |
32 |
Correct |
32 ms |
604 KB |
Output is correct |
33 |
Correct |
27 ms |
600 KB |
Output is correct |
34 |
Correct |
24 ms |
708 KB |
Output is correct |
35 |
Correct |
16 ms |
708 KB |
Output is correct |
36 |
Correct |
18 ms |
860 KB |
Output is correct |
37 |
Correct |
24 ms |
860 KB |
Output is correct |
38 |
Correct |
6 ms |
604 KB |
Output is correct |
39 |
Correct |
233 ms |
3728 KB |
Output is correct |
40 |
Correct |
18 ms |
784 KB |
Output is correct |
41 |
Correct |
17 ms |
672 KB |
Output is correct |
42 |
Correct |
37 ms |
856 KB |
Output is correct |
43 |
Correct |
27 ms |
856 KB |
Output is correct |
44 |
Correct |
63 ms |
1884 KB |
Output is correct |
45 |
Correct |
62 ms |
2104 KB |
Output is correct |
46 |
Correct |
92 ms |
9800 KB |
Output is correct |
47 |
Correct |
285 ms |
3744 KB |
Output is correct |
48 |
Correct |
173 ms |
3696 KB |
Output is correct |
49 |
Correct |
182 ms |
3732 KB |
Output is correct |
50 |
Correct |
123 ms |
3728 KB |
Output is correct |
51 |
Correct |
235 ms |
4368 KB |
Output is correct |
52 |
Correct |
169 ms |
4220 KB |
Output is correct |
53 |
Correct |
221 ms |
3600 KB |
Output is correct |
54 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
55 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
756 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
432 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
12 ms |
348 KB |
Output is correct |
20 |
Correct |
7 ms |
348 KB |
Output is correct |
21 |
Correct |
5 ms |
860 KB |
Output is correct |
22 |
Correct |
11 ms |
576 KB |
Output is correct |
23 |
Correct |
9 ms |
348 KB |
Output is correct |
24 |
Correct |
8 ms |
448 KB |
Output is correct |
25 |
Correct |
6 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
564 KB |
Output is correct |
28 |
Correct |
6 ms |
600 KB |
Output is correct |
29 |
Correct |
23 ms |
604 KB |
Output is correct |
30 |
Correct |
7 ms |
348 KB |
Output is correct |
31 |
Correct |
13 ms |
1884 KB |
Output is correct |
32 |
Correct |
32 ms |
604 KB |
Output is correct |
33 |
Correct |
27 ms |
600 KB |
Output is correct |
34 |
Correct |
24 ms |
708 KB |
Output is correct |
35 |
Correct |
16 ms |
708 KB |
Output is correct |
36 |
Correct |
18 ms |
860 KB |
Output is correct |
37 |
Correct |
24 ms |
860 KB |
Output is correct |
38 |
Correct |
6 ms |
604 KB |
Output is correct |
39 |
Correct |
233 ms |
3728 KB |
Output is correct |
40 |
Correct |
18 ms |
784 KB |
Output is correct |
41 |
Correct |
17 ms |
672 KB |
Output is correct |
42 |
Correct |
37 ms |
856 KB |
Output is correct |
43 |
Correct |
27 ms |
856 KB |
Output is correct |
44 |
Correct |
63 ms |
1884 KB |
Output is correct |
45 |
Correct |
62 ms |
2104 KB |
Output is correct |
46 |
Correct |
92 ms |
9800 KB |
Output is correct |
47 |
Correct |
285 ms |
3744 KB |
Output is correct |
48 |
Correct |
173 ms |
3696 KB |
Output is correct |
49 |
Correct |
182 ms |
3732 KB |
Output is correct |
50 |
Correct |
123 ms |
3728 KB |
Output is correct |
51 |
Correct |
235 ms |
4368 KB |
Output is correct |
52 |
Correct |
169 ms |
4220 KB |
Output is correct |
53 |
Correct |
221 ms |
3600 KB |
Output is correct |
54 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
55 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
756 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
432 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
12 ms |
348 KB |
Output is correct |
20 |
Correct |
7 ms |
348 KB |
Output is correct |
21 |
Correct |
5 ms |
860 KB |
Output is correct |
22 |
Correct |
11 ms |
576 KB |
Output is correct |
23 |
Correct |
9 ms |
348 KB |
Output is correct |
24 |
Correct |
8 ms |
448 KB |
Output is correct |
25 |
Correct |
6 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
3 ms |
564 KB |
Output is correct |
28 |
Correct |
6 ms |
600 KB |
Output is correct |
29 |
Correct |
23 ms |
604 KB |
Output is correct |
30 |
Correct |
7 ms |
348 KB |
Output is correct |
31 |
Correct |
13 ms |
1884 KB |
Output is correct |
32 |
Correct |
32 ms |
604 KB |
Output is correct |
33 |
Correct |
27 ms |
600 KB |
Output is correct |
34 |
Correct |
24 ms |
708 KB |
Output is correct |
35 |
Correct |
16 ms |
708 KB |
Output is correct |
36 |
Correct |
18 ms |
860 KB |
Output is correct |
37 |
Correct |
24 ms |
860 KB |
Output is correct |
38 |
Correct |
6 ms |
604 KB |
Output is correct |
39 |
Correct |
233 ms |
3728 KB |
Output is correct |
40 |
Correct |
18 ms |
784 KB |
Output is correct |
41 |
Correct |
17 ms |
672 KB |
Output is correct |
42 |
Correct |
37 ms |
856 KB |
Output is correct |
43 |
Correct |
27 ms |
856 KB |
Output is correct |
44 |
Correct |
63 ms |
1884 KB |
Output is correct |
45 |
Correct |
62 ms |
2104 KB |
Output is correct |
46 |
Correct |
92 ms |
9800 KB |
Output is correct |
47 |
Correct |
285 ms |
3744 KB |
Output is correct |
48 |
Correct |
173 ms |
3696 KB |
Output is correct |
49 |
Correct |
182 ms |
3732 KB |
Output is correct |
50 |
Correct |
123 ms |
3728 KB |
Output is correct |
51 |
Correct |
235 ms |
4368 KB |
Output is correct |
52 |
Correct |
169 ms |
4220 KB |
Output is correct |
53 |
Correct |
221 ms |
3600 KB |
Output is correct |
54 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
55 |
Halted |
0 ms |
0 KB |
- |