广度MLE搜索
2026-08-30 15:01:19
发布于:北京
3阅读
0回复
0点赞
可以用结构体来记数
#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
char a[N][N];
struct node{
int x,y;
int len;
};
int r,c;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
bool check(int x,int y){
return x>0&&x<=r&&y>0&&y<=c;
}
bool vis[N][N];
queue<node> q;
void bfs(){
q.push({1,1,1});
vis[1][1]=1;
while(!q.empty()){
node u=q.front();
q.pop();
if(u.x==r&&u.y==c){cout<<u.len;return;}
for(int i=0;i<4;i++){
int nx=u.x+dx[i];
int ny=u.y+dy[i];
if(vis[nx][ny]==0&&check(nx,ny)&&a[nx][ny]=='.'){
q.push({nx,ny,u.len+1});
vis[nx][ny]=1;
}
}
}
}
int main(){
cin>>r>>c;
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
cin>>a[i][j];
}
}
bfs();
return 0;
}
这里空空如也







有帮助,赞一个