文章
8
粉丝
14
获赞
7
访问
648
注意每次输入都要对两个数组进行初始化操作memset,同时可以防止数组越界。
#include<bits/stdc++.h>
using namespace std;
const int N = 105;
char g[N][N];
int d[N][N];
int h,w;
int bfs(int x,int y){
int dis = 0;
queue<pair<int,int>> q;
memset(d,-1,sizeof(d));
int dx[4] = {-1,1,0,0};
int dy[4] = {0,0,-1,1};
q.push({x,y});
d[x][y] = 0;
while(q.size()){
auto u = q.front();
q.pop();
if(g[u.first][u.second] == 'E'){
dis = d[u.first][u.second];
break;
}
//上下左右
for(int i = 0;i < 4;i ++){
int new_x = u.first + dx[i];
int new_y = u.second + dy[i];
if((g[new_x][new_y] == 'E' || g[new_x][new_y] == '*') && d[new_x][new_y] == -1){
q.push({new_x,new_y});
d[new_x][new_y] = d[u.first][u.second] + 1;
}
}
}
if(!dis) return -1;
else return dis;
}
int main(){
...
登录后发布评论
暂无评论,来抢沙发