Shortest Distance in a Maze
C++
// C++ program to find the shortest path between
// a given source cell to a destination cell.
#include <bits/stdc++.h>
using namespace std;
#define ROW 9
#define COL 10
//To store matrix cell cordinates
struct Point
{
int x;
int y;
};
// A Data Structure for queue used in BFS
struct queueNode
{
Point pt; // The cordinates of a cell
int dist; // cell's distance of from the source
};
// check whether given cell (row, col) is a valid
// cell or not.
bool isValid(int row, int col)
{
// return true if row number and column number
// is in range
return (row >= 0) && (row < ROW) &&
(col >= 0) && (col < COL);
}
// 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};
// function to find the shortest path between
// a given source cell to a destination cell.
int BFS(int mat[][COL], Point src, Point dest)
{
// check source and destination cell
// of the matrix have value 1
if (!mat[src.x][src.y] || !mat[dest.x][dest.y])
return -1;
bool visited[ROW][COL];
memset(visited, false, sizeof visited);
// Mark the source cell as visited
visited[src.x][src.y] = true;
// Create a queue for BFS
queue<queueNode> q;
// Distance of source cell is 0
queueNode s = {src, 0};
q.push(s); // Enqueue source cell
// Do a BFS starting from source cell
while (!q.empty())
{
queueNode curr = q.front();
Point pt = curr.pt;
// If we have reached the destination cell,
// we are done
if (pt.x == dest.x && pt.y == dest.y)
return curr.dist;
// Otherwise dequeue the front cell in the queue
// and enqueue its adjacent cells
q.pop();
for (int i = 0; i < 4; i++)
{
int row = pt.x + rowNum[i];
int col = pt.y + colNum[i];
// if adjacent cell is valid, has path and
// not visited yet, enqueue it.
if (isValid(row, col) && mat[row][col] &&
!visited[row][col])
{
// mark cell as visited and enqueue it
visited[row][col] = true;
queueNode Adjcell = { {row, col},
curr.dist + 1 };
q.push(Adjcell);
}
}
}
// Return -1 if destination cannot be reached
return -1;
}
// Driver program to test above function
int main()
{
int mat[ROW][COL] =
{
{ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
{ 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 },
{ 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 0, 1, 1, 1, 0, 1, 0 },
{ 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
{ 1, 1, 0, 0, 0, 0, 1, 0, 0, 1 }
};
Point source = {0, 0};
Point dest = {3, 4};
int dist = BFS(mat, source, dest);
if (dist != INT_MAX)
cout << "Shortest Path is " << dist ;
else
cout << "Shortest Path doesn't exist";
return 0;
}
Also in C++:
- Title
- vector in c++ class
- Category
- C++
- Title
- how to sort a vector in c++
- Category
- C++
- Title
- c++ overload operator
- Category
- C++
- Title
- c++ formatting
- Category
- C++
- Title
- compile c++ linux
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- euler's totient function c++
- Category
- C++
- Title
- to_string c++
- Category
- C++
- Title
- c++ do you not inherit constructor
- Category
- C++
- Title
- chess perft 5
- Category
- C++
- Title
- insertion sort in c++ program
- Category
- C++
- Title
- c++ return multiple values
- Category
- C++
- Title
- how to find hcf in c++
- Category
- C++
- Title
- double max value c++
- Category
- C++
- Title
- new c++
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- simple timer arduino blynk library error
- Category
- C++
- Title
- initialise 2d vector in c++
- Category
- C++
- Title
- dfenwick tree code c++
- Category
- C++
- Title
- c++ split at character
- Category
- C++
- Title
- Runtime Error: Runtime ErrorBad memory access (SIGBUS)
- Category
- C++
- Title
- c++ overloaded equality check operator
- Category
- C++
- Title
- how to read and write in a file c++
- Category
- C++
- Title
- SFML window
- Category
- C++
- Title
- vector initialization c++
- Category
- C++
- Title
- c++ stream string into fiel
- Category
- C++
- Title
- how to use wasd c++
- Category
- C++
- Title
- how to use winmain function
- Category
- C++
- Title
- binary serach in c++
- Category
- C++
- Title
- c++ char print fixed
- Category
- C++
- Title
- including cpp header file in c++
- Category
- C++
- Title
- c++ remove space from string
- Category
- C++
- Title
- how to get os name in c++
- Category
- C++
- Title
- double to int c++
- Category
- C++
- Title
- how initilaize deffult value to c++ class
- Category
- C++
- Title
- making random numbers in c++
- Category
- C++
- Title
- prefix sum array
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- how to declare a function in c++
- Category
- C++
- Title
- c++ scanf
- Category
- C++
- Title
- c++ declare char
- Category
- C++
- Title
- new in c++
- Category
- C++
- Title
- how to convert number to string
- Category
- C++
- Title
- how to include seld declared header file in c++
- Category
- C++
- Title
- Runtime Error: Runtime ErrorAbort signal from abort(3) (SIGABRT)
- Category
- C++
- Title
- c++ how to add something at the start of a vector
- Category
- C++
- Title
- for loop
- Category
- C++
- Title
- primitive and non primitive data types in c++
- Category
- C++
- Title
- initialize vector of vector c++
- Category
- C++
- Title
- advanced c++ topics
- Category
- C++
- Title
- index string c++
- Category
- C++
- Title
- what are the different ways to traverse a binary tree
- Category
- C++
- Title
- create new file c++
- Category
- C++
- Title
- Operator overloading in C++ Programming
- Category
- C++
- Title
- length of 2d array c++
- Category
- C++
- Title
- opencv compile c++
- Category
- C++
- Title
- c++ method name
- Category
- C++
- Title
- c++ crash windows
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- heap sort
- Category
- C++
- Title
- knapsack
- Category
- C++
- Title
- c++ unittest in ros
- Category
- C++
- Title
- do while loop c++
- Category
- C++
- Title
- c++ modulo make it give only positive numbers
- Category
- C++
- Title
- how to sort in descending order c++
- Category
- C++
- Title
- printf c++
- Category
- C++
- Title
- C++ and endl
- Category
- C++
- Title
- c++ function return array
- Category
- C++
- Title
- c++ string to vector int
- Category
- C++
- Title
- how to convert qt string to string
- Category
- C++
- Title
- pair c++
- Category
- C++
- Title
- how to remove maximum number of characters in c++ cin,ignore
- Category
- C++
- Title
- assegnare valori in c++
- Category
- C++
- Title
- map arduino
- Category
- C++
- Title
- get line C++
- Category
- C++
- Title
- change int to string cpp
- Category
- C++
- Title
- max three values c++
- Category
- C++
- Title
- how to define a while statement in c++
- Category
- C++
- Title
- initialize 2d array c++
- Category
- C++
- Title
- never gonna give you up lyrics
- Category
- C++
- Title
- 1d fixed length arrays c++
- Category
- C++
- Title
- programa para saber si un numero es primo
- Category
- C++
- Title
- differentialble programming
- Category
- C++
- Title
- convert integer to string c++
- Category
- C++
- Title
- how to make string get spaces c++
- Category
- C++
- Title
- fast io c++ geeksforgeeks
- Category
- C++
- Title
- substr in c++
- Category
- C++
- Title
- best fit algorithm
- Category
- C++
- Title
- create a bitset of 1024 bits,
- Category
- C++
- Title
- cpp loop through object
- Category
- C++
- Title
- min heap declaration in c++ stl
- Category
- C++
- Title
- passing array to function c++ pointer
- Category
- C++
- Title
- digitalwrite C++
- Category
- C++
- Title
- how to get a letter from the user c++ string
- Category
- C++
- Title
- c++ string^ to char*
- Category
- C++
- Title
- how to declare a vector in c++
- Category
- C++
- Title
- how to print 5 precision float in c++
- Category
- C++
- Title
- c++ program to input and print text using Dynamic Memory Allocation.loop
- Category
- C++
- Title
- factorion
- Category
- C++
- Title
- how to make loop in c++
- Category
- C++