[LeetCode] 490. 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] 490. The Maze 迷宫的更多相关文章
- LeetCode 490. The Maze
原题链接在这里:https://leetcode.com/problems/the-maze/ 题目: There is a ball in a maze with empty spaces and ...
- [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 ...
- [LeetCode] 505. The Maze II 迷宫 II
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- 【南京邮电】maze 迷宫解法
[南京邮电]maze 迷宫解法 题目来源:南京邮电大学网络攻防训练平台. 题目下载地址:https://pan.baidu.com/s/1i5gLzIt (密码rijss) 0x0 初步分析 题目中给 ...
- [LeetCode] The Maze 迷宫
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- [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 rolli ...
- 【LeetCode】490. The Maze 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 490. The Maze
原题链接:https://leetcode.com/articles/the-maze/ 这道题目是需要冲会员才能使用的,然而我个穷逼现在还是失业状态根本冲不起...以后如果把免费题目都刷完了的话,再 ...
- Maze迷宫问题(求最优解)
迷宫地形我们可以通过读文件的形式,通过已知入口逐个遍历坐标寻找通路. 文件如图: 每个坐标的位置用结构体来记录: struct Pos //位置坐标 { int _row; int _col; }; ...
随机推荐
- Linux高性能服务器编程,书中的 shell 命令
记录<Linux高性能服务器编程>书里面讲解到的若干 shell 命令 arp 命令查看ARP高速缓存: [root@VM_0_10_centos heliang]# arp -a ? ( ...
- Java Exception 异常处理
一.定义 异常(Exception) : 是指程序运行时出现的非正常情况,是特殊的运行错误对象,对应着Java语言特定的运行错误处理机制. 二.两大常见的异常类型 • RuntimeException ...
- 【Zabbix】zabora批量部署
zabora简化批量部署 目的:简化部署zabora,批量监控数据库的常用指标 1 数据库用户赋权 上传cre_arp_monitor.sh,并且部署用户. [root@oradb ~]# chown ...
- 2018-9-30-win10-UWP-剪贴板-Clipboard
原文:2018-9-30-win10-UWP-剪贴板-Clipboard title author date CreateTime categories win10 UWP 剪贴板 Clipboard ...
- winfrom 获取焦点控件
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Win ...
- 数据解析模块BeautifulSoup简单使用
一.准备环境: 1.准备测试页面test.html <html> <head> <title> The Dormouse's story </title> ...
- VMware与 Device/Credential Guard 不兼容,解决办法及心得
以下为心路历程,想要直接解决可以直接拉到最后看后续 百度要你取消Hyper-V功能,但我要用docker,以及一些相关的帖子都无效的情况下 https://blog.csdn.net/u0136677 ...
- Python进阶(二)
1.模块和包的概念 python的解决方案是把同名的模块放到不同的包中 1.1,导入模块 要使用一个模块,我们必须首先导入该模块.Python使用import语句导入一个模块.例如,导入系统自带的模块 ...
- Java自学-集合框架 泛型Generic
ArrayList上使用泛型 步骤 1 : 泛型 Generic 不指定泛型的容器,可以存放任何类型的元素 指定了泛型的容器,只能存放指定类型的元素以及其子类 package property; pu ...
- JZOJ.2117. 【2016-12-30普及组模拟】台风
题目大意: 天气预报频道每天从卫星上接受卫星云图.图片被看作是一个矩阵,每个位置上要么是”#”,要么”.”,”#”表示该位置没有云,”.”表示有云,地图上每个位置有多达8个相邻位置,分别是,左上.上. ...