样例过了 却只有30 求神犇帮助
#include <bits/stdc++.h>
#define init ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
ll n, q[1000001], q1[1000001], a[1001][1001], h, t, ans[1000001];
ll x[2], y[2];
ll dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0};
int main() {
init;
cin >> n;
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= n; j++) {
char c;
cin >> c;
a[i][j] = c - '0';
}
}
cin >> x[0] >> y[0] >> x[1] >> y[1];
t++;
q[t] = x[0];
q1[t] = y[0];
a[x[0]][y[0]] = 1;
while (h < t) {
h++;
for (ll i = 0; i < 4; i++) {
ll xa = q[h]+dx[i], ya = q1[h]+dy[i];
if (xa>0 && ya>0 && xa<=n && ya<=n && !a[xa][ya]) {
t++;
q[t] = xa;
q1[t] = ya;
a[xa][ya] = 1;
ans[t] = ans[h]+1;
if (q[t] == x[1] && q1[t] == y[1]) {
cout << ans[t];
return 0;
}
}
}
}
return 0;
}