I am trying to create a BFS for a maze that will stop when a certain point is reached. While testing it I'm running into a Segmentation fault (core dumped) error. I am trying to modify the code I found on this site. The main differences between what I am trying to do and the code in that site are that I do not need to output the distance traveled so and I am also supposed to output the order in the queue the vertices are in the inside of the matrix. For example, the output should be the following:
What the output of the program should be
The Hashes represent vertices that do not get added to the queue I am not too concerned with them as of now due to this error that is stopping me from going forward.
Here is my code thus far:
#include <queue>
#include <iostream>
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define ROW 8
#define COL 11
struct Point{
int x;
int y;
};
bool isValid(int x, int y)
{
// return true if row number and column number
// is in range
return (x >= 0) && (x< ROW) &&
(y >= 0) && (y < COL);
}
int rowNum[] = {-1, 0, 0, 1};
int colNum[] = {0, -1, 1, 0};
int BFS(int mat[][COL], Point src, Point dest)
{
int order = 1;
// Mark the source cell as visited
bool visited[ROW][COL];
memset(visited, false, sizeof visited);
visited[src.x][src.y] = true;
// Create a queue for BFS
queue <Point> vertices;
// Enqueue source cell
vertices.push(src);
// Do a BFS starting from source cell
while(visited[dest.x][dest.y] != true){
Point current = vertices.front();
vertices.pop();
for (int i = 0; i < 4; i++)
{
int row = current.x + rowNum[i];
int col = current.y + colNum[i];
// if adjacent cell is valid, has path and
// not visited yet, enqueue it.
if (isValid(row, col) == true && (mat[row][col]) &&
!visited[row][col])
{
cout << "Hi" << endl;
// mark cell as visited and enqueue it
visited[row][col] = true;
mat[row][col] = order;
order++;
vertices.push(current);
cout << vertices.front().x;
cout << vertices.front().y;
}
else {
}
}
}
return -1;
}
int main()
{
int mat[ROW][COL] =
{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};
Point source = {0, 0};
Point dest = {3, 4};
BFS(mat, source, dest);
for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COL; ++j)
{
std::cout << mat[i][j] << ' ';
}
std::cout << std::endl;
}
return 0;
}
I did some troubleshooting beforehand and have found that the error most likely is caused by this spot right here:
if (isValid(row, col) == true && (mat[row][col]) &&
!visited[row][col])
{
// mark cell as visited and enqueue it
visited[row][col] = true;
mat[row][col] = order;
order++;
vertices.push(current);
cout << vertices.front().x;
cout << vertices.front().y;
}
I assume that because I set up a bunch of output messages (you may notice the cout "Hi's") to occur at certain points in the cause to find the source of the error and that is where it led me.
Any help is appreciated and just to be clear i am trying to find out why i am getting the segmentation fault error.
Thank you.