LeetCode #505. The Maze II

505 迷宫 II 题目描述: 由空地和墙组成的迷宫中有一个球。球可以向上下左右四个方向滚动,但在遇到墙壁前不会停止滚动。当球停下时,可以选择下一个方向。 给定球的起始位置,目的地和迷宫,找出让球停在目的地的最短距离。距离的定义是球从起始位置(不包括)到目的地(包括)经过的空地个数。如果球无法停在目的地,返回 -1。 迷宫由一个0和1的二维数组表示。 1表示墙壁,0表示空地。你可以假定迷宫的边缘都是墙壁。起始位置和目的地的坐标通过行号和列号给出。 示例 1: 输入 1: 迷宫由以下二维数组表示 0 0 1 0 0 0 0 0 阅读详情

题目描述:

There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling updownleft 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, find the shortest distance for the ball to stop at the destination. The distance is defined by the number of empty spaces traveled by the ball from the start position (excluded) to the destination (included). If the ball cannot stop at the destination, return -1.

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.

Example 1:

Input 1: a maze represented by a 2D array

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

Input 2: start coordinate (rowStart, colStart) = (0, 4)
Input 3: destination coordinate (rowDest, colDest) = (4, 4)

Output: 12

Explanation: One shortest way is : left -> down -> left -> down -> right -> down -> right.
             The total distance is 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12.

Example 2:

Input 1: a maze represented by a 2D array

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

Input 2: start coordinate (rowStart, colStart) = (0, 4)
Input 3: destination coordinate (rowDest, colDest) = (3, 2)

Output: -1

Explanation: There is no way for the ball to stop at the destination.

Note:

  1. There is only one ball and one destination in the maze.
  2. Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
  3. The given maze does not contain border (like the red rectangle in the example pictures), but you could assume the border of the maze are all walls.
  4. The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.
class Solution {
public:
    int shortestDistance(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
        if(maze.size()==0||maze[0].size()==0) return true;
        int m=maze.size(), n=maze[0].size();
        vector<vector<int>> dist(m,vector<int>(n,-1));
        vector<pair<int,int>> dirs={{0,1},{0,-1},{1,0},{-1,0}};
        queue<pair<int,int>> q;
        q.push({start[0],start[1]});
        dist[start[0]][start[1]]=0;
        while(!q.empty())
        {
            pair<int,int> p=q.front();
            q.pop();
            for(pair dir:dirs)
            {
                int x=p.first, y=p.second;
                int step=0;
                while(x>=0&&x<m&&y>=0&&y<n&&maze[x][y]==0)
                { // 一直往一个方向遍历
                    x+=dir.first;
                    y+=dir.second;
                    step++;
                } // 越界之后再回退一步
                x-=dir.first, y-=dir.second;
                step--;
                // 如果该点未访问过,或者之前访问的路径比当前路径长,都需要更新该点到起点的距离
                if(dist[x][y]==-1||dist[x][y]>dist[p.first][p.second]+step)
                {
                    dist[x][y]=dist[p.first][p.second]+step;
                    q.push({x,y});
                }
            }
        }
        return dist[destination[0]][destination[1]];
    }
};

 

leetcode刷题笔记-Dijkstra‘s algorithm Dijkstra's algorithm具体: https://blog.csdn.net/cjc211322/article/details/24933909 505.The Maze II class Solution(object): def shortestDistance(self, maze, start, destination): des = ... 阅读详情

相关推荐

leetcode迷宫505-LeetCode:分享LeetCode解决方案和算法研究

