前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >​LeetCode刷题实战490:迷宫

​LeetCode刷题实战490:迷宫

作者头像
程序员小猿
发布2022-03-03 15:45:02
3770
发布2022-03-03 15:45:02
举报
文章被收录于专栏:程序IT圈

算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !

今天和大家聊的问题叫做 迷宫,我们先来看题面:

https://leetcode-cn.com/problems/the-maze/

There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.

Given the ball's start position, the destination and the maze, determine whether the ball could stop at the destination.

The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.

由空地和墙组成的迷宫中有一个球。球可以向上下左右四个方向滚动,但在遇到墙壁前不会停止滚动。当球停下时,可以选择下一个方向。

给定球的起始位置,目的地和迷宫,判断球能否在目的地停下。

迷宫由一个0和1的二维数组表示。1表示墙壁,0表示空地。你可以假定迷宫的边缘都是墙壁。起始位置和目的地的坐标通过行号和列号给出。

示例

解题

https://blog.csdn.net/weixin_44171872/article/details/108931956

主要思路:

(1)广度优先搜索;

(2)将迷宫maze的元素置为2,来标识访问过的变换方向的位置;

(3)使用队列,存储各个可能变换方向的位置,并判断每个变换方向的位置是否是终点位置,若是,则返回true,否则,在没有访问过的情形下,压入到队列中;

代码语言:javascript
复制
class Solution {
public:
    bool hasPath(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
        if(start==destination){
            return true;
        }
        //变换方向的位置使用2进行标识
        maze[start[0]][start[1]]=2;
        //使用队列实现广度优先搜索
        queue<vector<int>> q;
        //压入初始位置
        q.push(start);
        while(!q.empty()){//终止条件是没有可以判断的位置
          //取出当前需要判断的位置
            vector<int> tmp=q.front();
            q.pop();
            //起始位置
            //向上方向
            int row=tmp[0];
            int col=tmp[1];
            while(row>=0&&maze[row][col]!=1){//向上
                --row;
            }
            //判断终止位置是否是目的位置
            if(row+1==destination[0]&&col==destination[1]){
                return true;
            }
            //若当前终止位置之前没有访问过,则压入队列,并进行标识
            if(maze[row+1][col]!=2){
                q.push({row+1,col});
                maze[row+1][col]=2;
            }

      //向下方向
            row=tmp[0];
            while(row<maze.size()&&maze[row][col]!=1){
                ++row;
            }
            if(row-1==destination[0]&&col==destination[1]){
                return true;
            }
            if(maze[row-1][col]!=2){
                q.push({row-1,col});
                maze[row-1][col]=2;
            }
      
      //向左方向
            row=tmp[0];
            while(col>=0&&maze[row][col]!=1){
                --col;
            }
            if(row==destination[0]&&col+1==destination[1]){
                return true;
            }
            if(maze[row][col+1]!=2){
                q.push({row,col+1});
                maze[row][col+1]=2;
            }
            //向右方向
            col=tmp[1];
            while(col<maze[0].size()&&maze[row][col]!=1){
                ++col;
            }
            if(row==destination[0]&&col-1==destination[1]){
                return true;
            }
            if(maze[row][col-1]!=2){
                q.push({row,col-1});
                maze[row][col-1]=2;
            }
        }
        return false;//跳出循环,说明没有找到合适的位置
    }
};

好了,今天的文章就到这里,如果觉得有所收获,请顺手点个在看或者转发吧,你们的支持是我最大的动力 。

上期推文:

LeetCode1-480题汇总,希望对你有点帮助!

LeetCode刷题实战481:神奇字符串

LeetCode刷题实战482:密钥格式化

LeetCode刷题实战483:最小好进制

LeetCode刷题实战484:寻找排列

LeetCode刷题实战485:最大连续 1 的个数

LeetCode刷题实战486:预测赢家

LeetCode刷题实战487:最大连续1的个数 II

LeetCode刷题实战488:祖玛游戏

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-01-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序员小猿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 示例
  • 解题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档