2 solutions

  • 0
    @ 2022-1-21 20:45:02
    #include<bits/stdc++.h>
    using namespace std;
    const int dx[]={0,1,0,-1};
    const int dy[]={1,0,-1,0};
    struct node{
        int x,y,turn;
    }s,t,p;
    queue<node> q;
    int n,m,c[1005][1005];
    bool v[1005][1005];
    int main()
    {
     
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        scanf("%d",&c[i][j]);
        scanf("%d%d%d%d",&s.x,&s.y,&t.x,&t.y);
        q.push(s);
        memset(v,0,sizeof(v));
        q.front().turn=0;
        while(!q.empty())
        {
            for(int i=0;i<4;i++)
            {
                p.x=q.front().x+dx[i];
                p.y=q.front().y+dy[i];
                while(p.x>0&&p.x<=n&&p.y>0&&p.y<=m&&!c[p.x][p.y])
                {
                    if(!v[p.x][p.y])
                    {
                        if(p.x==t.x&&p.y==t.y)
                        {
                            printf("%d\n",q.front().turn);
                            return 0;
                        }
                        v[p.x][p.y]=1;
                        p.turn=q.front().turn+1;
                        q.push(p);
                    }
                    p.x+=dx[i];
                    p.y+=dy[i];
                }
            }
            q.pop();
        }
    }
    

    Information

    ID
    781
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    7
    Tags
    # Submissions
    53
    Accepted
    13
    Uploaded By