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 ...
随机推荐
- git new
Quick setup — if you’ve done this kind of thing before Set up in Desktop or HTTPSSSH Get started by ...
- DDR3(2):初始化
调取 DDR3 IP核后,是不能直接进行读写测试的,必须先进行初始化操作,对 IP 核进行校验.本篇采用 Modelsim 软件配合 DDR3 IP核生成的仿真模型,搭建出 IP核的初始化过程. 一. ...
- 总结:WPF中模板需要绑定父级别的ViewModel该如何处理
原文:总结:WPF中模板需要绑定父级别的ViewModel该如何处理 <ListBox ItemsSource="{Binding ClassCollection}"> ...
- hbase-indexer官网wiki
Home Requirements Getting Started Installation Tutorial Demo Indexer Configuration CLI tools Metrics ...
- html 显示 pdf
html 显示 pdf文件四种方式: 1. <embed src="pdf/wobu.pdf" type="application/pdf" width= ...
- 504 Gateway Time-out ( Nginx + PHP ) 解决小计
问题 最近有个项目,运算量比较大,服务器经常报 504 Gateway Time-out 解决方案 fastcgi_connect_timeout 默认值是 60 第一次尝试修改为 120 ,仍然报5 ...
- css3中的calc的使用
最近在布局的时候遇到一个问题,在页面中的左侧是侧边栏,右边是内容区域,内容区域中有一个固定定位的标签页,在设置固定定位的标签设置宽度的时候应该是内容区域的宽度,而固定定位的时候相对于是窗口的宽度,所以 ...
- HTML 初始
HTML(Hyper Text Markup Language的缩写)中文译为“超文本标记语言”,主要是通过HTML标签对网页中的文本.图片.声音等内容进行描述. 一.HTML 骨架结构 每种语言都有 ...
- C/C++ 函数参数传递:传值,传指针,传引用
前面我们介绍了函数的调用约定,明白了函数调用者与被调用者之间传递参数的顺序与如何进行栈恢复的. 实际上,函数调用者如何将参数传递给被调用者也是有讲究的. 总的来说,函数参数传递分为3种情况:传值,传指 ...
- Linux搭建MySQL主从
实现目标 搭建两台MySQL服务器(一主一从),一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作. 工作流程概述 主服务器: 开启二进制日志 配置唯一的server-id 获 ...