ESxyz @ 2023-07-12 14:23:24
#include <bits/stdc++.h>
using namespace std;
char aa[1005];
struct xy{
int x,y;
}node,Top;
const int dx[4]={1,-1,0,0};
const int dy[4]={0,-0,1,-1};
int a[401][401];
bool b[401][401];
int n,m;
void bfs(int x,int y,int step){
if(x==n&&y==m)
return;
a[x][y] = step;
b[x][y] = true;
queue<xy> Q;
node.x = x;
node.y = y;
Q.push(node);
while (!Q.empty()){
Top=Q.front();
Q.pop();
for (int i=0;i<4;i++){
int NewX=Top.x+dx[i];
int NewY=Top.y+dy[i];
if (NewX<1||NewX>n||NewY<1||NewY>m) continue;
if (!b[NewX][NewY]){
a[NewX][NewY]=a[Top.x][Top.y]+1;
b[NewX][NewY]=true;
node.x=NewX;
node.y=NewY;
Q.push(node);
}
}
}
}
int main(){
int x,y;
cin>>n;
for(int zx=0;zx<n;zx++){
cin>>aa;
for(int zy=0;zy<n;zy++)if(aa[zy]=='1')b[zx+1][zy+1]=true;
}
cin>>x>>y>>n>>m;
bfs(x,y,0);
cout<<a[n][m];
return 0;
}
by Aakkosetsumussa @ 2023-07-12 14:48:23
@ESxyz
27行错了
然后还有数组太小了
改一下就过了
#include <bits/stdc++.h>
using namespace std;
char aa[1005];
struct xy {
int x,y;
} node,Top;
const int dx[4]= {1,-1,0,0};
const int dy[4]= {0,-0,1,-1};
int a[4001][4001];
bool b[4001][4001];
int N,n,m;
void bfs(int x,int y,int step) {
if(x==n&&y==m)
return;
a[x][y] = step;
b[x][y] = true;
queue<xy> Q;
node.x = x;
node.y = y;
Q.push(node);
while (!Q.empty()) {
Top=Q.front();
Q.pop();
for (int i=0; i<4; i++) {
int NewX=Top.x+dx[i];
int NewY=Top.y+dy[i];
if (NewX<1||NewX>N||NewY<1||NewY>N) continue;
if (!b[NewX][NewY]) {
a[NewX][NewY]=a[Top.x][Top.y]+1;
b[NewX][NewY]=true;
node.x=NewX;
node.y=NewY;
Q.push(node);
}
}
}
}
int main() {
int x,y;
cin>>N;
for(int zx=0; zx<N; zx++) {
cin>>aa;
for(int zy=0; zy<N; zy++)if(aa[zy]=='1')b[zx+1][zy+1]=true;
}
cin>>x>>y>>n>>m;
bfs(x,y,0);
cout<<a[n][m];
return 0;
}
by ESxyz @ 2023-07-14 10:49:47
@Aakkosetsumussa 懂了谢谢