Breadth First Search on Matrix in C++

C++Server Side ProgrammingProgramming

In a given matrix, there are four objects to analyze the element position: left, right, bottom and top.

Breadth First Search is nothing but finding the shortest distance between the two elements of a given 2-D Matrix. Thus in each cell, there are four operations we can perform which can be expressed in four numerals such as,

• '2' describes that the cell in the matrix is Source.
• '3' describes that the cell in the matrix is Destination.
• '1' describes that the cell can be moved further in a direction.
• '0' describes that the cell in the matrix can not be moved in any direction.

On the basis of adobe justification, we can perform a Breadth First Search Operation on a given Matrix.

Approach to Solve this Problem

The algorithm to traverse the whole matrix and find the minimum or shortest distance between any cell using BFS is as follows:

• First take the input row and column.
• Initialize a matrix with the given row and column.
• An integer function shortestDist(int row, int col, int mat[][col]) takes the row, column and matrix as the input and returns the shortest distance between the elements of the matrix.
• Initialize the variable source and destination to find out the source as well as the destination element.
• If the element is '3', then mark it as the destination and if the element is '2', then mark it as the source element.
• Now initialize the queue data structure to implement Breadth First Search on the given matrix.
• Insert the row and column of the matrix in the queue as pairs. Now move in the cell and find out if it is a destination cell or not. If the destination cell is having a distance minimum or less than the current cell, then update the distance.
• Again move to another direction to find out the minimum distance of the cell from the current cell.
• Return the minimum distance as the output.

Example

#include<bits/stdc++.h>
using namespace std;
int findDistance(int row, int col, int mat[][5]) {
int source_i, source_j, destination_i, destination_j;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (mat[i][j] == 2) {
source_i = i;
source_j = j;
}
if (mat[i][j] == 3) {
destination_i = i;
destination_j = j;
}
}
}
int dist[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
dist[i][j] = INT_MAX;
}
// initialise queue to start BFS on matrix
queue < pair < int, int >> q;
q.push(make_pair(source_i, source_j));
dist[source_i][source_j] = 0;

// modified BFS by add constraint checks
while (!q.empty()) {
// storing the x co-ordinate or row information of cell
int x = q.front().first;
// storing the y co-ordinate or column information of cell
int y = q.front().second;
// Remove the cell from queue
q.pop();

// If move towards left is allowed or it is the destnation cell
if (y - 1 >= 0 && (mat[x][y - 1] == 1 || mat[x][y - 1] == 3)) {
// if distance to reach the cell to the left is less than the computed previous path distance, update it
if (dist[x][y] + 1 < dist[x][y - 1]) {
dist[x][y - 1] = dist[x][y] + 1;
q.push(mkp(x, y - 1));
}
}
// If move towards right is allowed or it is the destination cell
if (y + 1 < col && (mat[x][y + 1] == 1 || mat[x][y + 1] == 3)) {
// if distance to reach the cell to the right is less than the computed previous path distance, update it
if (dist[x][y] + 1 < dist[x][y + 1]) {
dist[x][y + 1] = dist[x][y] + 1;
q.push(mkp(x, y + 1));
}
}
// If upward direction is allowed
if (x - 1 >= 0 && (mat[x - 1][y] == 1 || mat[x - 1][y] == 3)) {
if (dist[x][y] + 1 < dist[x - 1][y]) {
dist[x - 1][y] = dist[x][y] + 1;
q.push(mkp(x - 1, y));
}
}

// If downward direction allowed
if (x + 1 < row && (mat[x + 1][y] == 1 || mat[x + 1][y] == 3)) {
// if distance to reach the cell to the down is less than the computed previous path distance, update it
if (dist[x][y] + 1 < dist[x + 1][y]) {
dist[x + 1][y] = dist[x][y] + 1;
q.push(mkp(x + 1, y));
}
}
}
return dist[destination_i][destination_j];
}

int main() {
// initialising number of rows and columns
int row = 5;
int col = 5;
// initialising matrix
int mat[][5] = {
{1, 0, 0, 2, 1},
{1, 0, 1, 1, 1},
{0, 1, 1, 2, 0},
{3, 1, 0, 0, 1},
{1, 1, 0, 0, 1}
};
int answer = findDistance(row, col, mat);
// When source and destination are unreachable
cout << "No Path Found" << endl;
else {
cout << "The Shortest Distance between Source and Destination is:" << endl;
}
return 0;
}

Output

The Shortest Distance between Source and Destination is:4
Published on 23-Feb-2021 04:46:33