linwanzhou @ 2024-07-28 10:51:45
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
char a[N][N];
int st[N][N];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
struct node{
int x, y, z;
};
int main(){
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
int sx, sy, ex, ey;
cin >> sx >> sy >> ex >> ey;
sx --;
sy --;
ex --;
ey --;
queue<node>q;
q.push({sx, sy, 0});
st[sx][sy] = 1;
while(q.size()) {
int x = q.front().x, y = q.front().y, z = q.front().z;
q.pop();
if (x == ex && y == ey) {
cout << z << endl;
return 0;
}
for (int i = 0; i < 4; i++) {
int tx = x + dx[i], ty = y + dy[i];
if (tx < 0 || tx >= n || ty < 0 || ty >= n) continue;
if (a[tx][ty] == '1') continue;
if (st[tx][ty]) continue;
st[tx][sy] = 1;
q.push({tx, ty, z + 1});
}
}
return 0;
}