views:

128

answers:

2

"Bob has become lost in his neighborhood. He needs to get from his current position back to his home. Bob's neighborhood is a 2 dimensional grid, that starts at (0, 0) and (width - 1, height - 1). There are empty spaces upon which bob can walk with no difficulty, and houses, which Bob cannot pass through. Bob may only move horizontally or vertically by one square at a time.

Bob's initial position will be represented by a 'B' and the house location will be represented by an 'H'. Empty squares on the grid are represented by '.' and houses are represented by 'X'. Find the minimum number of steps it takes Bob to get back home, but if it is not possible for Bob to return home, return -1.

An example of a neighborhood of width 7 and height 5:

...X..B
.X.X.XX
.H.....
...X...
.....X.

"

A: 

If Bob was adjacent to his house, could u figure out the answer?

Otherwise let N, E, W, S be the squares immediately to the north, east, west, and south of Bob's starting position. If I gave u the answers to the shortest path from N, E, W, S to home, could u figure out the answer from B to H?

emory
A: 

Since it's homework, i m not supposed to provide a solution.

I would say just try to solve it recursively, as you have to repeat the set of operations -

find the possible moves, pick one, return if its the Home other wise pick the next move.

Also for shortest path you have to count the steps to solution and later if you got multiple solutions then select the one with minimum steps.

Hemant