Java program to find who reaches first to the destination in a 2-dimensional array.

Given a 2-dimensional array where ‘s’ stones and ‘p’ people are present on its grids, you need to find who reaches the destination grid first when people can move only left, right, top, bottom and can’t move to a grid where the stone is placed.

Approach : Depth First Search!!

Start from destination grid and keep counting the minimum distance to all available grids in 2-D array except stone grids. We are using a visited map that will keep record of grids with their minimum distance from destination grid.

This way while checking who has reached first to destination grid, we just need to traverse set of peopleGrids and compare their distance in a constant time to find who reached first to destination grid.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Hope it helps, enjoy coding!