Description–
解题思路–
暴搜ヽ(o_ _)o
代码–
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
const int f[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int n, ans, k[5], b[1005][1005];
bool a[1005][1005];
char c;
struct hll
{
int x, y;
};
queue <hll> q;
void bfs()
{
while (!q.empty())
{
hll u = q.front();
q.pop();
for (int i = 0; i <= 3; ++i)
{
int xx = u.x + f[i][0];
int yy = u.y + f[i][1];
if (xx > n || yy > n || xx <= 0 || yy <= 0) continue;
if (a[xx][yy] || b[xx][yy]) continue;
q.push((hll){xx, yy}), b[xx][yy] = b[u.x][u.y] + 1;
if (xx == k[3] && yy == k[4])
{
printf("%d", b[xx][yy] - 1);
return ;
}
}
}
}
int main()
{
scanf("%d", &n), ans = n * n;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
c = getchar();
while (c != '1' && c != '0')
c = getchar();
a[i][j] = c - '0';
}
}
scanf("%d%d%d%d", &k[1], &k[2], &k[3], &k[4]);
q.push((hll){k[1], k[2]});
b[k[1]][k[2]] = 1, bfs
();
return 0;
}