>Link
ybtoj走迷宫
>解题思路
一道非常典型的bfs
>代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define N 2010
using namespace std;
const int xx[4] = {
-1, 0, 0, 1}, yy[4] = {
0, -1, 1, 0};
int n, a[N][N], sx, sy, tx, ty, qx[N], qy[N], head, tail, c[N][N];
bool v[N][N];
char cc;
int main()
{
scanf ("%d", &n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
cc = getchar();
while (cc != '0' && cc != '1') cc = getchar();
a[i][j] = cc - '0';
}
scanf ("%d%d%d%d", &sx, &sy, &tx, &ty);
memset (c, 0x7f, sizeof (c));
c[sx][sy] = 0, tail++, qx[1] = sx, qy[1] = sy, v[sx][sy] = 1;
while (head < tail)
{
head++;
int x = qx[head], y = qy[head];
for (int i = 0; i < 4; i++)
{
int tox = x + xx[i], toy = y + yy[i];
if (tox < 1 || toy < 1 || tox > n || toy > n) continue;
if (a[tox][toy] == 1 || v[tox][toy]) continue;
v[tox][toy] = 1;
tail++;
qx[tail] = tox, qy[tail] = toy;
c[tox][toy] = c[x][y] + 1;
if (tox == tx && toy == ty)
{
printf ("%d", c[tx][ty]);
return 0;
}
}
}
return 0;
}