954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Grid problem

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.

ajhais
Newbie Poster
5 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 
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.

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

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.

Hi.. thanks for the solution. I was thinking of going with the same.. How can we approach this problem if the map is dynamic.. that is if some obstacles can randomly appear in the map.. We actually need to code a bot for battle city game..

ajhais
Newbie Poster
5 posts since Jul 2008
Reputation Points: 10
Solved Threads: 0
 
Hi.. thanks for the solution. I was thinking of going with the same.. How can we approach this problem if the map is dynamic.. that is if some obstacles can randomly appear in the map.. We actually need to code a bot for battle city game..

Well if you know DFSearch then you will know that each time the whole array gets passed as one of the function argument to the recursive DFSearch function and checking is done in a for loop.
So before checking in the for loop in case of dynamic obstacles just change the particular

Array[][] = 0;

before passing the array to next DFS function recursively.

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

ajhais,
It should actually be a Breadth First Search (BFS), not a DFS.

nucleon
Posting Pro in Training
478 posts since Oct 2008
Reputation Points: 163
Solved Threads: 91
 
ajhais, It should actually be a Breadth First Search (BFS), not a DFS.

No its a DFS only not BFS : You don't need to move to all points adjoining a point,if you wanted to do that then BFS is required. But in this problem we want to move through different blocks taking steps moving away from start point so we need DFS where once we reach a point then we need to take a step towards destination not worrying about the other points reachable from that point.
After all evaluation we need to find the shortest path.

csurfer
Posting Pro
568 posts since Jan 2009
Reputation Points: 485
Solved Threads: 88
 

I don't feel like arguing. Can someone else weigh in on this? DFS or BFS?

nucleon
Posting Pro in Training
478 posts since Oct 2008
Reputation Points: 163
Solved Threads: 91
 

If you know that there is a way, why not go for it, why should one try all the ways possible? If you are lucky you would have chose the right path the first time. I would go for DFS. I believe one should use a stack and not queue.

Prabakar
Posting Whiz
342 posts since May 2008
Reputation Points: 94
Solved Threads: 33
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You