I have problem in unweighted undirected graph the question ask if any path between two point in binary matrix, the user insert the matrix and the cell in matrix to test
bool BFS(vector<vector<int>>mat, Point src, Point dest,int n,int m)
{
if (!mat[src.x][src.y] || !mat[dest.x][dest.y])
return 0;
bool visited[n][m];
memset(visited, false, sizeof visited);
visited[src.x][src.y] = true;
queue<queueNode> q;
queueNode s = {src};
q.push(s);
while (!q.empty())
{
queueNode curr = q.front();
Point pt = curr.pt;
if (pt.x == dest.x && pt.y == dest.y)
return 1;
q.pop();
for (int i = 0; i < 8; i++)
{
int row = pt.x + rowNum[i];
int col = pt.y + colNum[i];
if (isValid(row, col,n,m) && mat[row][col] &&
!visited[row][col])
{
visited[row][col] = true;
queueNode Adjcell = {row, col};
q.push(Adjcell);
}
}
}
return 0;
}
In hacker rank some test not work properly because timeout. Any one can help please?