JIAKAOBO

LeetCode

venmo
wechat

感谢赞助!

  • ㊗️
  • 大家
  • offer
  • 多多!

Problem

There is a ball in a maze with empty spaces (represented as 0) and walls (represented as 1). The ball can go through the 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 m x n maze, the ball’s start position and the destination, where start = [startrow, startcol] and destination = [destinationrow, destinationcol], return true if the ball can stop at the destination, otherwise return false.

You may assume that the borders of the maze are all walls (see examples).

Example 1:

image tooltip here

Input: maze = [[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]], start = [0,4], destination = [4,4]
Output: true
Explanation: One possible way is : left -> down -> left -> down -> right -> down -> right.

Example 2:

Input: maze = [[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]], start = [0,4], destination = [3,2]
Output: false
Explanation: There is no way for the ball to stop at the destination. Notice that you can pass through the destination but you cannot stop there.

Example 3:

Input: maze = [[0,0,0,0,0],[1,1,0,0,1],[0,0,0,0,0],[0,1,0,0,1],[0,1,0,0,0]], start = [4,3], destination = [0,1]
Output: false

Constraints:

  • m == maze.length
  • n == maze[i].length
  • 1 <= m, n <= 100
  • maze[i][j] is 0 or 1.
  • start.length == 2
  • destination.length == 2
  • 0 <= start_row, destination_row <= m
  • 0 <= start_col, destination_col <= n
  • Both the ball and the destination exist in an empty space, and they will not be in the same position initially.
  • The maze contains at least 2 empty spaces.

Code

class Solution {
    public boolean hasPath(int[][] maze, int[] start, int[] destination) {
        int m = maze.length;
        int n = maze[0].length;

        boolean[][] visited = new boolean[m][n];
        int[][] dirs = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};;

        Queue <int[]> queue = new LinkedList <> ();
        queue.add(start);
        // 使用二维数组标记visited
        visited[start[0]][start[1]] = true;

        while (!queue.isEmpty()) {
            int[] curr = queue.poll();

            if (curr[0] == destination[0] && curr[1] == destination[1]){
                return true;
            }

            for (int[] dir: dirs) {
                int x = curr[0];
                int y = curr[1];

                // 一直走到墙或者到头
                while (x + dir[0]>= 0 &&
                       y + dir[1] >= 0 &&
                       x + dir[0] < m &&
                       y + dir[1] < n &&
                       maze[x + dir[0]][y + dir[1]] == 0) {
                    x += dir[0];
                    y += dir[1];
                }

                // 上一步是否访问过
                if (!visited[x][y]) {
                    queue.add(new int[] {x, y});
                    visited[x][y] = true;
                }
            }
        }

        return false;
    }
}
class Solution {
    int[][] dirs={{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
    
    public boolean hasPath(int[][] maze, int[] start, int[] destination) {
        return helper(maze, start, destination, new boolean[maze.length][maze[0].length]);
    }

    private boolean helper(int[][] maze, int[] curr, int[] dest, boolean[][] visited) {
        if(curr[0] == dest[0] && curr[1] == dest[1]) return true;

        if(visited[curr[0]][curr[1]]) return false;

        visited[curr[0]][curr[1]] = true;

        for(int[] dir : dirs) {
            int x = curr[0];
            int y = curr[1];

            while(x + dir[0] >= 0 &&
                  x + dir[0] < maze.length &&
                  y + dir[1] >= 0 &&
                  y + dir[1] < maze[0].length &&
                  maze[x + dir[0]][y + dir[1]] != 1) {
                x += dir[0];
                y += dir[1];
            }

            if(helper(maze, new int[]{x, y}, dest, visited)){
                return true;
            }
        }

        return false;
    }
}