BFS 实现
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
vector<vector<int>> movelist{{0,-1},{0,1},{-1,0},{1,0}};
int n, m;
cin >> n >> m;
int sx, sy, tx, ty;
cin >> sx >> sy >> tx >> ty;
sx--;
sy--;
tx--;
ty--;
queue<pair<int, int>> q{};
int res = 0;
q.push(make_pair(sx, sy));
vector<vector<char>> num(n, vector<char>(m, ' '));
for (int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> num[i][j];
}
}
while (!q.empty()) {
int qlength = q.size();
for (int i = 0; i < qlength; i++) {
auto qr = q.front();
q.pop();
int x = qr.first;
int y = qr.second;
if(x == tx && y == ty) {
cout << res << endl;
return 0;
}
for (int i = 0; i < 4; i++) {
int nx = x + movelist[i][0];
int ny = y + movelist[i][1];
if(nx >= 0 && nx < n && ny >= 0 && ny < m && num[nx][ny] == '.') {
num[nx][ny] = '*';
q.push(make_pair(nx, ny));
}
}
}
res++;
}
cout << -1 << endl;
return 0;
}