Sunday, July 8, 2007

Solve the Rat In A Maze problem using backtracking.

This is one of the classical problems of computer science. There is a rat trapped in a maze. There are multiple paths in the maze from the starting point to the ending point. There is some cheese at the exit. The rat starts from the entrance of the maze and wants to get to the cheese.

This problem can be attacked as follows.




Have a m*m matrix which represents the maze.

For the sake of simplifying the implementation, have a boundary around your matrix and fill it up with all ones. This is so that you know when the rat is trying to go out of the boundary of the maze. In the real world, the rat would know not to go out of the maze, but hey! So, initially the matrix (I mean, the maze) would be something like (the ones represent the "exra" boundary we have added). The ones inside specify the obstacles.


111111111111111111111
100000000000000000001
100000010000000000001
100000010000000000001
100000000100001000001
100001000010000000001
100000000100000000001
100000000000000000001
111111111111111111111



The rat can move in four directions at any point in time (well, right, left, up, down). Please note that the rat can't move diagonally. Imagine a real maze and not a matrix. In matrix language



Moving right means adding {0,1} to the current coordinates.

Moving left means adding {0,-1} to the current coordinates.

Moving up means adding {-1,0} to the current coordinates.

Moving right means adding {1,0} to the current coordinates.



The rat can start off at the first row and the first column as the entrance point.

From there, it tries to move to a cell which is currently free. A cell is free if it has a zero in it.

It tries all the 4 options one-by-one, till it finds an empty cell. If it finds one, it moves to that cell and marks it with a 1 (saying it has visited it once). Then it continues to move ahead from that cell to other cells.

If at a particular cell, it runs out of all the 4 options (that is it cant move either right, left, up or down), then it needs to backtrack. It backtracks till a point where it can move ahead and be closer to the exit.

If it reaches the exit point, it gets the cheese, ofcourse.

The complexity is O(m*m).



Here is some pseudocode to chew upon


findpath()
{
Position offset[4];
Offset[0].row=0; offset[0].col=1;//right;
Offset[1].row=1; offset[1].col=0;//down;
Offset[2].row=0; offset[2].col=-1;//left;
Offset[3].row=-1; offset[3].col=0;//up;

// Initialize wall of obstacles around the maze
for(int i=0; i < m+1;i++)
maze[0][i] = maze[m+1][i]=1; maze[i][0] = maze[i][m+1]=1;

Position here;
Here.row=1;
Here.col=1;

maze[1][1]=1;
int option = 0;
int lastoption = 3;

while(here.row!=m || here.col!=m)
{
//Find a neighbor to move
int r,c;

while (option <= LastOption)
{
r=here.row + offset[position].row;
c=here.col + offset[option].col;
if(maze[r][c]==0)break;
option++;
}

//Was a neighbor found?
if(option <= LastOption)
{
path->Add(here);
here.row=r;here.col=c;
maze[r][c]=1;
option=0;
}
else
{
if(path->Empty())return(False);
Position next;
Path->Delete(next);
If(new.row==here.row)
Option=2+next.col - here.col;
Else { option = 3 + next.row - here.col;}
Here=next;
}
return(TRUE);
}
}

17 comments:

Mukund said...

Kindly give the explaination for the code.. I will be very much obliged. thank you

Mukund said...

Kindly give the explaination for the code.. I will be very much obliged. thank you

Anonymous said...

nice and easy explaination ...but the code should b elaborated a bit for the students who have just started datastructure...thank you..

Anonymous said...

Please explain the Code.

Anonymous said...

Genial dispatch and this enter helped me alot in my college assignement. Thank you for your information.

Anonymous said...

can u xplain d code with a simple em\xample

Anonymous said...

Copied from crack the interview!!! SUCKER!!

Goran said...

Could make use of recursion... much more effective and smaller!

public boolean move(int x, int y, Maze maze){

//If we found the cheese!
if (x==endX && y==endY) {
maze.printMaze();
return true;
}

/*Are we hitting a wall?*/
if(maze.isWall(x,y)) return false;
else maze.setTrail(x, y);
System.out.println("x:"+x+" y:"+y+" size:"+maze.getSize());

//First we try to move right:
if( (x+1)0 && this.move(x-1, y, maze) ) return true;
if( (y-1)>0 && this.move(x, y-1, maze) ) return true;

//No options... lets take a step back?
maze.clearTrail(x, y);

return false;

}

Anonymous said...

I think in the else part of function
while setting option value

It must be set as
option = 3 + next.row - here.row

amit said...

This blog is very interesting for me and others, I will return very soon for other items, thank you very much!


Ant treatment Vancouver

Anonymous said...

I blog quite often and I genuinely thank you for your information.
This great article has truly peaked my interest. I'm going to book mark your site and keep checking for new information about once per week. I opted in for your Feed too. sprawdĹş to, http://www.clubdiy.com/zbxe/index.php?mid=study_tusang&document_srl=1794

Anonymous said...


You actually said that fantastically!
My site: sex dziewczyny,
tani sex telefon: http://tanisextelefon.waw.pl

Anonymous said...

whoah this blog is magnificent i really like reading your articles.
Stay up the great work! You realize, many persons are looking around
for this info, you could help them greatly.

Feel free to visit my website :: calculate bmr

Anonymous said...

Spot on with this write-up, I seriously think this web site needs far more attention.
I'll probably be back again to read through more, thanks for the info!

My site :: diet that works

Anonymous said...

Howdy! This blog post couldn't be written much better! Reading through this post reminds me of my previous roommate! He always kept preaching about this. I most certainly will send this article to him. Fairly certain he'll have a
very good read. I appreciate you for sharing!


Check out my web-site :: raspberry ketones

Anonymous said...

Hello, I read your blogs daily. Your writing style is awesome,
keep up the good work!

My page: calories walking calculator

Anonymous said...

I am actually delighted to glance at this weblog posts which carries tons of helpful facts, thanks for providing these data.


Here is my blog :: calories walking