[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 ...
随机推荐
- Mybatis-no getter for property named 'col_name' in 'class com.xxx.onebean'
Mybatis中出现该异常 There is no getter for property named 'col_name' in 'class com.xxx.onebean 意思是onebean这 ...
- 线程池与Python中的GIL
线程池是一个操作系统的概念,它是对多线程的一种优化. 多线程的时候,创建和销毁线程伴随着操作系统的开销,如果频繁创建/销毁线程,则会使效率大大降低. 而线程池,是先创建出一批线程放入池子里,需要创建线 ...
- python全栈学习--day1
计算机基础 CPU:中央处理器 内存:4GB,8GB,临时处理事务的地方,供给CPU数据. 硬盘:相当于电脑的数据库,存储着大量的数据,文件,电影等. 操作系统:执行者,支配所有关系 window ...
- ASP.NET Core原理概述
ASP.NET Core 是一个控制台应用程序,在其 main 方法中创建一个Web服务器,以下是program.cs中的代码: using Microsoft.AspNetCore; using M ...
- Beta冲刺第三天
一.昨天的困难 没有困难. 二.今天进度 1.林洋洋:修改权限相关的资源表示,修复flex布局排版高度问题,修复文件更新问题,去除登录页面的默认账号密码,服务器部署. 2.黄腾达:修复日程首次执行时间 ...
- Hibernate之深入Hibernate的映射文件
这周周末 要把hibernate的映射文件搞定 .. 1.映射文件的主结构 主要结构 :根元素为<hibernate-mapping ></hibernate-mapping> ...
- vue内置指令详解——小白速会
指令 (Directives) 是带有 v- 前缀的特殊属性,职责是,当表达式的值改变时,将其产生的连带影响,响应式地作用于 DOM. 内置指令 1.v-bind:响应并更新DOM特性:例如:v-bi ...
- windows安装gcc编译器
由于vc6.0对c语言编译不是很好,有些语句是正确的,但是编译却不能通过 所以决定在windows中安装gcc编译器来使用! http://www.cnblogs.com/cryinstall/arc ...
- CentOS7下安装python-pip
一.检查是否已经安装 检查linux有没有安装python-pip包,直接执行:: yum install python-pip 二.安装 pip install 1.没有python-pip包就执行 ...
- 新概念英语(1-3)Sorry, sir
Does the man get his umbrella back? A:My coat and my umbrella please. B:Here is my ticket. A:Thank y ...