class Solution { public boolean hasPath(int[][] maze, int[] start, int[] destination) { int nr = maze.length; int nc = maze[0].length; boolean[][] visited = new boolean[nr][nc]; int[][] dirs = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; Queue queue = new LinkedList<>(); queue.add(start); visited[start[0]][start[1]] = true; while (!queue.isEmpty()) { int[] idx = queue.poll(); if (idx[0] == destination[0] && idx[1] == destination[1]) { return true; } for (int[] dir : dirs) { int x = idx[0] + dir[0]; int y = idx[1] + dir[1]; while (x >= 0 && y >= 0 && x < nr && y < nc && maze[x][y] == 0) { x += dir[0]; y += dir[1]; } if (!visited[x - dir[0]][y - dir[1]]) { queue.add(new int[] {x - dir[0], y - dir[1]}); visited[x - dir[0]][y - dir[1]] = true; } } } return false; } }