[LeetCode] 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.
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: true
- Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right.
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: false
- Explanation: There is no way for the ball to stop at the destination.
Note:
- There is only one ball and one destination in the maze.
- Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
- 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.
- The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.
这道题让我们遍历迷宫,但是与以往不同的是,这次迷宫是有一个滚动的小球,这样就不是每次只走一步了,而是朝某一个方向一直滚,直到遇到墙或者边缘才停下来,博主记得貌似之前在手机上玩过类似的游戏。那么其实还是要用 DFS 或者 BFS 来解,只不过需要做一些修改。先来看 DFS 的解法,用 DFS 的同时最好能用上优化,即记录中间的结果,这样可以避免重复运算,提高效率。这里用二维记忆数组 memo 来保存中间结果,然后用 maze 数组本身通过将0改为 -1 来记录某个点是否被访问过,这道题的难点是在于处理一直滚的情况,其实也不难,有了方向,只要一直在那个方向上往前走,每次判读是否越界了或者是否遇到墙了即可,然后对于新位置继续调用递归函数,参见代码如下:
解法一:
- class Solution {
- public:
- vector<vector<int>> dirs{{,-},{-,},{,},{,}};
- bool hasPath(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {int m = maze.size(), n = maze[].size();
- return helper(maze, start[], start[], destination[], destination[]);
- }
- bool helper(vector<vector<int>>& maze, int i, int j, int di, int dj) {
- if (i == di && j == dj) return true;
- bool res = false;
- int m = maze.size(), n = maze[].size();
- maze[i][j] = -;
- for (auto dir : dirs) {
- int x = i, y = j;
- while (x >= && x < m && y >= && y < n && maze[x][y] != ) {
- x += dir[]; y += dir[];
- }
- x -= dir[]; y -= dir[];
- if (maze[x][y] != -) {
- res |= helper(maze, x, y, di, dj);
- }
- }
- return res;
- }
- };
同样的道理,对于 BFS 的实现需要用到队列 queue,在对于一直滚的处理跟上面相同,参见代码如下:
解法二:
- class Solution {
- public:
- bool hasPath(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
- if (maze.empty() || maze[].empty()) return true;
- int m = maze.size(), n = maze[].size();
- vector<vector<bool>> visited(m, vector<bool>(n, false));
- vector<vector<int>> dirs{{,-},{-,},{,},{,}};
- queue<pair<int, int>> q;
- q.push({start[], start[]});
- visited[start[]][start[]] = true;
- while (!q.empty()) {
- auto t = q.front(); q.pop();
- if (t.first == destination[] && t.second == destination[]) return true;
- for (auto dir : dirs) {
- int x = t.first, y = t.second;
- while (x >= && x < m && y >= && y < n && maze[x][y] == ) {
- x += dir[]; y += dir[];
- }
- x -= dir[]; y -= dir[];
- if (!visited[x][y]) {
- visited[x][y] = true;
- q.push({x, y});
- }
- }
- }
- return false;
- }
- };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/490
类似题目:
参考资料:
https://leetcode.com/problems/the-maze/
https://leetcode.com/problems/the-maze/discuss/97081/java-bfs-solution
https://leetcode.com/problems/the-maze/discuss/97112/Short-Java-DFS-13ms-Solution
[LeetCode] The Maze 迷宫的更多相关文章
- 【南京邮电】maze 迷宫解法
[南京邮电]maze 迷宫解法 题目来源:南京邮电大学网络攻防训练平台. 题目下载地址:https://pan.baidu.com/s/1i5gLzIt (密码rijss) 0x0 初步分析 题目中给 ...
- [LeetCode] The Maze III 迷宫之三
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- [LeetCode] The Maze II 迷宫之二
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- [LeetCode] 490. The Maze 迷宫
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- Leetcode: The Maze II
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- Maze迷宫问题(求最优解)
迷宫地形我们可以通过读文件的形式,通过已知入口逐个遍历坐标寻找通路. 文件如图: 每个坐标的位置用结构体来记录: struct Pos //位置坐标 { int _row; int _col; }; ...
- Leetcode: The Maze III(Unsolved Lock Problem)
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- Leetcode: The Maze(Unsolved locked problem)
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- [LeetCode] 499. The Maze III 迷宫 III
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
随机推荐
- pip遇见的format问题
这是pip升级以后的问题. DEPRECATION: The default format will switch to columns in the future. You can use –for ...
- http的CA证书安装(也就是https)
近几年随着安全意识的提高,https流行起来,很多小伙伴不太了解https是什么,其实http和https并没有区别,简单的来说,https就是将http通信进行了加密和解密的一个过程.加上谷歌浏览器 ...
- wpf研究之道-datagrid控件(1)
"想要说些什么 又不知从何说起",每当想要写一些关于wpf的文章,总是沉思良久,怕自己写不好.今天我想要说的是wpf中datagrid控件.我们先来看看它在整个类的层次结构: ...
- 使用 win10 的正确姿势 (第二版)
文章为本人原创,转载请注明出处,谢谢. 17年9月初,写了第一篇<使用 win10 的正确姿势>,而现在半年多过去,文章更新了一些,主要是桌面的变化. 一. 重新定义桌面 我的桌面: 将桌 ...
- 跨平台原生AR/VR应用研发引擎-NVisionXR开放内测
NVisionXR引擎正式开放内测.现在,对原生AR/VR应用开发有兴趣的企业和开发者均可通过NVisionXR官网(www.nvisionxr.com)申请试用. NVisionXR引擎介绍视频 ...
- 【Spring系列】自己手写一个 SpringMVC 框架
参考文章 一.了解SpringMVC运行流程及九大组件 1.SpringMVC的运行流程 1)用户发送请求至前端控制器DispatcherServlet 2)DispatcherServlet收到请求 ...
- JDK1.8源码(六)——java.util.LinkedList 类
上一篇博客我们介绍了List集合的一种典型实现 ArrayList,我们知道 ArrayList 是由数组构成的,本篇博客我们介绍 List 集合的另一种典型实现 LinkedList,这是一个有链表 ...
- Leetcode 17.——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 第十四,十五周PTA作业
1.第十四周part1 7-3 #include<stdio.h> int main() { int n; scanf("%d",&n); int a[n]; ...
- 第1次作业:小菜鸟的平凡IT梦
#1.结缘计算机的始末 ##1.1与计算机相识的几年 作为一个95后,出生在一个互联网开始兴盛的时代.我记得小学的时候,开始知道电脑这个东西,学校有了机房,开始有了所谓的电脑课.那时候计算机对于我来说 ...