public boolean hasPath(int[][] maze, int[] start, int[] destination) {
if (maze == null || maze.length == 0 || maze[0].length == 0) {
int m = maze.length, n = maze[0].length;
boolean[][] visited = new boolean[m][n];
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
return searchPathByDFS(maze, start, destination, visited, directions);
public boolean searchPathByDFS(int[][] maze, int[] start, int[] destination, boolean[][] visited, int[][] directions) {
if (visited[curRow][curCol]) {
visited[curRow][curCol] = true;
if (curRow == destination[0] && curCol == destination[1]) {
for (int[] direction : directions) {
int nextRow = curRow + direction[0];
int nextCol = curCol + direction[1];
while (isValid(nextRow, nextCol, maze)) {
if (searchPathByDFS(maze, new int[] {nextRow, nextCol}, destination, visited, directions)) {
public boolean isValid(int row, int col, int[][] maze) {
return row >= 0 && row < maze.length && col >= 0 && col < maze[0].length && maze[row][col] == 0;