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 ...
随机推荐
- bypass-media 模式30秒挂断
语音正常,但是通话30秒后自动挂断, 服务器为阿里云,专网模式 修改ext-sip-ip 为公网ip
- 1.0EnterpriseFrameWork 框架学习
1.先报其主页 :博主的框架是开源的 http://www.cnblogs.com/kakake/p/3938262.html . 2.学习的精髓是:该框架支持 ORM.SQL语句 和 存储过程 ,O ...
- Java数组转集合与集合转数组的坑
在Java中将数组转为集合,会用到Arrays.asList()的方法,然而,这个方法却与我们的预期期望存在一些出入,当用到asList方法将数组转化成List列表时,对得到的List列表进行add( ...
- docker-compose进阶
笔者在前文<Docker Compose 简介>和<Dcoker Compose 原理>两篇文章中分别介绍了 docker compose 的基本概念以及实现原理.本文我们将继 ...
- 使用JavaConfig配置SpringMVC
目录结构 web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi ...
- WinExec, ShellExecute,CreateProcess 区别
其中以WinExec最为简单,ShellExecute比WinExec灵活一些,CreateProcess最为复杂. WinExec 有两个参数,前一个指定路径,后一个指定显示方式. ShellExe ...
- redis AbortOnConnectFail
AbortOnConnectFail =true 服务器上停止redis service,即便后来redis服务端修好能够接通时,也不会自动连接. 所以建议设为false
- vue的v-bind详解
前言 v-bind 主要用于属性绑定,比方你的class属性,style属性,value属性,href属性等等,只要是属性,就可以用v-bind指令进行绑定.这篇文章主要介绍了VueJs中的V-bin ...
- 从 Vue 的视角学 React(二)—— 基本语法
基于 Vue.js 开发的时候,每个 vue 文件都是一个单独的组件,可以包含 HTML,JS,CSS 而 React 是以函数为基础,每个 function 就是一个组件.虽然 JSX 让 HTML ...
- echart添加轴最小值,最大值,间隔以及设置线条颜色
yAxis: [{ type: 'value' }, { type: 'value', name: '上证指数', //设置最小值,最大值,间隔 min: 1000, max: 6000, inter ...