89998
2025-07-30 19:54:01
发布于:广东
#include <iostream>
#include <queue>
using namespace std;
struct node{
int x;
int y;
int d;
};
char mp[50][50];
bool vis[50][50];
int dir[4][2] = { {1,0},{-1,0},{0,-1},{0,1} };
int n,m;
int x1 , y1 , x2 , y2;
int bfs(int x, int y){
queue<node> q;
q.push({x, y, 0});
vis[x][y] = true;
while(!q.empty()){
node t = q.front();
q.pop();
if(t.x == x2&& t.y == y2) return t.d;
for(int i = 0; i < 4; i++){
int nx = t.x + dir[i][0];
int ny = t.y + dir[i][1];
if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && !vis[nx][ny] && mp[nx][ny] != '#'){
q.push({nx, ny, t.d + 1});
vis[nx][ny] = true;
}
}
}
return -1;
}
int main(){
freopen("dawn.in", "r", stdin);
freopen("dawn.out","w",stdout);
cin >> n >>m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <=m; j++){
cin >> mp[i][j];
}
}
cin>>x1>>y1>>x2>>y2;
cout << bfs(x1,y1);
fclose(stdin);
fclose(stdout);
return 0;
}
这里空空如也
有帮助,赞一个