美文网首页
490. The Maze

490. The Maze

作者: Jeanz | 来源:发表于2017-09-07 00:20 被阅读0次

    There is a ball in a maze with empty spaces and walls. The ball can go through 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 ball's start position, the destination and the maze, determine whether the ball could stop at the destination.
    The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.
    Example 1

    **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.[图片上传中。。。(1)]
    

    Example 2

    **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.[图片上传中。。。(2)]
    
    **Note:**
    There is only one ball and one destination in the maze.
    Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
    The given maze does not contain border (like the red rectangle in the example pictures), but you could assume the border of the maze are all walls.
    The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.
    
    一刷
    题解:与一般的maze不同,最后点不仅要走到终点,并且走到终点时要hitting the wall才会停下来。就是假设我们选择了一个方向,球不碰壁不会停,然后再选择另一个方向。
    还是用BFS来解, 不过每次入队列的都是选择一个方向之后停下来的那个点
    ```java
    public class Solution {
        class Point {
            int x,y;
            public Point(int _x, int _y) {x=_x;y=_y;}
        }
        public boolean hasPath(int[][] maze, int[] start, int[] destination) {
            int m=maze.length, n=maze[0].length;
            if (start[0]==destination[0] && start[1]==destination[1]) return true;
            int[][] dir=new int[][] {{-1,0},{0,1},{1,0},{0,-1}};
            boolean[][] visited=new boolean[m][n];
            LinkedList<Point> list=new LinkedList<>();
            visited[start[0]][start[1]]=true;
            list.offer(new Point(start[0], start[1]));
            while (!list.isEmpty()) {
                Point p=list.poll();
                int x=p.x, y=p.y;
                for (int i=0;i<4;i++) {
                    int xx=x, yy=y;
                    while (xx>=0 && xx<m && yy>=0 && yy<n && maze[xx][yy]==0) {
                        xx+=dir[i][0];
                        yy+=dir[i][1];
                    }
                    xx-=dir[i][0];
                    yy-=dir[i][1];
                    if (visited[xx][yy]) continue;
                    visited[xx][yy]=true;
                    if (xx==destination[0] && yy==destination[1]) return true;
                    list.offer(new Point(xx, yy));
                }
            }
            return false;
            
        }
    }
    

    二刷同一刷
    BFS(queue)代码更简洁:

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

    三刷
    DFS

    class Solution {
        int[][] dirs = {{1,0}, {-1,0}, {0,1}, {0,-1}};
        int m, n;
        public boolean hasPath(int[][] maze, int[] start, int[] destination) {
            if(maze == null || maze.length == 0||maze[0].length==0) return false;
            m = maze.length;
            n = maze[0].length;
            boolean[][] visited = new boolean[m][n];
            return dfs(visited, maze, start[0], start[1], destination);
        }
        
        private boolean dfs(boolean[][] visited, int[][] maze, int i, int j, int[] destination){
            visited[i][j] = true;
            for(int[] dir : dirs){
                int x = i;
                int y = j;
                while(x>=0 && x<m && y>=0 && y<n && maze[x][y] == 0){
                    x += dir[0];
                    y += dir[1];
                }
                x-=dir[0];
                y-=dir[1];
                if(destination[0] == x && destination[1] == y) return true;
                if(!visited[x][y]){
                    if(dfs(visited, maze, x, y, destination)) return true;
                }
            }
            return false;
        }
    }
    

    相关文章

      网友评论

          本文标题:490. The Maze

          本文链接:https://www.haomeiwen.com/subject/uejbjxtx.html