LeetCode 490. The Maze
原题链接在这里:https://leetcode.com/problems/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.
题解:
Starting from start index, perform BFS on 4 directions. For each direction, only stops when hitting the boundary or wall.
Then check if hitting position is visited before. If not, mark it as visited, add it to queue.
Time Complexity: O(m*n). m = maze.length. n = maze[0].length.
Space: O(m*n).
AC Java:
class Solution {
int [][] dirs = new int[][]{{-1, 0}, {1, 0}, {0,-1}, {0,1}}; public boolean hasPath(int[][] maze, int[] start, int[] destination) {
if(maze == null || maze.length == 0 || maze[0].length == 0){
return false;
} int m = maze.length;
int n = maze[0].length;
if(start[0]<0 || start[0]>=m || start[1]<0 || start[1]>=n ||
destination[0]<0 || destination[0]>=m || destination[1]<0 || destination[1]>=n){
return false;
} LinkedList<int []> que = new LinkedList<>();
boolean [][] visited = new boolean[m][n];
que.add(start);
visited[start[0]][start[1]] = true;
while(!que.isEmpty()){
int [] cur = que.poll();
if(cur[0] == destination[0] && cur[1] == destination[1]){
return true;
} for(int [] dir : dirs){
int r = cur[0];
int c = cur[1];
while(r+dir[0]>=0 && r+dir[0]<m && c+dir[1]>=0 && c+dir[1]<n && maze[r+dir[0]][c+dir[1]]==0){
r += dir[0];
c += dir[1];
} if(!visited[r][c]){
visited[r][c] = true;
que.add(new int[]{r, c});
}
}
} return false;
}
}
Could also use DFS. DFS state needs maze, current starting point, destinaiton point, visited matrix and current moving direction.
If starting point == destinaiton point, return true.
Otherwise, for each of 4 directions, calculae the stop point.
If the stop point hasn't been visited before, mark it as visited and continue DFS from that point. If any of 4 dirs, dfs result from that point return true, then return true.
Time Complexity: O(m*n).
Space: O(m*n).
AC Java:
class Solution {
int [][] dirs = new int[][]{{-1, 0}, {1,0}, {0,-1}, {0,1}};
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
if(maze == null || maze.length == 0 || maze[0].length == 0){
return false;
} int m = maze.length;
int n = maze[0].length;
boolean [][] visited = new boolean[m][n];
visited[start[0]][start[1]] = true; for(int [] dir : dirs){
if(dfs(maze, start, destination, dir, visited)){
return true;
}
} return false;
} private boolean dfs(int[][] maze, int[] start, int[] dest, int [] dir, boolean [][] visited){
int m = maze.length;
int n = maze[0].length; int r = start[0];
int c = start[1]; if(r == dest[0] && c ==dest[1]){
return true;
} while(r+dir[0]>=0 && r+dir[0]<m && c+dir[1]>=0 && c+dir[1]<n && maze[r+dir[0]][c+dir[1]]==0){
r += dir[0];
c += dir[1];
} if(visited[r][c]){
return false;
} visited[r][c] = true;
for(int [] nextDir : dirs){
if(dfs(maze, new int[]{r,c}, dest, nextDir, visited)){
return true;
}
} return false;
}
}
LeetCode 490. The Maze的更多相关文章
- [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] 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 ...
- 【LeetCode】490. The Maze 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- [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 499. The Maze III
原题链接在这里:https://leetcode.com/problems/the-maze-iii/ 题目: There is a ball in a maze with empty spaces ...
- LeetCode 505. The Maze II
原题链接在这里:https://leetcode.com/problems/the-maze-ii/ 题目: There is a ball in a maze with empty spaces a ...
- 490. The Maze
原题链接:https://leetcode.com/articles/the-maze/ 这道题目是需要冲会员才能使用的,然而我个穷逼现在还是失业状态根本冲不起...以后如果把免费题目都刷完了的话,再 ...
- [LeetCode] 490. The Maze_Medium tag: BFS/DFS
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
随机推荐
- spring.profiles.active=@profiles.active@的含义
spring.profiles.active=@profiles.active@ ,其实是配合 maven profile进行选择不同配置文件进行启动. 当执行 mvn clean package - ...
- C语言是什么
大家对于Java可能并不陌生,那你对c语言了解多少呢,今天小编带大家来了解c语言是什么. c语言是一门面向过程.抽象化的通用程序设计语言,广泛应用于底层开发.C语言具有高效.灵活.功能丰富.表达力强和 ...
- markdown入门基础------程序员必备技能
本人博客:https://blog.csdn.net/qq_39576571/article/details/97256521 ·首先:什么是markdown markdown是一种轻量级的标记语言, ...
- Marshmallow详解
目录 Marshmallow详解 1. Scheme 2. Serializing(序列化) 3. 过滤输出 4. Deserializing(反序列化) 5. 处理多个对象的集合 6. Valida ...
- 【leetcode-22】括号生成
给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())& ...
- kafka broker Leader -1引起spark Streaming不能消费的故障解决方法
一.问题描述:Kafka生产集群中有一台机器cdh-003由于物理故障原因挂掉了,并且系统起不来了,使得线上的spark Streaming实时任务不能正常消费,重启实时任务都不行.查看kafka t ...
- Java静态变量初始化的坑
class SingleTon { private static SingleTon singleTon = new SingleTon(); public static int count1; pu ...
- Reactive MySQL Client
Reactive MySQL Client是MySQL的客户端,具有直观的API,侧重于可伸缩性和低开销. 特征 事件驱动 轻量级 内置连接池 准备好的查询缓存 游标支持 行流 RxJava 1和Rx ...
- pacman 命令详解
Pacman 是一个命令行工具,这意味着当你执行下面的命令时,必须在终端或控制台中进行. 1.更新系统 在 Arch Linux 中,使用一条命令即可对整个系统进行更新:pacman -Syu 如果你 ...
- vue--设置cookie
Vue-CLI项目-vue-cookie与vue-cookies处理cookie vue-cookie 一.模块的安装 npm install vue-cookie --save #--save可以不 ...