Professional Documents
Culture Documents
on
Backtracking
Submitted toward the partial fulfillment of the requirement for the
Award of the degree of
Bachelor of Technology
In
Information
Technology
Submitted by
ADITYA KUMAR
THAKUR(2K20/IT/08)
Prof. Ritu
Agarwal
if x is a new solution
add to list of solutions
backtrack(expand x)
Of the above we are going to discuss the Maze and N Queen problem in detail to get a
better understanding of the Concept of Backtracking.
Rat In a Maze
“Rat in a Maze” is one of the trickiest questions asked in interviews and competitive
programming competitions alike. The goal here is to conceptualise and solve the
problem in an effective manner while learning new concepts such as backtracking.
A maze is basically a form of a 2D matrix in which some cells are blocked. One of the
cells is termed as a source or starting cell, from where we have to start traversing a
maze. And another one of them is termed as a destination or ending cell, where we
have to reach and reaching there would end our traversal. We have to find a path from
the source to the destination that can be traversed without moving into any of the
blocked cells and without moving out of the maze . A picture of an unsolved maze is
shown below, where grey cells denote the cells with obstacles and white cells denote
the cells which can be traversed.
To solve these types of maze problems, we first start with the source cell and move in a
direction where the path is not blocked.
● A maze is given as N*N binary matrix of blocks where the source block is the
upper leftmost block i.e., maze[0][0] and destination maze[N-1][N-1].
● A rat starts from the source and has to reach the destination.
● The Rat can move in three direction-Left, Right, Down
● In the maze matrix, 0 means the block is a dead end and 1 means the block can
be used in the path from source to destination
● If the move is possible,then move to that block and again start looking for the
valid move until the destination is reached
● Also keep on marking the cells as visited and when we traversed all the paths
possible from that block, then unmark that block for other different paths and
remove the character from the path formed
● As the last index of the grid is reached, then store the traversed path
Rat In a Maze
CODE
//RAT IN A MAZE
//submitted by:-
#include<iostream>
return true;
return false;
//maze function
if(x==n-1&&y==n-1){
solArr[x][y]=(char)48;
return true;
if(x<0||y<0||x>=n||y>=n){
return false;
if(isSafe(arr,x,y,n)){
solArr[x][y]=(char)48;
if(maze(arr,x+1,y,n,solArr)){
return true;
if(maze(arr,x,y+1,n,solArr)){
return true;
if(maze(arr,x,y-1,n,solArr)){
return true;
solArr[x][y]=(char)35;
return false;
return false;
int main(){
int n;
cin>>n;
//declaring a 2D ARRAY
char **arr=new char*[n];
for(int i=0;i<n;i++){
arr[i]=new char[n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>arr[i][j];
//passing the maze into the function to the the get the solution array
for(int i=0;i<n;i++){
solArr[i]=new char[n];
for(int j=0;j<n;j++){
if(i==0||j==0||i==n-1||j==n-1){
solArr[i][j]=(char)36;
if(arr[i][j]==(char)49){
solArr[i][j]=(char)32;
if(i!=0&&j!=0&&i!=n-1&&j!=n-1&&arr[i][j]!=(char)49){
solArr[i][j]=(char)35;
if(maze(arr,0,0,n,solArr)){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<solArr[i][j]<<' ';
cout<<endl;
}else{
}
OUTPUT
INPUT MAZE 1 AND OUTPUT 1
References
● https://www.geeksforgeeks.org/backtracking-introduction/#:~:text
=Backtracking%20is%20an%20algorithmic%2Dtechnique,reachi
ng%20any%20level%20of%20the
● https://en.wikipedia.org/wiki/Backtracking
● https://www.javatpoint.com/backtracking-introduction
● https://www.tutorialspoint.com/introduction-to-backtracking
● https://www.tutorialspoint.com/Rat-in-a-Maze-Problem
● https://www.geeksforgeeks.org/rat-in-a-maze-backtracking-2/
● https://www.codingninjas.com/blog/2020/09/02/backtracking-rat-
i n-a-maze/