• POJ-3984-迷宫问题


    链接:https://vjudge.net/problem/POJ-3984

    题意:

    给5x5的迷宫,1不可走,0可走。

    从左上角走到右下角。求最短的路径

    思路:

    BFS+路径记录

    练习bfs的路径记录

    代码:

    #include <iostream>
    #include <memory.h>
    #include <queue>
    #include <stack>
    using namespace std;
    typedef long long LL;
    struct Node
    {
        int _x,_y;
        Node(int x,int y)
        {
            _x = x;
            _y = y;
        }
        Node(){}
    }last[5][5];
    const int Next[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
    int Map[5][5];
    int vis[5][5];
    
    int main()
    {
        for (int i = 0;i<5;i++)
            for (int j = 0;j<5;j++)
                scanf("%d",&Map[i][j]);
        queue<Node> que;
        que.push(Node(0,0));
        vis[0][0] = 1;
        while (!que.empty())
        {
            int x = que.front()._x;
            int y = que.front()._y;
            if (x == 4&&y == 4)
                break;
            for (int i = 0;i<4;i++)
            {
                int nx = x+Next[i][0];
                int ny = y+Next[i][1];
                if (nx < 0||nx > 4||ny < 0||ny > 4||vis[nx][ny] == 1||Map[nx][ny] == 1)
                    continue;
                last[nx][ny]._x = x;
                last[nx][ny]._y = y;
                que.push(Node(nx,ny));
                vis[nx][ny] = 1;
            }
            que.pop();
        }
        stack<Node> Path;
        int px = 4,py = 4;
        while (px != 0||py != 0)
        {
            Path.push(Node(px,py));
            int tx = last[px][py]._x;
            int ty = last[px][py]._y;
            px = tx;
            py = ty;
        }
        Path.push(Node(0,0));
        while (Path.size())
        {
            printf("(%d, %d)
    ",Path.top()._x,Path.top()._y);
            Path.pop();
        }
    
        return 0;
    }
    

      

  • 相关阅读:
    idea
    C#
    mysql
    .net5
    .net5
    .net5
    .net5
    .net5
    .net5
    .net5
  • 原文地址:https://www.cnblogs.com/YDDDD/p/10269370.html
Copyright © 2020-2023  润新知