There is aballin a maze with empty spaces and walls. The ball can go through empty spaces by rollingup,down,leftorright, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.
Given the ball'sstart position, thedestinationand themaze, 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.
In order to do this traversal, one of the simplest schemes is to undergo depth first search. In this case, we choose one path at a time and try to go as deep as possible into the levels of the tree before going for the next path.
In order to implement this, we make use of a recursive function dfs(maze, start, desination, visited).
we try to explore the search space on a level by level basis. We try to move in all the directions at every step. When all the directions have been explored and we still don't reach the destination, then only we proceed to the new set of traversals from the new positions obtained.
classSolution {// UP: {-1, 0}, RIGHT: {0, 1}, DOWN: {1, 0}, LEFT: {0, -1}privatestaticint DIR[][] = {{-1,0}, {0,1}, {1,0}, {0,-1}};publicbooleanhasPath(int[][] maze,int[] start,int[] destination) {int m =maze.length;int n = maze[0].length;boolean[][] visited =newboolean[m][n];returntraverse(maze, start, destination, visited); }privatebooleantraverse(int[][] maze,int[] position,int[] destination,boolean[][] visited) {if (visited[position[0]][position[1]]) {returnfalse; }if (Arrays.equals(position, destination)) {returntrue; } visited[position[0]][position[1]] =true;// Roll the ball for `UP, RIGHT, DOWN, LEFT` four directionsfor (int i =0; i <DIR.length; i++) {int[] newPosition =roll(maze, position,DIR[i]);if (traverse(maze, newPosition, destination, visited)) {returntrue; } }returnfalse; }// Roll the ball based on current position and direction, return new stopped postionprivateint[] roll(int[][] maze,int[] position,int[] direction) {int row = position[0];int col = position[1];while (canRoll(maze, row + direction[0], col + direction[1])) { row += direction[0]; col += direction[1]; }returnnewint[] {row, col}; }// Using next row, col position to check if a ball can rollprivatebooleancanRoll(int[][] maze,int nrow,int ncol) {int m =maze.length;int n = maze[0].length;if (nrow <0|| nrow >= m || ncol <0|| ncol >= n) {returnfalse; }if (maze[nrow][ncol] ==1) {returnfalse; }returntrue; }}
Time complexity :O(mn). Complete traversal of maze will be done in the worst case. Here, m and n refers to the number of rows and coloumns of the maze.
Space complexity :O(mn). visited array of size m*n is used.