Given a n*n grid with free spaces and obstacles in the form an n*n integer matrix. We need to find shortest between any two given points (x1,y1) and (x2,y2) through the free spaces. We are not allowed to move in diagonal. Please help me figuring out an algorithm for doing this. Thanks in advance.
Well this is one of the many ideas you can apply to this problem.Here it goes:
Firstly take the value of n for your n*n grid from the user and also a valid grid structure input where free spaces are represented by 1's and obstacles are represented by 0's.
Your array for 4*4 might look like this:
0 1 2 3
0 1 1 0 0
1 0 1 0 0
2 1 1 1 1
3 0 1 1 1
Now pass this array as input array and (x1,y1) as input co-ordinate
and (x2,y2) as the destination point to be reached for a "DEPTH FIRST CHECK" graph algorithm.
Actually the best graph algorithm to find the shortest path between two vertices is "DIJKSTRA'S ALGORITHM" but here as you are just moving one step to top bottom left or right only and all steps are of same length you can use Depth First Search only to solve your problem.