走迷宫
作者:
zplzh
,
2024-04-02 11:27:39
,
所有人可见
,
阅读 5
#include <bits/stdc++.h>
#define int long long
#define xx first
#define yy second
using namespace std;
const int N = 110;
typedef pair<int , int> PII;
queue<PII> q;
int n, m, a, b, c, h;
int g[N][N];
int d[N][N];
int bfs() {
memset(d, -1, sizeof(d));
d[a][b] = 0;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
while (q.size()) {
auto t = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int x = t.xx + dx[i];
int y = t.yy + dy[i];
if (x >= 1 && x <= n && y >= 1 && y <= m && g[x][y] == 1 && d[x][y] == -1) {
d[x][y] = d[t.xx][t.yy] + 1;
q.push({x, y});
if (c == x && y == h) return d[x][y];
}
}
}
return -1;
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> g[i][j];
cin >> a >> b >> c >> h;
q.push(make_pair(a,b));
cout << bfs() << endl;
}
signed main() {
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}