You are on page 1of 20

Project Report

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)

UNDER THE SUPERVISION


OF

Prof. Ritu
Agarwal

Delhi Technological University

Bawana Road, Delhi-110042


Introduction to Backtracking

Backtracking is a technique based on algorithms to solve problems. It uses recursive


calling to find the solution by building a solution step by step increasing values with
time. It removes the solutions that don't give rise to the solution of the problem based on
the constraints given to solve the problem.
Backtracking algorithm is applied to some specific types of problems,
● Decision problem used to find a feasible solution of the problem.
● Optimisation problem used to find the best solution that can be applied.
● Enumeration problem used to find the set of all feasible solutions of the problem.
In a backtracking problem, the algorithm tries to find a sequence path to the solution
which has some small checkpoints from where the problem can backtrack if no feasible
solution is found for the problem.
State Space Tree
A space tree is a tree representing all the possible states (solution or non-solution) of
the problem from the root as an initial state to the leaf as a terminal state.

State Space Tree


Algorithm for Backtracking
Backtrack(x)
if x is not a solution
return false

if x is a new solution
add to list of solutions
backtrack(expand x)

Backtracking applies to various Problems

● To find all Hamiltonian Paths present in a graph


● To solve the N Queen Problem
● Maze Solving Problem
● The knight’s tour problem

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

The objective is to analyse one of the most asked programming questions in


competitive programming.

“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.

Its solution looks like

ALGORITHM FOR RAT IN A MAZE


WE HAVE ALSO EMPLOYED DEPTH FIRST SEARCH ALGORITHM

● 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:-

//ADITYA KUMAR THAKUR(2K20/IT/08)

#include<iostream>

using namespace std;

//function to check if the next step is safe or not

//meaning next step is not an obstacle or is out of the array

bool isSafe(char** arr,int x,int y,int n){

if(x<n&&y<n && arr[x][y]==(char)49){

return true;

return false;

//maze function

bool maze(char **arr,int x,int y,int n,char **solArr){

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;

cout<<"ENTER NUMBER OF ROWS AND COLUMNS:";

cin>>n;

//declaring a 2D ARRAY
char **arr=new char*[n];

for(int i=0;i<n;i++){

arr[i]=new char[n];

cout<<”0 means obstacle”<<endl;

cout<<”1 means path can be traversed”<<endl;

//taking input of maze

cout<<"ENTER THE MAZE:-"<<endl;

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

// and declaring 2D SOLUTION ARRAY

char **solArr=new char*[n];

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;

//printing the final 2D array

cout<<"PRINT THE MAZE PATH TRAVELLED:-"<<endl;

if(maze(arr,0,0,n,solArr)){

cout<<”0 represents paths travelled”<<endl;

cout<<”# represents obstacles”<<endl;

cout<<”$ represents the boundary”<<endl;

for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

cout<<solArr[i][j]<<' ';

cout<<endl;

}else{

cout<<”NO PATH THAT CAN BE TRAVERSED FOUND”<<endl;}

}
OUTPUT
INPUT MAZE 1 AND OUTPUT 1

INPUT MAZE 2 AND OUTPUT 2

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/

You might also like