Hello everyone,
I have a problem in backtracking the solution in RAT maze . The problem
just needs the number of solutions to the maze. But i wanted to determine what are the solutions of this maze.
Like we must find all the path in which the rat must move to reach destination,
i.e.,directions need to be followed…l-left, r-right, d-down, u-up.
One solution of the following example is rrdd denoting right right down down.
I can only give you algorithm but it is very easy to convert it to code
I hope you know how to calculate the number of ways for your example the numbers of ways is 2 it can be calculate easily using another matrix ( Let us denote WM) to store number of way to reach aparticular block so
for
0 0 0
0 1 0
0 0 0
WM will be
1 1 1
1 0 1
1 1 2
( if you have doubt in how i calculated above WM , Comment… I will explain )
Now you have WM and it is known that we MUST start from upper leftmost block
in order to generate all the ways
u can use following pseudo code ( it uses stack )…
def print_path(present block , stack , direction)
if present block has zero :
return
if present block is destination:
print all the entries in stack and the direction also
else
decrease one in present block
push the direction in stack
print_path(present block to right , stack , right);
print_path(present block to down , stack , down);
return;
after writing the algo i came to know that we can go up and left also I Only did for right down may be you can extend this to that also , you take care of the limiting conditions like end of matrix etc…