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.
Solution #1, DFS. 这个其实也不用backtracking
比正常的DFS要多一些步骤:
1. 记录走的方向,
2. 每个方向都需要visited信息
3. 检查撞墙
4. 如果不是墙,继续当前方向
O(m * n), 最坏情况每一个格子都走一次
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][5]; return dfs(maze, destination, visited, start[0] - 1, start[1], 1) || dfs(maze, destination, visited, start[0], start[1] + 1, 2) || dfs(maze, destination, visited, start[0] + 1, start[1], 3) || dfs(maze, destination, visited, start[0], start[1] - 1, 4); } private boolean dfs(int[][] maze, int[] destination, boolean[][][] visited, int row, int col, int dir) { if (row < 0 || row >= maze.length || col < 0 || col >= maze[0].length || visited[row][col][dir] || maze[row][col] == 1) return false; visited[row][col][dir] = true; if ((dir == 1 && (row == 0 || maze[row - 1][col] == 1)) || (dir == 3 && (row == maze.length - 1 || maze[row + 1][col] == 1))) { if (destination[0] == row && destination[1] == col) return true; return dfs(maze, destination, visited, row, col - 1, 4) || dfs(maze, destination, visited, row, col + 1, 2); } if ((dir == 2 && (col == maze[0].length - 1 || maze[row][col + 1] == 1)) || (dir == 4 && (col == 0 || maze[row][col - 1] == 1))) { if (destination[0] == row && destination[1] == col) return true; return dfs(maze, destination, visited, row - 1, col, 1) || dfs(maze, destination, visited, row + 1, col, 3); } boolean flag = false; if (dir == 1) flag = dfs(maze, destination, visited, row - 1, col, 1); if (dir == 2) flag = dfs(maze, destination, visited, row, col + 1, 2); if (dir == 3) flag = dfs(maze, destination, visited, row + 1, col, 3); if (dir == 4) flag = dfs(maze, destination, visited, row, col - 1, 4); if (flag) return true; visited[row][col][dir] = false; return false; } }
DFS,在每一层递归里一直走,直到碰到墙。这是对Solution #1的简化
这里不用backtracking,因为每次遇到墙之后情况都会往4个方向尝试走。
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]; int[][] dirs = {{-1,0}, {1,0}, {0,1},{0,-1}}; return dfs(maze, destination, visited, start[0], start[1], dirs); } private boolean dfs(int[][] maze, int[] destination, boolean[][] visited, int row, int col, int[][] dirs) { if (visited[row][col]) return false; if (destination[0] == row && destination[1] == col) return true; visited[row][col] = true; for (int[] dir : dirs) { int i = 1; while (row + dir[0] * i >= 0 && row + dir[0] * i < maze.length && col + dir[1] * i >= 0 && col + dir[1] * i < maze[0].length && maze[row + dir[0] * i][col + dir[1] * i] != 1) { i++; } i--; if (dfs(maze, destination, visited, row + dir[0] * i, col + dir[1] * i, dirs)) return true; } // visited[row][col] = true; return false; } }
Solution #3, BFS
class Solution { public boolean hasPath(int[][] maze, int[] start, int[] destination) { Queue<int[]> que = new LinkedList<>(); que.add(start); int[][] dirs = {{1,0},{-1,0},{0,1},{0,-1}}; boolean[][] visited = new boolean[maze.length][maze[0].length]; while (!que.isEmpty()) { int[] pos = que.poll(); if (pos[0] == destination[0] && pos[1] == destination[1]) return true; if (maze[pos[0]][pos[1]] == 1 || visited[pos[0]][pos[1]]) continue; visited[pos[0]][pos[1]] = true; for (int[] dir : dirs) { int i = 0; while (pos[0] + dir[0] * i >= 0 && pos[0] + dir[0] * i < maze.length && pos[1] + dir[1] * i >= 0 && pos[1] + dir[1] * i < maze[0].length && maze[pos[0] + dir[0] * i][pos[1] + dir[1] * i] == 0) { i++; } i--; int[] next = {pos[0] + dir[0] * i, pos[1] + dir[1] * i}; que.add(next); } } return false; } }
Can we do it using backtracking? and what will be the time complexity?
ReplyDelete