#include <bits/stdc++.h>
using namespace std;
int R;
int C;
int N;
vector<pair<int,int>> trans = {{-1,0},{1,0},{0,1},{0,-1}};
vector<pair<int,int>> dtrans = {{-1,0},{1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
queue<pair<int,int>> prop;
queue<pair<int,int>> fprop;
int sx,sy;
int gx,gy;
bool valid(int i, int j){
if(i < 0 || i >= R) return false;
if(j < 0 || j >= C) return false;
return true;
}
int main(){
scanf(" %d",&R);
scanf(" %d",&C);
scanf(" %d",&N);
bool visited[R + 5][C + 5];
bool visited1[R + 5][C + 5];
char lst[R + 5][C + 5];
int d[R + 5][C + 5];
scanf(" %d",&sx);
scanf(" %d",&sy);
sx--;
sy--;
scanf(" %d",&gx);
scanf(" %d",&gy);
gx--;
gy--;
for(int i = 0; i < R; i++){
for(int j = 0; j < C; j++){
scanf(" %c",&lst[i][j]);
if(lst[i][j] == '.'){
lst[i][j] = 1;
}
else{
lst[i][j] = 0;
}
d[i][j] = -1;
visited[i][j] = false;
visited1[i][j] = false;
}
}
d[sx][sy] = 0;
prop.push({sx,sy});
visited[sx][sy] = true;
int cont = 0;
while(d[gx][gy] == -1){// printf("%d\n",d[gx][gy]);
vector<pair<int,int>> v;
/*for(int i = 0; i < R; i++){
for(int j = 0; j < C; j++){
printf("%2d ",d[i][j]);
}
printf("\n");
}*/
while(!prop.empty()){
pair<int,int> ii = prop.front();
prop.pop();
v.push_back(ii);
int i = ii.first;
int j = ii.second;
visited1[i][j] = true;
for(pair<int,int> ii : trans){
int ni = ii.first + i;
int nj = ii.second + j;
if(!valid(ni,nj) || visited[ni][nj] || !lst[ni][nj]) continue;
prop.push({ni,nj});
visited[ni][nj] = true;
d[ni][nj] = 1;
}
}
cont++;
for(int i = 0; i < N - 1; i++){
while(!v.empty()){
fprop.push({v.back()});
//printf("%d %d\n",v.back().first,v.back().second);
v.pop_back();
}
while(!fprop.empty()){
pair<int,int> ii = fprop.front();
fprop.pop();
int i = ii.first;
int j = ii.second;
for(pair<int,int> ii : dtrans){
int ni = ii.first + i;
int nj = ii.second + j;
if(!valid(ni,nj) || visited[ni][nj]) continue;
v.push_back({ni,nj});
prop.push({ni,nj});
visited[ni][nj] = true;
visited1[ni][nj] = true;
d[ni][nj] = cont;
}
}
}
while(!v.empty()){
fprop.push({v.back()});
//printf("s");
v.pop_back();
}
while(!fprop.empty()){
pair<int,int> ii = fprop.front();
fprop.pop();
int i = ii.first;
int j = ii.second;
for(pair<int,int> ii : dtrans){
int ni = ii.first + i;
int nj = ii.second + j;
if(!valid(ni,nj) || visited[ni][nj]) continue;
prop.push({ni,nj});
visited[ni][nj] = true;
visited1[ni][nj] = true;
d[ni][nj] = cont;
}
}
}
printf("%d",d[gx][gy]);
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int main()':
Main.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf(" %d",&R);
| ~~~~~^~~~~~~~~~
Main.cpp:23:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | scanf(" %d",&C);
| ~~~~~^~~~~~~~~~
Main.cpp:24:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | scanf(" %d",&N);
| ~~~~~^~~~~~~~~~
Main.cpp:31:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | scanf(" %d",&sx);
| ~~~~~^~~~~~~~~~~
Main.cpp:32:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | scanf(" %d",&sy);
| ~~~~~^~~~~~~~~~~
Main.cpp:37:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | scanf(" %d",&gx);
| ~~~~~^~~~~~~~~~~
Main.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf(" %d",&gy);
| ~~~~~^~~~~~~~~~~
Main.cpp:45:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf(" %c",&lst[i][j]);
| ~~~~~^~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |