forked from pritomshad/MGS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolver.cpp
More file actions
59 lines (53 loc) · 1.42 KB
/
Copy pathSolver.cpp
File metadata and controls
59 lines (53 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "Globals.hpp"
#include "Solver.hpp"
#include <vector>
bool BFS(std::vector< std::vector<int> > &adj, int src, int dest, int mazeWidth, int mazeHeight, std::vector<int> &shortestPath)
{
int v = mazeWidth * mazeHeight;
bool bfs_visited[v];
std::list<int> queue;
int prev[v];
int dist[v];
bool pathFound = false;
for (int i = 0; i < v; i++)
{
bfs_visited[i] = false;
prev[i] = -1;
dist[i] = INT32_MAX;
}
bfs_visited[src] = true;
dist[src] = 0;
queue.push_back(src);
while (!queue.empty())
{
int u = queue.front();
queue.pop_front();
for (int i = 0; i < adj[u].size(); i++) {
if (bfs_visited[adj[u][i]] == false) {
bfs_visited[adj[u][i]] = true;
dist[adj[u][i]] = dist[u] + 1;
prev[adj[u][i]] = u;
queue.push_back(adj[u][i]);
// We stop BFS when we find
// destination.
if (adj[u][i] == dest)
{
pathFound = true;
break;
}
}
}
if (pathFound)
break;
}
if (pathFound)
{
int crawl = dest;
shortestPath.push_back(crawl);
while (prev[crawl] != -1) {
shortestPath.push_back(prev[crawl]);
crawl = prev[crawl];
}
}
return pathFound;
}