FAANG | Onsite | Longest path in a maze
Anonymous User
1549

Please can someone help me with this question:
Find the longest path in the maze. Start from any cell in the top row and the last cell in your path has to be in the bottom row. There are obstacles in the maze. No cell can be visited more than once. Traverse only Adjacent cells (Up,down,right,left)
In the below example 1-obstacle, 0-valid cell.

1100101
1101101
1100010
1000000
1111000

Thanks @mdu_
Update Path:
(0,3)->(0,2)->(1,2)->(2,2)->(3,2)->(3,3)->(2,3)->(2,4)->(3,4)->(3,5)->(3,6)->(4,6)->(4,5)->(4,4)
Ans:14
I had solved it using DFS with backtracking. Any thoughts?

Thanks @nasus
Question updated: (to consider only down,left and right cells)
Find the longest path in the maze. Start from any cell in the top row and the last cell in your path has to be in the bottom row. There are obstacles in the maze. No cell can be visited more than once. Traverse only Adjacent cells (down,right,left)
In the below example 1-obstacle, 0-valid cell.

1100101
1101101
1100010
1000000
1111000

Path: (considering only down)
(0,3)->(0,2)->(1,2)->(2,2)->(3,2)->(3,3)->(3,4)->(3,5)->(3,6)->(4,6)->(4,5)->(4,4)
Ans:12
How to solve this using DP?

Comments (3)