Problem
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.
class Solution { public boolean hasPath(int[][] maze, int[] start, int[] destination) { int m = maze.length, n = maze[0].length; boolean[][] visited = new boolean[m][n]; return dfs(maze, visited, start, destination); } private boolean dfs(int[][] maze, boolean[][] visited, int[] start, int[] destination) { int row = start[0], col = start[1]; //check boundaries and if the point visited before if (row < 0 || row >= maze.length || col < 0 || col >= maze[0].length || visited[row][col]) return false; //mark as a visited stop point visited[row][col] = true; //if stop point is destination => true if (row == destination[0] && col == destination[1]) return true; //DFS on four directions int[][] dirs = new int[][]{{1,0},{-1,0},{0,1},{0,-1}}; for (int i = 0; i < 4; i++) { int x = row, y = col; //rolling until out or hit the wall while (x >= 0 && x < maze.length && y >= 0 && y < maze[0].length && maze[x][y] != 1) { x += dirs[i][0]; y += dirs[i][1]; } //back to the stop point x -= dirs[i][0]; y -= dirs[i][1]; //start another dfs from the stop point if (dfs(maze, visited, new int[]{x, y}, destination)) return true; } return false; } }Solution - BFS
class Solution { public boolean hasPath(int[][] maze, int[] start, int[] destination) { int m = maze.length, n = maze[0].length; Dequequeue = new ArrayDeque<>(); boolean[][] visited = new boolean[m][n]; queue.offer(start); while (!queue.isEmpty()) { int[] cur = queue.poll(); int row = cur[0], col = cur[1]; if (row == destination[0] && col == destination[1]) return true; if (visited[row][col]) continue; visited[row][col] = true; int[][] dirs = new int[][]{{1,0},{-1,0},{0,1},{0,-1}}; for (int[] dir: dirs) { int x = row; int y = col; while (x >= 0 && x < maze.length && y >= 0 && y < maze[0].length && maze[x][y] == 0) { x += dir[0]; y += dir[1]; } x -= dir[0]; y -= dir[1]; queue.offer(new int[]{x, y}); } } return false; } }
文章版權歸作者所有,未經(jīng)允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/72454.html
摘要:題目鏈接和上一題不一樣的是這道題要求最短的路徑,普通的遍歷和都是可以做的,但是求最短路徑的話還是用。這里相當于每個點有至多條相連,每條的就是到墻之前的長度。 490. The Maze 題目鏈接:https://leetcode.com/problems... 又是圖的遍歷問題,就是簡單的遍歷,所以dfs和bfs都可以做,復雜度也是一樣的。這道題要求球不能停下來,即使碰到destina...
摘要:開始看這道題目的時候,沒有看懂和的作用。然后對這個放入的結點開始操作遍歷的所有,當前遍歷到的的叫做。當完成,則中沒有新的結點了,退出循環(huán)。返回在中更新過的,結束。 Problem Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. We use #...
摘要:解題思路涉及到圖的遍歷無非就是深度優(yōu)先搜索廣度優(yōu)先搜索,可以先看前幾日的這篇文章就需要借助隊列實現(xiàn),可以借助棧也可以直接用遞歸實現(xiàn)。 題目: 給定無向連通圖中一個節(jié)點的引用,返回該圖的深拷貝(克隆)。圖中的每個節(jié)點都包含它的值 val(Int) 和其鄰居的列表(list[Node])。 Given a reference of a node in a connected undirec...
摘要:題目鏈接一般這種題,給一個起點,給一個終點,最后要求最短的路徑,都是用來解。的圖不是很大,也能。 The Maze II 題目鏈接:https://leetcode.com/contest/...一般這種題,給一個起點,給一個終點,最后要求最短的路徑,都是用bfs來解。 public class Solution { public String findShortestWay(...
1. 說明 本文所有的算法嚴格按照《算法導論》,本文將詳細的對BFS和DFS進行分析,并提供算法的 js 實現(xiàn),同時會對創(chuàng)建鏈表的方式進行優(yōu)化 2. 圖的表示 圖的表示分為對頂點集 V 的表示和對邊集 E 的表示,這里的重點是如何表示邊,邊的表示分為鄰接矩陣和鄰接鏈表這兩種表示方法,鄰接矩陣適合表示邊稠密的圖,其消耗空間為|V|*|V|,如果是無向圖,則可以用上三角矩陣或者下三角矩陣來表示,是空間...
閱讀 3005·2021-10-12 10:12
閱讀 3052·2021-09-22 16:04
閱讀 3287·2019-08-30 15:54
閱讀 2602·2019-08-29 16:59
閱讀 2902·2019-08-29 16:08
閱讀 868·2019-08-29 11:20
閱讀 3492·2019-08-28 18:08
閱讀 648·2019-08-26 13:43