题解 | #迷宫问题#

迷宫问题

http://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc

思路:广度优先遍历矩阵。代价相同的图中,广度优先遍历可以保证遍历到的目标点就是经过最短路径到达的点。为此,我们可以创建一个Point类,属性为横纵坐标和父节点。从(0,0)出发,将经过的坐标点都设为1,避免重复经过而进入死循环。把当前点的上下左右值为0的点都加入队列中,直到遇见出口为止。遇到出口时,pos的father路径就是最短路径的逆路径。此时只需要把逆路径反转一下即可。当然我们可以利用递归可以后序输出链表的特性,省去反转路径的操作。

Java版本:

 class Point{
        int px;
        int py;
        Point father;
        Point(int px,int py,Point father){
            this.px=px;
            this.py=py;
            this.father = father;
        }
        Point(){}
    }
public  class Main{
    public static void print(Point p){
        if(p != null){
            print(p.father);
            System.out.println("("+p.px+","+p.py+")");
        }
    }
    public  static void main(String args[]){
        Scanner sc = new Scanner(System.in);
            while(sc.hasNextInt()){
            int row = sc.nextInt();
            int col = sc.nextInt();
            int [][] grid = new int[row][col];
            for(int i=0;i<row;++i){
                for(int j=0;j<col;++j){
                    grid[i][j]=sc.nextInt();
                }
            }
            Queue<Point> que = new LinkedList<Point>();
            que.offer(new Point(0,0,null));
            grid[0][0]=1;
            Point pos = null;
            while(true){
                pos = que.poll();
                int px = pos.px;
                int py = pos.py;
                if(px==row-1&&py==col-1)break;
                else {
                    if(px+1<row&&grid[px+1][py]==0){
                        grid[px+1][py]=1;
                        que.offer(new Point(px+1,py,pos));
                    }
                    if(py-1>=0&&grid[px][py-1]==0){
                        grid[px][py-1]=1;
                    	que.offer(new Point(px,py-1,pos));
                    }
                    if(px-1>=0&&grid[px-1][py]==0){
                        grid[px-1][py]=1;
                    	que.offer(new Point(px-1,py,pos)); 
                    }
                    if(py+1<col&&grid[px][py+1]==0){
                        grid[px][py+1]=1;
                    	que.offer(new Point(px,py+1,pos));
                    }
                }
            }
            print(pos);
        }
    }
}

CPP版本

#include <bits/stdc++.h>
#include <cstddef>
using namespace std;
class Pos {
    public:
        int px;
        int py;
        Pos *father;
        Pos(int px, int py, Pos *father): px(px), py(py), father(father){
        }
};
void print_road(Pos *p, Pos *q) 
{
    if(q != NULL) {
        print_road(p, q -> father);
        cout << "(" + to_string(q -> px) + "," + to_string(q -> py) + ")" << endl;
    }
}
int main() 
{
    int x, y;
    cin >> x;
    cin >> y;
    vector<vector<int>> mat;
    for(int i = 0; i < x; ++i) {
        vector<int> vec;
        for(int j = 0; j < y; ++j) {
            int temp;
            cin >> temp;
            vec.push_back(temp);
        }
        mat.push_back(vec);
    }
    queue<Pos *> que;
    Pos *p = new Pos(0, 0, NULL);
    Pos *q;
    Pos *temp;
    que.push(p);
    mat[0][0] = 1;
    while(true) {
        q = que.front();
        que.pop();
        if(q -> px == x - 1 && q ->py == y - 1) {
           print_road(p, q);
           return 0;
        } else {
            int pxa1 = q -> px + 1;
            int pxm1 = q -> px - 1;
            int pya1 = q -> py + 1;
            int pym1 = q -> py - 1;
            if(pxa1 < x && mat[pxa1][q -> py] == 0) {
                mat[pxa1][q ->py] = 1;
                temp =  new Pos(pxa1, q ->py, NULL);
                temp -> father = q;
                que.push(temp);
            }
            if(pxm1 >= 0 && mat[pxm1][q ->py] == 0) {
                mat[pxm1][q ->py] = 1;
                temp =  new Pos(pxm1, q ->py, NULL);
                 temp ->father = q;
                que.push(temp);
            }
            if(pya1 < y && mat[q ->px][pya1] == 0) {
                mat[q ->px][pya1] = 1;
                 temp = new Pos(q ->px, pya1, NULL);
                 temp ->father = q;
                que.push(temp);
            }
            if(pym1 >= 0 && mat[q ->px][pym1] == 0) {
                mat[q ->px][pym1] = 1;
                temp = new Pos(q ->px, pym1, NULL);
                 temp ->father = q;
                que.push(temp);
            }
        }
    }
    return 0;
}
全部评论
思路很溜,递归用得恰到好处,赞!
3 回复 分享
发布于 2021-10-17 16:21
牛逼的bfs
1 回复 分享
发布于 2022-04-26 18:15
最短路径就是bfs遍历最快第一个到目标点的路径,而谁第一个遍历到终点循环遍历也结束保证最终结果就是最短路径。
点赞 回复 分享
发布于 2023-08-18 09:17 湖北
牛逼牛逼牛逼 大佬太厉害了
点赞 回复 分享
发布于 2023-01-12 23:54 北京
经典BFS算法
点赞 回复 分享
发布于 2022-11-28 19:50 广东
学习学习
点赞 回复 分享
发布于 2022-07-21 06:18
n,m = map(int,input().strip().split())#获取行和列 maze1 = [] for i in range(n): maze1.append(list(map(int,input().split())))#变成int将无引号 x, y = 0, 0 step=0 start=(0,0) end=(n-1,m-1) que=[] que.append(start) #print(maze1) #获取矩阵列表 while que: step += 1 for _ in range(len(que)): now=que.pop(0) r, c = now #解包 maze1[r][c]=3 if now == end: for t in que: print(t) for rk, ck in [(r-1,c), (r+1,c), (r, c-1), (r,c+1)]: if 0 <= rk < n and 0 <= ck < m and not maze1[rk][ck]: maze1[rk][ck]=3 que.append((rk,ck)) 大神帮忙看看我的bfs哪里有问题?
点赞 回复 分享
发布于 2022-07-19 12:59
妙啊
点赞 回复 分享
发布于 2022-06-12 10:32
解题思路简单清晰。牛逼,佩服
点赞 回复 分享
发布于 2022-06-02 19:32
也想到过这种方法无奈编码水平没达到
点赞 回复 分享
发布于 2022-05-02 10:43
牛逼的bfs
点赞 回复 分享
发布于 2022-04-26 18:15
牛逼的bfs
点赞 回复 分享
发布于 2022-04-26 18:15
牛逼的bfs
点赞 回复 分享
发布于 2022-04-26 18:15
牛逼的bfs
点赞 回复 分享
发布于 2022-04-26 18:15

相关推荐

点赞 评论 收藏
分享
评论
72
39
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务