check 787. The Maze
Description
中文English
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.
Have you met this question in a real interview?
Example
Given:
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
start coordinate (rowStart, colStart) = (0, 4)
destination coordinate (rowDest, colDest) = (4, 4)
Return:true
Comments
Post a Comment