原题链接在这里: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:

  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.

题解:

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;
}
}

跟上The Maze IIThe Maze III.

LeetCode 490. The Maze的更多相关文章

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

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

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

  4. 【LeetCode】490. The Maze 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

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

  6. LeetCode 499. The Maze III

    原题链接在这里:https://leetcode.com/problems/the-maze-iii/ 题目: There is a ball in a maze with empty spaces ...

  7. LeetCode 505. The Maze II

    原题链接在这里:https://leetcode.com/problems/the-maze-ii/ 题目: There is a ball in a maze with empty spaces a ...

  8. 490. The Maze

    原题链接:https://leetcode.com/articles/the-maze/ 这道题目是需要冲会员才能使用的,然而我个穷逼现在还是失业状态根本冲不起...以后如果把免费题目都刷完了的话,再 ...

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

随机推荐

  1. 给定一个长度为N的数组,找出出现次数大于n/2,n/3的数,要求时间复杂度O(n),空间复杂度O(1)

    先讨论出现次数大于n/2的数字,如果这样的数字存在,那么这个数出现的次数大于其他数出现的次数的总和. 在数组A中,我们定义两个数据集合a1,a2.a1为出现次数大于n/2的数的集合,a2为其余数组成的 ...

  2. kafka controller脑裂(多个controller)问题

    问题:情况一:创建topic成功,但是produce的时候,却报unknown partition的错误,但zk上却显示了每个partition的leader信息:情况二: 给某个topic增加分区, ...

  3. Linux下快速安装Python3和pip

    如果本机安装了python2,尽量不要管他,使用python3运行python脚本就好,因为可能有程序依赖目前的python2环境, 比如yum!!!!! 不要动现有的python2环境! 一.安装p ...

  4. 腾讯java面试经验 (含面试题解析)

      本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.腾讯等公司offer,岗位是Java后端开发,因为发展原因最终选择去了腾讯,入职一年时间了,也成为了面试官,之 ...

  5. Julie D. Saba:儿童肿瘤学是一个令人惊奇的领域

    编者按 作为一名儿童肿瘤学家,工作中充满了挑战与机遇.近几十年来,世界各地的儿童肿瘤的发病率呈持续上升的趋势.许多人认为这不仅是由于诊断水平的提高,而且是因为儿童肿瘤的潜在风险也确实在增加.据英国儿童 ...

  6. ASP.NET 后台 COOKIE 的设置

    这几年经常与安全打交道,深知 COOKIE 对一个网站的安全影响有多大,所以在编写相与 cookie 相关代码的时候,都会特别的小心. 最近做一个系统,有几个地方用到 cookie, 然后统一把 co ...

  7. 【转载】C#通过Copy方法快速复制DataTable对象

    C#中的Datatable数据变量的操作过程中,可以通过DataTable的Copy方法快速复制当前的DataTable变量到新对象中,复制数据包含当前DataTable的结构信息如列名,同时也包含当 ...

  8. 从 Vue 的视角学 React(一)—— 项目搭建

    虽然 Vue 在国内一家独大,但在全球范围内,React 依然是最流行的前端框架 最近和一些朋友聊天,发现很多项目都选择了 React 技术栈,而且公司的新项目也决定使用 React 我一直以来都是走 ...

  9. QCache 缓存(类似于map的模板类,逻辑意义上的缓存Cache,方便管理,默认类似于LRU的淘汰算法)

    最近在学习缓存方面的知识,了解了缓存(Cache)的基本概念,为什么要使用缓存,以及一些缓存算法(缓存替换),如LRU.LFU.ARC等等. 这些缓存算法的实现过程会使用一些基本的数据结构,如list ...

  10. Lnmp环境安装禅道项目管理软件

    1.本地环境 CentOS Linux release 7.5.1804 (Core) PHP 7.1.0-dev (cli) mysql Ver 14.14 Distrib 5.7.22 nginx ...