Create a matrix distance[x][y] and set all values to a large number.
This will represent the distance to travel to the destination on the 
shortest route.
Now set the distance at the destination to zero.
Set all adjacent locations to one, and repeat the process recursively, 
always setting valid adjacent locations with a larger distance value to the 
distance from the current location plus one. In the end, you will have the 
distance from every location on the grid. Now you can find the shortest 
path from any location by always moving to a space which is closer than 
your current location.

Don

On Sunday, April 20, 2014 11:52:44 AM UTC-4, bujji wrote:
>
> Consider a city whose streets are defined by an X ×Y grid. We are 
> interested in
> walking from the upper left-hand corner of the grid to the lower 
> right-hand corner.
> Unfortunately, the city has bad neighborhoods, whose intersections we do 
> not want
> to walk in. We are given an X × Y matrix BAD, where BAD[i,j] = “yes” if and
> only if the intersection between streets i and j is in a neighborhood to 
> avoid.
>
> Give an O(XY ) algorithm to find the shortest path across the grid that 
> avoids
> bad neighborhoods. You may assume that all blocks are of equal length. For 
> partial
> credit, give an O(X^2 Y^2) algorithm.
>
>  If we walk in down or right directions only Dynamic programming solution 
> would be simple. But because of bad intersection points,  we may need to 
> walk in up, down, right or left directions.
>
> -Thanks 
> Bujji
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to algogeeks+unsubscr...@googlegroups.com.

Reply via email to