Google | Phone | Check if path exists
Anonymous User
2845

Given a 2d array where 1 represent a stone and 0 represent water. Return true if you can reach to the last row from the first row else false. You can travel in all directions (top, bottom, left, right) by one position.

Input:
[[0, 1, 0, 1, 1],
 [0, 1, 1, 0, 1],
 [0, 0, 1, 1, 0],
 [0, 0, 0, 1, 1]]

Output: True

Input:
[[0, 1, 0, 1, 1],
 [0, 1, 1, 0, 1],
 [0, 0, 0, 1, 0],
 [0, 0, 0, 1, 1]]

Output: False

Follow up:

  1. Return the indexes of given path
  2. Return how many path exist in the matrix.

It was a standard dfs/bfs grid question. I was able to come up with the top-down optimal solution with some help. In the followup part, I don't remember what I said for first question. I could have discussed dijkstra or floyd-warshall. For the 2nd question, we can use backtracking. Please suggest your approach here.

Related question on Leetcode: https://leetcode.com/problems/unique-paths/

Comments (11)