10分求助

P1746 离开中山路

sen_lin114514 @ 2023-10-26 16:59:53

#include <iostream>

char maze[1005][1005];
int x1,y1,x2,y2;

bool flag[1005][1005];

int q[4][1005000];

int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};

int n;

void bfs(int x,int y)
{
    int h = 1 ,r = 2;
    q[1][h] = x;
    q[2][h] = y;
    q[3][h] = 0;
    flag[x][y] = true;
    while (h < r)
    {
        for (int i = 0 ; i < 4 ; i ++)
        {
            int xx = q[1][h] + dx[i];
            int yy = q[2][h] + dy[i];
            if (xx > 0 && yy > 0 && xx <= n && yy <= n && maze[xx][yy] == '0' && flag[xx][yy] == false)
            {
                flag[xx][yy] == true;
                q[1][r] = xx;
                q[2][r] = yy;
                q[3][r] = q[3][h] + 1;
                if (xx == x2 && yy == y2)
                {
                    std :: cout << q[3][r];
                    return;
                }
                r ++;
            }
        }
        h ++;
    }
}

int main()
{
    std :: cin >> n;
    for (int i = 1 ; i <= n ; i ++)
    {
        for (int j = 1 ; j <= n ; j ++)
        {
            std :: cin >> maze[i][j];
        }
    }
    std :: cin >> x1 >> y1 >> x2 >> y2;
    bfs(x1,y1);
}

样例过了,但只有10分


|