最正宗的题解,100%AC
2026-10-06 11:22:40
发布于:广东
14阅读
0回复
0点赞
话不多说,直接看答案
#include<bits/stdc++.h>
using namespace std;
struct node{
int x,y,s;
};
int n,m,sx,sy,gx,gy,dx[]={0,1,0,-1},dy[]={1,0,-1,0};
char a[55][55];
bool vis[55][55];
void bfs(){
queue<node> q;
q.push({sx,sy,0});
vis[sx][sy]=1;
while(!q.empty()){
node z=q.front();
q.pop();
if(z.x==gx&&z.y==gy){
cout<<z.s;
return;
}
for(int i=0; i<4; i++){
int xx=z.x+dx[i],yy=z.y+dy[i];
if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&vis[xx][yy]==0&&a[xx][yy]=='.'){
vis[xx][yy]=1;
q.push({xx,yy,z.s+1});
}
}
}
}
int main(){
cin>>n>>m>>sx>>sy>>gx>>gy;
for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) cin>>a[i][j];
bfs();
return 0;
}
全部评论 1
good
2026-09-06 来自 浙江
0












有帮助,赞一个