The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. ― Donald E. Knuth

# Algorithms

## Backtracking

Problem :-
Given a maze in the form of a matrix of size n x n. A robot is placed at cell ( 0, 0 ).
Print all possible paths that robot can take to reach the last cell ( n-1, n-1 ) of the maze.
Constraint :- Robot can move rightwards ( R ) or downwards ( D ).
Consider the maze of size 3 x 3 as shown below :-
X X X         Robot is placed at ( 0, 0 )
X X X
X X X
Possible paths that robot can take are :-
X X X        X X X        X X X        X X X        X X X         X X X
X X X        X X X        X X X        X X X        X X X         X X X
X X X        X X X        X X X        X X X        X X X         X X X
DDRR        DRDR        DRRD        RDDR       RDRD         RRDD

Solution :-
At any point ( cell ) in the maze, robot can move horizontally ( right cell ) or vertically ( down cell ).
1 ) Suppose the robot chooses the horizontal move, we will move to the right cell and again choose one of the possible moves ( horizontal or vertical ) and repeat these steps till we reach the last cell. If at any cell, a valid movement is not possible then we backtrack and choose other alternative move.
2 ) Same steps are followed if the robot chooses the vertical move.
We try out the horizontal and vertical alternatives at every cell to find out all possible paths that the robot can take to reach the final cell in the maze. See the implementation below.