Using Java Given 2d Matrix Target Element Find Length Shortest Path Top Left Element Targe Q37085187

– Using Java, given a 2D matrix and a target element, find thelength of the shortest path from the top-left element to the targetelement in the 2D matrix. ‘1’ in the matrix indicates a path while’0′ indicates a wall and cannot be used as part of the path. Youmay use your own helper functions.

– Function Header: public int shortestPath(int[] matrix, inttarget)

– Example Input:

target = 15

matrix =

[ [1, 1, 15, 0],

[1, 0, 1, 0],

[1, 1, 1, 0] ]

Output = 2; Since we go from position (0,0) -> (0,1) ->(0,2) which is our target element.


Solution


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.