标签:while sem idt dir vat start present rectangle problem
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.
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.
DFS
1 class Solution { 2 public boolean hasPath(int[][] maze, int[] start, int[] destination) { 3 int m = maze.length, n = maze[0].length; 4 boolean[][] visited = new boolean[m][n]; 5 return dfs(maze, visited, start, destination); 6 } 7 private boolean dfs(int[][] maze, boolean[][] visited, int[] start, int[] destination) { 8 int row = start[0], col = start[1]; 9 if (row < 0 || row >= maze.length || col < 0 || col >= maze[0].length || visited[row][col]) return false; 10 visited[row][col] = true; 11 if (row == destination[0] && col == destination[1]) return true; 12 13 int[] directions = { 0, 1, 0, -1, 0 }; 14 for (int i = 0; i < directions.length - 1; i++) { 15 int[] newStart = roll(maze, start[0], start[1], directions[i], directions[i + 1]); 16 if (dfs(maze, visited, newStart, destination)) return true; 17 } 18 return false; 19 } 20 21 private int[] roll(int[][] maze, int row, int col, int rowInc, int colInc) { 22 while (canRoll(maze, row + rowInc, col + colInc)) { 23 row += rowInc; 24 col += colInc; 25 } 26 return new int[]{row, col}; 27 } 28 29 private boolean canRoll(int[][] maze, int row, int col) { 30 if (row >= maze.length || row < 0 || col >= maze[0].length || col < 0 || maze[row][col] == 1) return false; 31 return true; 32 } 33 }
BFS
1 class Solution { 2 public boolean hasPath(int[][] maze, int[] start, int[] destination) { 3 Deque<int[]> queue = new ArrayDeque<>(); 4 boolean[][] visited = new boolean[maze.length][maze[0].length]; 5 queue.offer(start); 6 while (!queue.isEmpty()) { 7 int[] cur = queue.poll(); 8 int row = cur[0], col = cur[1]; 9 if (row == destination[0] && col == destination[1]) { 10 return true; 11 } 12 if (visited[row][col]) { 13 continue; 14 } 15 visited[row][col] = true; 16 17 int[] directions = { 0, 1, 0, -1, 0 }; 18 for (int i = 0; i < directions.length - 1; i++) { 19 int[] newStart = roll(maze, row, col, directions[i], directions[i + 1]); 20 queue.offer(newStart); 21 } 22 } 23 return false; 24 } 25 26 private int[] roll(int[][] maze, int row, int col, int rowInc, int colInc) { 27 while (canRoll(maze, row + rowInc, col + colInc)) { 28 row += rowInc; 29 col += colInc; 30 } 31 return new int[] { row, col }; 32 } 33 34 private boolean canRoll(int[][] maze, int row, int col) { 35 if (row >= maze.length || row < 0 || col >= maze[0].length || col < 0 || maze[row][col] == 1) 36 return false; 37 return true; 38 } 39 }
标签:while sem idt dir vat start present rectangle problem
原文地址:https://www.cnblogs.com/beiyeqingteng/p/11146097.html