Skip to content

Commit 83b7de0

Browse files
committed
Day 7 Shortest path maze problem
1 parent 5e8cf6a commit 83b7de0

File tree

2 files changed

+144
-2
lines changed

2 files changed

+144
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 142 |
10-
| Current Streak | 6 days |
9+
| Total Problems | 143 |
10+
| Current Streak | 7 days |
1111
| Longest Streak | 91 ( August 17, 2015 - November 15, 2015 ) |
1212

1313
</center>
@@ -219,6 +219,7 @@ Include contains single header implementation of data structures and some algori
219219
| Count the number of prime numbers less than a non-negative number, n.| [countPrimes.cpp](leet_code_problems/countPrimes.cpp)|
220220
| Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example : for k = 3, n = 9 result would be [[1,2,6], [1,3,5], [2,3,4]], similarly for k = 3, n = 7, result would be [[1,2,4]]. | [combinationSum3.cpp](leet_code_problems/combinationSum3.cpp) |
221221
| Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. Follow up: Could you do it without any loop/recursion in O(1) runtime?| [addDigits.cpp](leet_code_problems/addDigits.cpp)|
222+
|Given a matrix with cell values 0 or 1. Find the length of the shortest path from (a1, b1) to (a2, b2), such that path can only be constructed through cells which have value 1 and you can only travel in 4 possible directions, i.e. left, right, up and down.|[shortest_path_maze.cpp](leet_code_problems/shortest_path_maze.cpp)|
222223

223224

224225

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Given a matrix with cell values 0 or 1. Find the length of the shortest path from (a1, b1)
3+
* to (a2, b2), such that:
4+
* Path can only be constructed through cells which have value 1.
5+
* You can only travel in 4 possible directions, i.e. left, right, up and down.
6+
*
7+
* For example: Given matrix:
8+
* Input:
9+
* mat[ROW][COL] = {{1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
10+
* {1, 0, 1, 0, 1, 1, 1, 0, 1, 1 },
11+
* {1, 1, 1, 0, 1, 1, 0, 1, 0, 1 },
12+
* {0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
13+
* {1, 1, 1, 0, 1, 1, 1, 0, 1, 0 },
14+
* {1, 0, 1, 1, 1, 1, 0, 1, 0, 0 },
15+
* {1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
16+
* {1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
17+
* {1, 1, 0, 0, 0, 0, 1, 0, 0, 1 }};
18+
* Source = {0, 0};
19+
* Destination = {3, 4};
20+
* Output:
21+
* Shortest Path is 11
22+
*
23+
* We will use breadth first search, we start from source cell (and distance 0)
24+
* and explore neighbors in all possible directions, and keep adding
25+
* distance from source node for each cell, so for a cell we reach,
26+
* its shortest path = shortest path of parent + 1. We stop when we have reached
27+
* destination. If we have explored all possible valid cells from source cell,
28+
* we return false, i.e. we do not have valid path from source.
29+
*/
30+
31+
32+
#include <iostream>
33+
#include <queue>
34+
#include <limits>
35+
36+
struct Point
37+
{
38+
int x;
39+
int y;
40+
};
41+
42+
struct Node
43+
{
44+
Point point;
45+
int distance;
46+
};
47+
48+
bool valid(const std::vector<std::vector<int>>& matrix, int x, int y)
49+
{
50+
return (x >= 0 && x < matrix[0].size() && y >= 0 && y < matrix.size());
51+
}
52+
53+
int shortestPath(const std::vector<std::vector<int>>& matrix,
54+
const Point& source,
55+
const Point& destination)
56+
{
57+
// An auxillary matrix to keep track of visited points
58+
// initially all cells are marked unvisited.
59+
//
60+
std::vector<std::vector<bool>> visited(
61+
matrix.size(),
62+
std::vector<bool>(matrix[0].size(), false));
63+
64+
// Possible moves from a cell.
65+
//
66+
std::vector<int> row = {-1, 0, 0, 1};
67+
std::vector<int> col = {0, -1, 1, 0};
68+
69+
std::queue<Node> nodeQueue;
70+
71+
// mark the source cell visited and push it to queue.
72+
//
73+
visited[source.x][source.y] = true;
74+
nodeQueue.push({source.x, source.y, 0});
75+
76+
while (!nodeQueue.empty())
77+
{
78+
// pop the front of the queue.
79+
Node current = nodeQueue.front();
80+
nodeQueue.pop();
81+
82+
Point point = current.point;
83+
84+
// if we have reached destination return distance.
85+
if (point.x == destination.x && point.y == destination.y)
86+
{
87+
return current.distance;
88+
}
89+
90+
for (int i = 0; i < 4; ++i)
91+
{
92+
int r = point.x + row[i];
93+
int c = point.y + col[i];
94+
95+
if (valid(matrix, r, c) &&
96+
matrix[r][c] && !visited[r][c])
97+
{
98+
visited[r][c] = true;
99+
Node adjNode = {r, c, current.distance + 1};
100+
nodeQueue.push(adjNode);
101+
}
102+
}
103+
}
104+
return std::numeric_limits<int>::max();
105+
}
106+
107+
int main()
108+
{
109+
const std::vector<std::vector<int>> matrix =
110+
{{ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
111+
{ 1, 0, 1, 0, 1, 1, 1, 0, 1, 1 },
112+
{ 1, 1, 1, 0, 1, 1, 0, 1, 0, 1 },
113+
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 },
114+
{ 1, 1, 1, 0, 1, 1, 1, 0, 1, 0 },
115+
{ 1, 0, 1, 1, 1, 1, 0, 1, 0, 0 },
116+
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
117+
{ 1, 0, 1, 1, 1, 1, 0, 1, 1, 1 },
118+
{ 1, 1, 0, 0, 0, 0, 1, 0, 0, 1 }};
119+
120+
Point source = {0, 0};
121+
Point destination = {3, 4};
122+
int distance = shortestPath(matrix, source, destination);
123+
if (distance !=
124+
std::numeric_limits<int>::max())
125+
{
126+
std::cout << "The distance between ("
127+
<< source.x << ", " << source.y
128+
<< ") and destination (" << destination.x
129+
<< ", " << destination.y << ") is "
130+
<< distance << std::endl;
131+
}
132+
else
133+
{
134+
std::cout << "The path does not exist between ("
135+
<< source.x << ", " << source.y
136+
<< ") and destination (" << destination.x
137+
<< ", " << destination.y << ") is "
138+
<< distance << std::endl;
139+
}
140+
return 0;
141+
}

0 commit comments

Comments
 (0)