20RE???

P1746 离开中山路

shoot_down @ 2022-10-12 16:20:39

求助

#include <iostream>
#include <cstring>
using namespace std;
int m,n;
int startx,starty,endx,endy;
char a[10001][10001];
int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
struct point
{
    int x;
    int y;
    int pre;
} q[1001];
void bfs()
{
    q[1].x=startx-1;
    q[1].y=starty-1;
    q[1].pre=-1;
    a[startx-1][startx-1]='1';
    int head=1,tail=1;
    while(head<=tail)
    {
        for(int i=0;i<4;i++)
        {
            int px=q[head].x+dx[i];
            int py=q[head].y+dy[i];
            if(px<0||px>n-1||py<0||py>n-1) continue; 
            if(a[px][py]=='1') continue;
            a[px][py]='1';
            tail++;
            q[tail].x=px;
            q[tail].y=py;
            q[tail].pre=head;
            if(px==endx-1&&py==endy-1)
            {
                int num=-1;
                while(tail!=-1)
                {
                    num++;
                    tail=q[tail].pre;
                }
                cout<<num;
                return;
            }           
        }
        head++;
    }
}
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            cin>>a[i][j];
    cin>>startx>>starty>>endx>>endy;
    bfs();
    return 0;
}

by 晴空一鹤 @ 2022-10-12 17:00:43

q数组开小了把,起码要上2002把


by 晴空一鹤 @ 2022-10-12 17:16:42

@20200900193lrq


|