O1478 【基础】迷宫出口

#include <queue>
#include <iostream>
using namespace std;
struct stu{int x, y;};
int n, a[105][105], ax, ay, bx, by;
string ans;
int dx[] = {0, 0, 0, 1, -1};
int dy[] = {0, 1, -1, 0, 0};

bool check(int x, int y)
{
    if (x < 1 || x > n)return 0;
    if (y < 1 || y > n)return 0;
    if (a[x][y] == 1)return 0;
    return 1;
}

void bfs()
{
    queue <stu> q;
    q.push(stu{ax, ay});
    while (!q.empty())
    {
        stu f = q.front();
        q.pop();
        for (int i = 1; i <= 4; i++)
        {
            int nx = f.x + dx[i];
            int ny = f.y + dy[i];
            if (check(nx, ny))
            {
                a[nx][ny] = 1;
                q.push(stu{nx, ny});
            }
        }
    }
}

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            cin >> a[i][j];
        }
    }
    cin >> ax >> ay >> bx >> by;
    if (a[ax][ay] == 1 || a[bx][by] == 1)
    {
        cout << "NO";
        return 0;
    }
    a[ax][ay] = 1;
    bfs();
    if (a[bx][by] == 1)ans = "YES";
    else ans = "NO";
    cout << ans;
    return 0;
}