

方法
class Solution {
public boolean hasPath(int[][] maze, int[] start, int[] destination) {
boolean[][] visited = new boolean[maze.length][maze[0].length];
return dfs(maze, start, destination, visited);
}
public boolean dfs(int[][] maze, int[] cur, int[] dest, boolean[][] visited) {
if (visited[cur[0]][cur[1]]) {
return false;
}
if (cur[0] == dest[0] && cur[1] == dest[1]) {
return true;
}
visited[cur[0]][cur[1]]= true;
int[] dirX = new int[]{0, 0, -1, 1};
int[] dirY = new int[]{-1, 1, 0, 0};
for (int i = 0; i < 4; i++) {
int row = cur[0], col = cur[1];
while (row >= 0 && row < maze.length
&& col >=0 && col < maze[0].length
&& maze[row][col] == 0) {
row += dirY[i];
col += dirX[i];
}
int[] next = new int[]{row - dirY[i], col - dirX[i]};
if (dfs(maze, next, dest, visited)) {
return true;
}
}
return false;
}
}