#include <bits/stdc++.h>
using namespace std;
int n, sx, sy, fx, fy, ans;
char a[5005][5005];
bool vis[5005][5005];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct node
{
int x, y, step;
};
int bfs(int x, int y)
{
queue<node> q;
q.push(node{x, y, 0});
vis[x][y] = 1;
while(!q.empty())
{
node t;
t = q.front();
q.pop();
if(t.x == fx && t.y == fy)
return t.step;
int tx, ty;
for(int i = 0; i < 3; i++)
{
tx = t.x + dx[i];
ty = t.y + dy[i];
if(tx >= 1 && tx <= n && ty >= 1 && ty <= n)
{
if(vis[tx][ty] == 0 && a[tx][ty] == '0')
{
q.push(node{tx, ty, t.step + 1});
vis[tx][ty] = 1;
}
}
}
}
return 2147483647;
}
int main()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
cin >> a[i][j];
}
}
cin >> sx >> sy >> fx >> fy;
vis[sx][sy] = 1;
ans = bfs(sx, sy);
cout << ans;
return 0;
}
思路为正常bfs思路