0% found this document useful (0 votes)
4K views18 pages

Daa Mini Project

The document describes finding the shortest safe route in a matrix with landmines using a backtracking algorithm. It includes a contribution table listing three contributors and their roles. It also includes pseudocode for marking unsafe cells and implementing the backtracking search to find the shortest path. The time complexity of the algorithm is O(4(n^2)). The conclusion states that a backtracking approach was used to find all possible solutions in a brute force manner.

Uploaded by

Sai Rohit Paturi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4K views18 pages

Daa Mini Project

The document describes finding the shortest safe route in a matrix with landmines using a backtracking algorithm. It includes a contribution table listing three contributors and their roles. It also includes pseudocode for marking unsafe cells and implementing the backtracking search to find the shortest path. The time complexity of the algorithm is O(4(n^2)). The conclusion states that a backtracking approach was used to find all possible solutions in a brute force manner.

Uploaded by

Sai Rohit Paturi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Problem Statement: Describes the problem scenario involving navigation through a matrix with landmines to find the shortest safe path.
  • Problem Explanation with Diagram and Example: Explains the problem visually with diagrams indicating landmine positions and pathfinding details.
  • Algorithm Used and Example: Describes the Backtracking algorithm employed and conditions under which it applies to the problem.
  • Code with Sample Input: Presents the complete code for solving the problem with relevant functions and logic explained.
  • Conclusion: Summarizes the project's findings, emphasizing the effectiveness of the algorithm in finding safe paths.
  • Time Complexity: Discusses the computational time complexity of the implemented algorithm, highlighting performance considerations.
  • References: Lists the resources and references used for project development, aiding further research and verification.

CONTRIBUTION TABLE

NAME CONTRIBUTION
Harshit Batra (RA2011026010206) Developing algorithm, prepared and
presented the ppt
Saahir Khan (RA2011026010218) Developing the algorithm, prepared the
report
Aman Kumar (RA2011026010226) Developing the algorithm, analysis of the
algorithm, ideated the application of
algorithm
Shortest safe route in a path with landmines

Problem Statement:
Imagine a case in which we are given a path in the form of a rectangular
matrix having few landmines arbitrarily placed , calculate length of the
shortest safe route possible from any cell in the first column to any cell in
the last column of the matrix. We have to avoid landmines and their four
adjacent cells (left, right, above and below) . We are allowed to move to
only adjacent cells which are not landmines. i.e. the route cannot contains
any diagonal moves.
Problem Explanation with Diagram and Example

Consider the field of size 4*4, shown below. The cells containing landmine
are marked with 0 and red colour. The cells near the landmine which are
unsafe are marked with a light red colour.

The shortest safe route for person, starting from any cell in the first column
to any cell in the last column of the field is marked with green colour. The
length of the path is 3.
Algorithm Used

BACKTRACKING ALGORITHM

• Backtracking is used to solve problems in which a sequence of


objects is chosen from a specified set so that the sequence
satisfies some criterion.

• Backtracking is a modified depth-first search of a tree.

• Backtracking is the procedure whereby, after determining that a


node can lead to nothing but dead nodes, we go back
("backtrack") to the node's parent and proceed with the search
on the next child.
Approach used in solving the problem
• At each intersection, we have to decide between three or fewer choices:
Go straight
Go left
Go right
• Each choice leads to another set of choices.
• One or more sequences of choices may (or may not) lead to a solution
• We first mark all adjacent cells of the landmines as unsafe.
• Then for each safe cell of first column of the matrix we move forward in all
allowed directions and recursively checks if they leads to the destination or
not.
• If destination is found, we update the value of shortest path .
• if none of the above solutions work we return false from our function
Code With Sample Input:

#include <bits/stdc++.h>
using namespace std;
#define R 12
#define C 10

// These arrays are used to get row and column


// numbers of 4 neighbours of a given cell
int rowNum[] = { -1, 0, 0, 1 };
int colNum[] = { 0, -1, 1, 0 };

// A function to check if a given cell (x, y)


