#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 = 0; i<R; i++){
for(int j = 0; j<C; j++){
pii c= {i, j};
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));
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 |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
432 KB |
Output is correct |
8 |
Correct |
1 ms |
436 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 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 |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1802 ms |
820 KB |
Output is correct |
20 |
Correct |
1581 ms |
548 KB |
Output is correct |
21 |
Correct |
464 ms |
848 KB |
Output is correct |
22 |
Correct |
1892 ms |
588 KB |
Output is correct |
23 |
Execution timed out |
2075 ms |
564 KB |
Time limit exceeded |
24 |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
432 KB |
Output is correct |
8 |
Correct |
1 ms |
436 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 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 |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1802 ms |
820 KB |
Output is correct |
20 |
Correct |
1581 ms |
548 KB |
Output is correct |
21 |
Correct |
464 ms |
848 KB |
Output is correct |
22 |
Correct |
1892 ms |
588 KB |
Output is correct |
23 |
Execution timed out |
2075 ms |
564 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
432 KB |
Output is correct |
8 |
Correct |
1 ms |
436 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 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 |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1802 ms |
820 KB |
Output is correct |
20 |
Correct |
1581 ms |
548 KB |
Output is correct |
21 |
Correct |
464 ms |
848 KB |
Output is correct |
22 |
Correct |
1892 ms |
588 KB |
Output is correct |
23 |
Execution timed out |
2075 ms |
564 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
432 KB |
Output is correct |
8 |
Correct |
1 ms |
436 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 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 |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1802 ms |
820 KB |
Output is correct |
20 |
Correct |
1581 ms |
548 KB |
Output is correct |
21 |
Correct |
464 ms |
848 KB |
Output is correct |
22 |
Correct |
1892 ms |
588 KB |
Output is correct |
23 |
Execution timed out |
2075 ms |
564 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |