1 solutions

  • 0
    @ 2026-4-23 17:38:28
    #include<bits/stdc++.h>
    using namespace std;
    typedef pair<int,int> PII;
    #define x first
    #define y second
    const int N=1010;
    int dx[]={-1,1,0,0};
    int dy[]={0,0,-1,1};
    int dist[N][N];
    PII pre[N][N];
    int g[N][N];
    int n,m;
    void dfs(int x,int y)
    {
        if(x!=0||y!=0)
        {
            PII t=pre[x][y];
            dfs(t.x,t.y);
        }
        cout<<x<<" "<<y<<endl;
    }
    void bfs()
    {
        queue<PII> q;
        q.push({0,0});
        memset(dist,0x3f,sizeof dist);
        dist[0][0]=0;
        while(q.size())
        {
            PII t=q.front();
            q.pop();
            for(int i=0;i<4;i++)
            {
                int a=t.x+dx[i],b=t.y+dy[i];
                if(a<0||a>n||b<0||b>n||dist[a][b]!=0x3f3f3f3f||g[a][b]==1)
                {
                    continue;
                }
                dist[a][b]=dist[t.x][t.y]+1;
                pre[a][b]=t;
                q.push({a,b});
            }
        }
    }
    int main()
    {
        cin>>n;
        m=n;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                cin>>g[i][j];
            }
        }
        bfs();
        dfs(n-1,m-1);
        return 0;
    }
    
    • 1

    Information

    ID
    225
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    7
    Tags
    # Submissions
    21
    Accepted
    7
    Uploaded By