35. Geeks Village and Wells | Code
Medium

Problem Statement

Return a 2D integer matrix of size n*m detailing the shortest path distance to fetch water from the nearest well (back and forth). Cells with 'N', 'W', and '.' have answer 0. Unreachable 'H' should be -1.

Examples

1Example 1
Input:
{ "n": 3, "m": 3, "c": [ [ "H", "H", "H" ], [ "H", "W", "H" ], [ "H", "H", "H" ] ] }
Output:
[ [ 4, 2, 4 ], [ 2, 0, 2 ], [ 4, 2, 4 ] ]
2Example 2
Input:
{ "n": 2, "m": 2, "c": [ [ "H", "N" ], [ ".", "W" ] ] }
Output:
[ [ 4, 0 ], [ 0, 0 ] ]
3Example 3
Input:
{ "n": 2, "m": 3, "c": [ [ "H", "N", "H" ], [ "N", "N", "W" ] ] }
Output:
[ [ -1, 0, 2 ], [ 0, 0, 0 ] ]
Loading...

Sign in to Run Code and Submit