// can be visited or not
bool isSafe(int mat[R][C], int visited[R][C],
int x, int y)
{
if (mat[x][y] == 0 || visited[x][y])
return false;

return true;
}

// A function to check if a given cell (x, y) is


// a valid cell or not
bool isValid(int x, int y)
{
if (x < R && y < C && x >= 0 && y >= 0)
return true;
return false;
}

// A function to mark all adjacent cells of


// landmines as unsafe. Landmines are shown with
// number 0
void markUnsafeCells(int mat[R][C])
{
for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
// if a landmines is found
if (mat[i][j] == 0)
{
// mark all adjacent cells
for (int k = 0; k < 4; k++)
if (isValid(i + rowNum[k], j + colNum[k]))
mat[i + rowNum[k]][j + colNum[k]] = -1;
}
}
}

// mark all found adjacent cells as unsafe


for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
if (mat[i][j] == -1)
mat[i][j] = 0;
}
}

// Uncomment below lines to print the path


/*for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
{
cout << std::setw(3) << mat[i][j];
}
cout << endl;
}*/
}

// Function to find shortest safe Route in the


// matrix with landmines
// mat[][] - binary input matrix with safe cells marked as 1
// visited[][] - store info about cells already visited in
// current route
// (i, j) are coordinates of the current cell
// min_dist --> stores minimum cost of shortest path so far
// dist --> stores current path cost
void findShortestPathUtil(int mat[R][C], int visited[R][C],
int i, int j, int &min_dist, int dist)
{
// if destination is reached
if (j == C-1)
{
// update shortest path found so far
min_dist = min(dist, min_dist);
return;
}

// if current path cost exceeds minimum so far


if (dist > min_dist)
return;

// include (i, j) in current path


visited[i][j] = 1;

// Recurse for all safe adjacent neighbours


for (int k = 0; k < 4; k++)
{
if (isValid(i + rowNum[k], j + colNum[k]) &&
isSafe(mat, visited, i + rowNum[k], j + colNum[k]))
{
findShortestPathUtil(mat, visited, i + rowNum[k],
j + colNum[k], min_dist, dist + 1);
}
}

// Backtrack
visited[i][j] = 0;
}

// A wrapper function over findshortestPathUtil()


void findShortestPath(int mat[R][C])
{
// stores minimum cost of shortest path so far
int min_dist = INT_MAX;

// create a boolean matrix to store info about


// cells already visited in current route
int visited[R][C];

// mark adjacent cells of landmines as unsafe


markUnsafeCells(mat);

// start from first column and take minimum


for (int i = 0; i < R; i++)
{
// if path is safe from current cell
if (mat[i][0] == 1)
{
// initialize visited to false
memset(visited, 0, sizeof visited);

// find shortest route from (i, 0) to any


// cell of last column (x, C - 1) where
// 0 <= x < R
findShortestPathUtil(mat, visited, i, 0,
min_dist, 0);

// if min distance is already found


if(min_dist == C - 1)
break;
}
}
// if destination can be reached
if (min_dist != INT_MAX)
cout << "Length of shortest safe route is "
<< min_dist;

else // if the destination is not reachable


cout << "Destination not reachable from "
<< "given source";
}

// Driver code
int main()
{
// input matrix with landmines shown with number 0
int mat[R][C] =
{
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 0, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 0, 1, 1, 1, 1 },
{ 1, 0, 1, 1, 1, 1, 1, 1, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 0, 1, 1, 1, 1, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 0, 1, 1, 1, 1, 1, 1 }
};
// find shortest path
findShortestPath(mat);

return 0;
}

Input-

O/P-
Length of shortest safe route is 13
Time complexity:

Expected time complexity is O(4(n^2)

Conclusion:

Through this project we have implemented the backtracking


algorithm to find the shortest and the safest path in a path with
landmines.We choose Backtracking Algorithm overs Breadth First
Search because using backtracking we can almost solve any
problems, due to its brute-force nature. It can be used to find all
the existing solutions if there exists for any problem. It is a step-
by-step representation of a solution to a given problem, which is
very easy to understand.
References:
https://www.geeksforgeeks.org
https://leetcode.com
https://www.youtube.com

You might also like