[Leetcode] The Maze

https://leetcode.com/problems/the-maze/?tab=Description

Thought:

It only asks to return a boolean value of whether the ball can roll to destination; One thing to pay attention is that the ball won't be able to change direction untll it hits a wall. Thus, unlike other matrix problems that it goes one step each time, it could roll several steps at one time.

 

1) BFS:

- Use a queue to store all possible stop points from current position.  

- Use boolean[][] to remember whether that pos has been to before. If yes, we dont save that point to queue.

- If the ball arrives at destination, return true. Done!

public boolean hasPath(int[][] maze, int[] start, int[] destination) {
        if(maze == null || maze.length==0){
            return false;
        }
        Queue<int[]> points = new LinkedList<int[]>();
        boolean[][] visited = new boolean[maze.length][maze[0].length];
        visited[start[0]][start[1]] = true;
        points.offer(start);
        int[] offsets = new int[]{0,1,0,-1,0};
        while(!points.isEmpty()){
            int[] cur = points.poll();
            for(int i=0;i<offsets.length-1;i++){
                 int x = cur[0];
                 int y = cur[1];
                 while((x + offsets[i]>=0&&x+offsets[i]<maze.length) && (y+offsets[i+1]>=0 && y+offsets[i+1]<maze[0].length) && maze[x + offsets[i]][y+offsets[i+1]] == 0){
                    x += offsets[i];
                    y += offsets[i+1];
                }
                if(x == destination[0] && y == destination[1]) {
                    return true;
                }
                if(!visited[x][y]){
                    points.offer(new int[]{x,y});
                    visited[x][y] = true;
                }
            }
        }
      return false;
    }

 

2) DFS:

- At each pos, the ball can go to any of 4 directions. Continue going one direction forward until a wall("1") is hit.  Then, start from that last stop point and continue moving in that direction.  

- Use boolean[][] to remember whether that pos has been to before. If yes, we skip going into same pos.

- If the ball arrives at destination, return true. Done!

 

    public boolean hasPath(int[][] maze, int[] start, int[] destination) {
        if(maze == null || maze.length == 0){
            return false;
        }
        boolean[][] visited = new boolean[maze.length][maze[0].length];
        int[] directions = { 0, 1, 0, -1, 0 };
        return dfs(maze, visited, start, destination, directions);
    }
    
    private boolean dfs(int[][] maze, boolean[][] visited, int[] start, int[] destination, int[] directions) {
        if (visited[start[0]][start[1]]) return false;
        if (Arrays.equals(start, destination)) return true;
        
        visited[start[0]][start[1]] = true;
        
        for (int i = 0; i < directions.length - 1; i++) {
            int[] newStart = roll(maze, start[0], start[1], directions[i], directions[i + 1]);
            if (dfs(maze, visited, newStart, destination, directions)) return true;
        }
        
        return false;
    }
    
    private int[] roll(int[][] maze, int row, int col, int rowInc, int colInc) {
        // keeps rolling until it reaches a wall!!
        while (row + rowInc >=0 && row + rowInc < maze.length &&  col + colInc >=0 && col + colInc < maze[0].length && maze[row + rowInc][col + colInc] == 0) {
            row += rowInc;
            col += colInc;
        }
        return new int[]{row, col};
    }

Time complexity: - O(n^2).

 

posted @ 2017-03-11 07:04  fifi努刷题  阅读(342)  评论(0)    收藏  举报