leetcode迷宫505 常用数据结构 数组/字符串(Array/String) 链表(Linked-list) 栈(Stack) 队列(Queue) 双端队列(Deque) 树(Tree) 哈希表 高级数据结构 优先队列(Priority Queue) 图(Graph) 前缀树(Trie) 线段树(Segment Tree) 树状数组(Fenwick Tree/Binary Indexed Tree) 排序算法 基本的排序算法 冒泡排序 插入排序 选择排序 常考的排序算法 快速排序 归并排序 拓扑排序 其他排序算法 堆排序 桶排序 递归和回溯 递归算法 回溯算法 BFS/DFS DFS BFS 动态规划 线性规划 区间规划 约束规划 二分搜索算法 贪婪算法 常用数据结构 数组/字符串(Array/String) 优点 构建一个数组非常简单 能在O(1)的时间里根据下标(index)查询某个元素 缺点 构建时必须分配一段连续的空间 查询某个元素是否存在时需要遍历整个数组,耗费O(n)的时间 删除和添加某个元素时,同样需要耗费O(n)的时间 典型题目: 题目太多,下一版总结 链表(Li

LeetCode 505. 迷宫 II(BFS / Dijkstra 最短路径)

文章目录1. 题目2. 解题2.1 BFS2.2 Dijkstra 最短路径 1. 题目 由空地和墙组成的迷宫中有一个球。 球可以向上下左右四个方向滚动,但在遇到墙壁前不会停止滚动。 当球停下时,可以选择下一个方向。 给定球的起始位置,目的地和迷宫,找出让球停在目的地的最短距离。 距离的定义是球从起始位置(不包括)到目的地(包括)经过的空地个数。 如果球无法停在目的地,返回 -1。 迷宫由一个0和1的二维数组表示。 1表示墙壁,0表示空地。 你可以假定迷宫的边缘都是墙壁。 起始位置和目的地的坐标通过行号和列

Michael是个半路程序员 5776

leetcode迷宫505-leetCode:每天一刷leetCode

leetcode博物馆505 力码 更新时间:2020 年 6 月 3 日星期三 17:48:04 GMT+0800 (GMT+08:00) 我已经解决了2 / 1358 个问题,而213 个问题仍然被锁定。 (笔记: :blue_book: 意味着你需要从 Leetcode 购买一本书) # 标题 源代码 解释 困难 1 简单的 2 中等的 3 中等的 4 难的 5 中等的 6 中等的 7 简单的 8 中等的 9 简单的 10 难的 11 中等的 12 中等的 13 简单的 14 简单的 15 中等的 16 中等的 17 中等的 18 中等的 19 中等的 20 简单的 21 简单的 22 中等的 23 难的 24 中等的 25 难的 26 简单的 27 简单的 28 简单的 29 中等的 30 难的 31 中等的 32 难的 33 中等的 34 中等的 35 简单的 36 中等的 37 难的 38 简单的 39 中等的 40 中等的 41 难的 42 难的 43 中等的 44 难的 45 难的 46 中等的 47 中等的 48 中等的 49 中等的 50 中等的 51 难的 52 难的 53 简单的

Leetcode505. The Maze II

题目地址: https://leetcode.com/problems/the-maze-ii/ 给定一个二维0−10-10−1矩阵,000代表空地,111代表障碍物。在某个空地上有个小球,它可以沿着四个方向滑动,每次滑动时只有遇到边界或者障碍物才会停下来。再给定一个空地作为终点,问该小球是否可能滑到终点,若可能,则返回最短路径的长度(不包括起点,包括终点);若不可能,返回−1-1−1。 题目类似...

数学、算法爱好者的博客 927

leetcode505 迷宫II

在迷宫中有一个球,里面有空的空间和墙壁。球可以通过滚上,下,左或右移动,但它不会停止滚动直到撞到墙上。当球停止时,它可以选择下一个方向。 给定球的起始位置,目标和迷宫,找到最短距离的球在终点停留。距离是由球从起始位置(被排除)到目的地(包括)所走过的空空间的数量来定义的。如果球不能停在目的地,返回-1。 迷宫由二维数组表示。1表示墙和0表示空的空间。你可以假设迷宫的边界都是墙。开始和目标坐...

wf0934的博客 1467

leetcode The Maze I,IIIII 详细总结

[LeetCode] 490. The Maze 迷宫 一:题意 链接:https://leetcode.com/articles/the-maze/ 给定一个二维平面表示一个迷宫,迷宫中0代表空闲处,1代表墙,有一个人从start处可以上下左右四个方向移动(得朝着一个方向一直走到头,碰到墙才能换方向),碰到墙得换方向移动,问是否停在目的地destination。 经过目的地不算到达,球并不会停下来 边界已经用墙封住了,图中显示出了! 二:思路 dfs是不行的,超时了! 使用bfs来做 1:首先从sta

rainMan的博客 912

【网格问题】leetcode505.迷宫II

题目: 由空地和墙组成的迷宫中有一个球。球可以向上下左右四个方向滚动,但在遇到墙壁前不会停止滚动。当球停下时,可以选择下一个方向。 给定球的起始位置,目的地和迷宫,找出让球停在目的地的最短距离。距离的定义是球从起始位置(不包括)到目的地(包括)经过的空地个数。如果球无法停在目的地,返回 -1。 迷宫由一个0和1的二维数组表示。 1表示墙壁,0表示空地。你可以假定迷宫的边缘都是墙壁。起始位置和目的地的坐标通过行号和列号给出。 解答: class Solution: def shortestD

jqq125的博客 842

leetcode】高频题目整理_广度优先遍历篇( High Frequency Problems, Breadth-first Search )

截止至今LeetCode题目总量已经有1582题,估计将来每年平均增长300题左右,大部分人肯定是刷不完的,所以得有选择地刷LeetCode。 一种公认的刷题策略是按类别刷题,可是每个类别也有许多题,在有限的时间里到底该刷哪些题呢?个人根据LeetCode官方给出的每个题目的出现频率,整理并收录了每个类别里高频出现的题目,对于官方统计频率太低的题目,不予收录,最终得到了这个高频题目表格。 例如,对于下图中题号#275与#270的题目将被收录,并且#275出现频率大于#270;而对于题号#1011与#11

嵌入式与Linux那些事的博客 1464

leetcode505. 迷宫 II【bfs + seen记录每个点每个方向的最小花费!】

bfs + 最优状态记录 + pq

小楼一夜听春雨 136

leetcode迷宫505-LeetCode:我所有的leetcode解决方案都在这里

leetcode 迷宫 505 力码 # 标题 解决方案 运行 104 0 毫秒 108 0 毫秒 111 0 毫秒 490 4 毫秒 505 748 毫秒 752 15 毫秒 1197 349 毫秒 1586 65 毫秒

LeetCode周赛#103 Q4 Online Election (优先队列)

题目来源:https://leetcode.com/contest/weekly-contest-103/problems/online-election/ 问题描述 911. Online Election In an election, the i-th vote was cast for persons[i] at time times[i]. Now, we would like ...

da_kao_la的博客 464

Leetcode算法的第十五天

序言 比较经典的迷宫问题: 由空地(用 0 表示)和墙(用 1 表示)组成的迷宫 maze 中有一个球。球可以途经空地向 上、下、左、右 四个方向滚动,且在遇到墙壁前不会停止滚动。当球停下时,可以选择向下一个方向滚动。 给你一个大小为 m x n 的迷宫 maze ,以及球的初始位置 start 和目的地 destination ,其中 start = [startrow, startcol] 且 destination = [destinationrow, destinationcol] 。请你判断球能

Jiana_Feng的博客 512

leetcode505.迷宫2

思路 这个题中,由于要找最近的距离,所以点是可以重复跑的,每次选最小距离,根据最小距离进行递归 注意还是不撞墙不能停的 代码 void dfs(int** maze, int mazeSize, int mazeColSize, int startx, int starty, int endx, int endy,int k,int len[][100]){ if(startx<0||starty<0||startx>mazeSize||starty>mazeCol...

weixin_41672404的博客 361

[LeetCode] 505. The Maze II 迷宫之二

There is aballin a maze with empty spaces and walls. The ball can go through empty spaces by rollingup,down,leftorright, but it won't stop rolling until hitting a wall. When the ball stops, ...

weixin_30640769的博客 485

leetcode-505. 迷宫 II--【BFS】

文章目录C++ BFS 最短路径 C++ BFS 最短路径 求最短路径,并不是最短的步骤 感觉吧所有的可能性都搜索了一下,效率低下 class Solution { public: struct node{ int i,j,step; node(int ii,int jj,int stepp){ i = ii; j = jj; step = stepp; } };

超级氯化钾的博客 380

leetcode505 迷宫2

https://leetcode-cn.com/problems/the-maze-ii/ 区别于普通的BFS,这道题的特点是。一路走到头才停下来选择接下来该怎么走。普通的BFS是一步一步的走。所以这里要用到优先队列,保证每一步走的都是当前距离的最小的那个方向的选择(贪心)。 class Solution { int[][] dirs = new int[][]{{-1,0},{1,0},{0,-1},{0,1}}; public int shortestDistance(int[][]

xiaobailaji的博客 239

[Leetcode] 505. The Maze II 解题报告

题目: 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

魔豆(Magicbean)的博客 2892
上一篇: LeetCode #490. The Maze
下一篇: LeetCode #1099. Two Sum Less Than K
LawFile
博客等级 码龄10年 5粉丝 627原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值