-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shortest_Path_In_Maze.cpp
80 lines (60 loc) · 1.34 KB
/
Shortest_Path_In_Maze.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <bits/stdc++.h>
using namespace std;
struct Point {
int x,y;
};
struct Node {
Point p;
int dist;
};
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
int row, col;
bool mat[1000][1000];
int BFS(Point src, Point dest) {
//What if the source or destn itself is blocked ?
if(!mat[src.x][src.y] || !mat[dest.x][dest.y])
return INT_MAX;
bool visited[row][col];
memset(visited, 0, sizeof visited);
queue<Node>q;
Node s = {src, 0};
q.push(s);
while(!q.empty()) {
Node curr = q.front();
Point currPoint = curr.p;
//We have reached the destn.
if(currPoint.x == dest.x and currPoint.y == dest.y)
return curr.dist;
q.pop();
for(int i=0; i<4; i++) {
int x = currPoint.x + dx[i];
int y = currPoint.y + dy[i];
if(x>=0 && x<row && y>=0 && y<col) {
visited[x][y] = true;
Node adj = { {x,y}, curr.dist+1 };
q.push(adj);
}
}
}
// iN CASE destination not reached.
return INT_MAX;
}
int main() {
cout << "Enter the dimensions of the grid : ";
cin >> row >> col;
Point src, dest;
for (int i=0; i<row; i++)
for(int j=0; j<col; j++)
cin >> mat[i][j];
//cout << "Enter source and destination :";
//cin >> src >> dest;
src = {0,0};
dest = {3,4};
int dist = BFS(src, dest);
if(dist!=INT_MAX)
cout<<"Shortest Path doesn't exist\n";
else
cout<<"Shortest path is " << dist << endl;
return 0;
}