-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathLee_Algorithm.cpp
110 lines (96 loc) · 3.59 KB
/
Lee_Algorithm.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/******************************************************************************
Author: @Suvraneel Bhuin
* Lee's Algorithm *
The objective is to find the length of the shortest path in a maze from a given source to a given destination,
Given the maze in form of a binary rectangular matrix.
-> Modify as required:
Definitions of ROW, COL
Input `maze`, `src`, `dest` in main function
*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;
// define no. of rows & columns
#define ROW 10
#define COL 10
// Node Structure
// consists of x, y coordinates and the distance
struct Node{
int x, y, distance;
};
// movement in 4 directions
int direction[4][2] = {{1, 0}, {0, 1}, { -1, 0}, {0, -1}};
// check whether a valid position (i.e, within grid & is unvisited).
bool check(int mat[][COL], bool visited[][COL], int row, int col){
return mat[row][col] && (row > -1) && (row <= ROW-1) && (col > -1) && (col <= COL-1) && !visited[row][col];
}
// shortest possible route
int bfs(int mat[][COL], int startX, int startY, int endX, int endY){
// Queue to keep track of all the cells accounted for in the path so far
queue<Node> Q;
bool visited[ROW][COL];
// mark all boxes unvisited initially
// memset: sets memory pointed to by first `sizeof(visited)` characters of array `visited` to 0
memset(visited, 0, sizeof(visited));
// set the starting cell as visited and enqueue cell
visited[startX][startY] = 1;
Q.push({startX, startY, 0});
// stores shortest path as of yet
// initialised to INT_MAX so that any value can replace it
int ans = INT_MAX;
while (!Q.empty()){
Node cell = Q.front();
Q.pop();
int i = cell.x, j = cell.y, distance = cell.distance;
// if the destination is found, update minimum distance & break while loop
if (i == endX && j == endY){
// tracks minimum distance on the fly
ans = distance;
return ans;
}
// check for all possible movements
for (int k = 0; k < 4; k++){
// check if movement is valid (ie, within grid & unvisited)
if (check(mat, visited, i + direction[k][0], j + direction[k][1])){
// mark next cell as visited and enqueue it
visited[i + direction[k][0]][j + direction[k][1]] = 1;
Q.push({ i + direction[k][0], j + direction[k][1], distance + 1 });
}
}
}
// return -1 incase destination cannot be reached (i.e, path blocked)
return -1;
}
// driver code
int main()
{
// input maze here
int maze[ROW][COL] = {
{ 1, 1, 1, 1, 1, 0, 0, 1, 1, 1 },
{ 0, 1, 1, 1, 1, 1, 0, 1, 0, 1 },
{ 0, 0, 1, 0, 1, 1, 1, 0, 0, 1 },
{ 1, 0, 1, 0, 1, 0, 1, 1, 0, 1 },
{ 0, 0, 0, 1, 0, 0, 0, 1, 0, 1 },
{ 0, 0, 1, 1, 1, 0, 0, 1, 1, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 1, 0, 1 },
{ 0, 1, 1, 0, 1, 1, 1, 1, 0, 0 },
{ 0, 1, 1, 1, 1, 0, 0, 1, 1, 1 },
{ 0, 0, 1, 0, 0, 1, 1, 0, 0, 1 },
};
// input source cell & destination cell here
int src[2] = { 0, 0};
int dest[2] = { 8, 9};
// Find the shortest path from source to destination by calling bfs
int res = bfs(maze, src[0], src[1], dest[0], dest[1]);
//ouput
if (res != -1)
cout<< "Length of the Shortest Path is => "<< res << " units." ;
else
cout<< "Path from source to destination doesn't exist !";
return 0;
}
/*
Output:
Length of the Shortest Path is => 17 units.
Time Complexity = O(ROW*COL)
Space Complexity = O(ROW*COL)
*/