文章
6
粉丝
0
获赞
10
访问
150
#include <bits/stdc++.h>
using namespace std;
const int MAX=110;
char maze[MAX][MAX];//迷宫
int h,w;//迷宫宽和高;
int sx,sy;//记录s的坐标
int dx[4]={-1,1,0,0};// 上下;
int dy[4]={0,0,-1,1};// 左右
int bfs(){
queue<pair<int,int>> q;// 存储x,y坐标,bfs使用队列
int steps[MAX][MAX]={}; // 记录到达x,y的步数
bool visited[MAX][MAX]={};//记录是否访问过
q.push({sx,sy});
visited[sx][sy]=true;
steps[sx][sy]=0;
while(!q.empty()){
pair<int,int>p=q.front();
int x=p.first,y=p.second;
q.pop();
if(maze[x][y]=='E'){
return steps [x][y];
}
// 遍历四个方向
for(int i=0;i<4;i++){
int nx=x+dx[i];
int ny=y+dy[i];
if(nx>=0&&nx<h&&ny>=0&&ny<w&&maze[nx][ny]!='#'&&!visited[nx][ny]){
visited[nx][ny]=true;
steps[nx][ny]=steps[x][y]+1;
q.push({nx,ny});
}
}
}
return -1;
}
int main() {
while(cin>>h>>w&&(h!=0||w!=0)){
for(int i=0;i<h;i++){
for(int j=0;j...
登录后发布评论
暂无评论,来抢沙发