787 · The Maze - LintCode

# Description

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.

1.There is only one ball and one destination in the maze.
2.Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
3.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. (沒有 border 也不用判斷 if(maze==null) )
5.The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.

# Example

Example 1:

Input:
map = 
[
 [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]
end = [3,2]
Output:
false

Example 2:

Input:
map = 
[[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]
end = [4,4]
Output:
true

# Solution

島嶼不一樣的是,這題是從已給的起點開始,在 bfs 的時候,球撞牆的時候才要停,不會一次走一步這樣。
那直到撞牆球才換方向怎麼寫?可以加一個 while loop 裡面,如果他是牆我就不走了。
記得當退出 while loop 時已經是在牆裡了,所以要 (x, y) 各減一步退回去。

public class Solution {
    /**
     * @param maze: the maze
     * @param start: the start
     * @param destination: the destination
     * @return: whether the ball could stop at the destination
     */
    public boolean hasPath(int[][] maze, int[] start, int[] destination) {
       boolean[][] isVisited = new boolean[maze.length][maze[0].length];
       int[] dx = {0, 0, 1, -1};
       int[] dy = {1, -1, 0, 0};
       Queue<Integer> queue = new LinkedList<>();
       queue.offer(start[0]);
       queue.offer(start[1]);
       while (!queue.isEmpty()){
           int curx = queue.poll();
           int cury = queue.poll();
           
           for (int i = 0; i < 4; i++) {
               int nextx = curx + dx[i];
               int nexty = cury + dy[i];
               while (nextx >= 0 && nextx < maze.length &&
                      nexty >= 0 && nexty < maze[0].length &&
                      maze[nextx][nexty] == 0){
                          nextx += dx[i];
                          nexty += dy[i];
                }
                // 退出 while 時,代表已經在牆中了,所以要退一步回來.
                nextx -= dx[i];
                nexty -= dy[i];
                
               
                // 下一層
                if (!isVisited[nextx][nexty]){
                    isVisited[nextx][nexty] = true;
                    queue.offer(nextx);
                    queue.offer(nexty);
                }
           }
       }
       return false;
       
         
       
    }
}