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:

  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.

这道题让我们遍历迷宫,但是与以往不同的是,这次迷宫是有一个滚动的小球,这样就不是每次只走一步了,而是朝某一个方向一直滚,直到遇到墙或者边缘才停下来,博主记得貌似之前在手机上玩过类似的游戏。那么其实还是要用 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

类似题目:

The Maze II

The Maze III

参考资料:

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

https://leetcode.com/problems/the-maze/discuss/97089/java-dfs-solution-could-anyone-tell-me-how-to-calculate-the-time-complexity

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] The Maze 迷宫的更多相关文章

  1. 【南京邮电】maze 迷宫解法

    [南京邮电]maze 迷宫解法 题目来源:南京邮电大学网络攻防训练平台. 题目下载地址:https://pan.baidu.com/s/1i5gLzIt (密码rijss) 0x0 初步分析 题目中给 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. 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 ...

  6. Maze迷宫问题(求最优解)

    迷宫地形我们可以通过读文件的形式,通过已知入口逐个遍历坐标寻找通路. 文件如图: 每个坐标的位置用结构体来记录: struct Pos //位置坐标 { int _row; int _col; }; ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. sql的优化30条

    1. 对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2. 应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使 ...

  2. Android,资料分享(2015 版)

    Java 学习 我要再次强调,一定要有Java 基础(虽然现在使用其他语言也可以开发Android,但毕竟是很小众),也不要认为学习Java 两三周就可以不用管了,这会在以后的深入学习中暴露出问题,所 ...

  3. 开始补习JavaScript的第一天

    JavaScript介绍: ①.JavaScript是一种解释性的,基于对象的脚本语言. ②.JavaScript是一种轻量级的编程语言,可以嵌入到html页面中,由浏览器来解释执行. ③.JavaS ...

  4. Linux下ping,telnet,ssh命令的比较

    ping工作在OSI模型的第三层,网络层. 主要用于测试到达目的主机的网络是否连接,不能检测某个端口是否开放. ping使用ICMP协议,不使用某个特定端口. 也可以 ping 域名 ,这样可以直接看 ...

  5. C语言最后一次博客作业

    1.当初你是如何做出选择计算机专业的决定的? 一开始选专业的时候,说实话我很纠结也很迷茫,对我来说,中学时代,似乎就只要考好试,做好题就可以了,对于未来想要做啥并没有那么多的规划和想法,偶尔跟基友畅聊 ...

  6. Alpha冲刺置顶随笔

    项目名称:城市安全风险管控系统 小组成员: 张梨贤.林静.周静平.黄腾飞 Alpha冲刺随笔 Alpha冲刺Day1:http://www.cnblogs.com/linlkg/p/7896980.h ...

  7. python中functools.singledispatch的使用

    from functools import singledispatch @singledispatch def show(obj): print (obj, type(obj), "obj ...

  8. 20145237 《Java程序设计》第2周学习总结

    教材学习内容总结 本周我学习了java的基础语法.分为类型.变量与运算符,流程控制. 一.类型:1.Java可以区分为基本类型和类类型.类类型也称作参考类型.2.Java中基本类型主要是整数.字节.浮 ...

  9. Python randrange() 函数

    Python randrange() 函数  Python 数字 描述 randrange() 方法返回指定递增基数集合中的一个随机数,基数缺省值为1. 语法 以下是 randrange() 方法的语 ...

  10. 201421123042 《Java程序设计》第3周学习总结

    #Week03-面向对象入门 1. 本周学习总结 1.1写出你认为本周学习中比较重要的知识点关键词,如类.对象.封装等 本周学习关键词:类,对象,封装,关键词:final,this,statis. 1 ...