• poj迷宫问题(bfs路径输出)


    迷宫问题
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 11638   Accepted: 6967

    Description

    定义一个二维数组:

    int maze[5][5] = {
    0, 1, 0, 0, 0,
    0, 1, 0, 1, 0,
    0, 0, 0, 0, 0,
    0, 1, 1, 1, 0,
    0, 0, 0, 1, 0,
    };

    它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

    Input

    一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

    Output

    左上角到右下角的最短路径,格式如样例所示。

    Sample Input

    0 1 0 0 0
    0 1 0 1 0
    0 0 0 0 0
    0 1 1 1 0
    0 0 0 1 0

    Sample Output

    (0, 0)
    (1, 0)
    (2, 0)
    (2, 1)
    (2, 2)
    (2, 3)
    (2, 4)
    (3, 4)
    (4, 4)

    Source

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    int map[10][10];
    int visit[10][10];
    int dir[4][2]= {0,1,0,-1,1,0,-1,0};
    struct nodes
    {
        int x,y;
    };
    nodes node1,node2,pre[15][15];
    void bfs()
    {
        queue<nodes> q;
        while(!q.empty())
            q.pop();
        node1.x=0;
        node1.y=0;
        visit[0][0]=1;
        pre[0][0].x=0;
        pre[0][0].y=0;
        q.push(node1);
        while(!q.empty())
        {
            node1=q.front();
            q.pop();
            if(node1.x==4&&node1.y==4)
            {
                break;
            }
            for(int i=0; i<4; i++)
            {
                node2.x=node1.x+dir[i][0];
                node2.y=node1.y+dir[i][1];
                if(node2.x>=0&&node2.x<5&&node2.y>=0&&node2.y<5&&!map[node2.x][node2.y]&&visit[node2.x][node2.y]==0)
                {
                    visit[node2.x][node2.y]=visit[node1.x][node1.y]+1;
                    pre[node2.x][node2.y].x=node1.x;
                    pre[node2.x][node2.y].y=node1.y;
                    q.push(node2);
    
                }
            }
        }
    }
    /*
    这样写如果第一个方向找的路不对就会出错
    void print(int x,int y)
    {
        if(x==0&&y==0)
        {
            printf("(0, 0)
    ");
            return ;
        }
        for(int i=0; i<4; i++)
        {
            int xx=x+dir[i][0];
            int yy=y+dir[i][1];
            if(xx>=0&&xx<=4&&yy>=0&&yy<=4&&visit[xx][yy]==visit[x][y]-1)
            {
                print(xx,yy);
                printf("(%d, %d)
    ",x,y);
            }
        }
    }
    */
    void print(int x,int y)
    {
        if(x==0&&y==0)
        {
            printf("(0, 0)
    ");
            return ;
        }
        print(pre[x][y].x,pre[x][y].y);
        printf("(%d, %d)
    ",x,y);
    }
    int main()
    {
        for(int i=0; i<5; i++)
    
        {
            for(int j=0; j<5; j++)
            {
                scanf("%d",&map[i][j]);
            }
        }
        memset(visit,0,sizeof(visit));
        bfs();
        print(4,4);
    }
  • 相关阅读:
    【动态规划、贪心】剪绳子
    【Leetcode 数组】 有序数组中出现次数超过25%的元素(1287)
    【Leetcode 数组】 杨辉三角(118)
    【Leetcode 数组】 螺旋矩阵 II(59)
    【Leetcode 数组】 螺旋矩阵(54)
    【BFPRT】数组中出现次数超过一半的数字
    【Leetcode 大小堆、二分、BFPRT、二叉排序树、AVL】数据流的中位数(295)
    【Leetcode 二分】 滑动窗口中位数(480)
    常见ie9兼容问题
    js常用正则表达式
  • 原文地址:https://www.cnblogs.com/dshn/p/4750336.html
Copyright © 2020-2023  润新知