1 条题解

  • 1
    @ 2024-9-12 14:20:19
    #include<bits/stdc++.h>
    #define ULL unsigned long long
    #define LL long long
    #define PII pair<int,int>
    using namespace std;
    const int N = 3 * 1e6 + 10,M = 2 * 1e3 + 10,inf = 0x3f3f3f3f;
    
    int g[100][100];
    bool st[100][100];
    int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
    struct node
    {
        int x,y;
        vector<PII> path; //记录路径
    };
    void bfs()
    {
        queue<node> q;
        node start;
        start.x = 0,start.y = 0;
        start.path.push_back({0,0});
        q.push(start);
        st[0][0] = 1;
        while (q.size())
        {
            node t = q.front();
            q.pop();
            if(t.x==4&&t.y==4)
            {
                for(int i=0;i<t.path.size();i++) 
                printf("(%d, %d)\n",t.path[i].first,t.path[i].second);
                return;
            }
            for(int i=0;i<4;i++)
            {
                node n;
                n.x = t.x + dir[i][0];
                n.y = t.y + dir[i][1];
                n.path = t.path; //复制上一步的路径
                n.path.push_back({n.x,n.y}); //再加上这一步的路径
                if(n.x < 0 || n.y < 0 || n.x >= 5 || n.y >= 5) continue;
                if(st[n.x][n.y]) continue;
                if(g[n.x][n.y]==1) continue;
                st[n.x][n.y] = 1;
                q.push(n);
            }
        }
        cout<<"no way"<<endl;
    }
    int main()
    {
        ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
        for(int i=0;i<5;i++)
            for(int j=0;j<5;j++)
                cin>>g[i][j];
        bfs();
        return 0;
    }
    ```
    
    
    ```
    • 1

    信息

    ID
    1200
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    (无)
    递交数
    2
    已通过
    2
    上传者