-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1091-Shortest_Path_in_Binary_Matrix.cpp
More file actions
121 lines (110 loc) · 3.52 KB
/
1091-Shortest_Path_in_Binary_Matrix.cpp
File metadata and controls
121 lines (110 loc) · 3.52 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
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
111
112
113
114
115
116
117
118
119
120
121
/*******************************************************************************
* 1091-Shortest_Path_in_Binary_Matrix.cpp
* Billy.Ljm
* 01 June 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/shortest-path-in-binary-matrix/
*
* Given an n x n binary matrix grid, return the length of the shortest clear
* path in the matrix. If there is no clear path, return -1.
*
* A clear path in a binary matrix is a path from the top-left cell
* (i.e., (0, 0)) to the bottom-right cell (i.e., (n - 1, n - 1)) such that:
* - All the visited cells of the path are 0.
* - All the adjacent cells of the path are 8-directionally connected (i.e.,
* they are different and they share an edge or a corner).
*
* The length of a clear path is the number of visited cells of this path.
*
* ===========
* My Approach
* ===========
* We'll use breadth-first search to explore paths starting from the top-left
* cell until we reach the bottom-right cell.
*
* This has a time complexity of O(n^2) and space complexity of O(n^2), where
* n is the width of the square matrix.
******************************************************************************/
#include <iostream>
#include <vector>
#include <queue>
/**
* Solution
*/
class Solution {
public:
int shortestPathBinaryMatrix(std::vector<std::vector<int>>& grid) {
// visited cells will be marked by grid[i][j] > 1
std::queue<std::pair<int, int>> qq; // queue of cells to visit next
std::pair<int, int> crawler(0, 0); // cell being visited
int nsteps = 1; // number of steps
// enqueue first cell
if (grid[0][0] == 0) qq.push(std::pair<int, int>(0, 0));
grid[0][0] = 2; // mark as visited
// breadth-first search
while (!qq.empty()) {
// pop only the current nsteps
int qsize = qq.size();
for (int i = 0; i < qsize; i++) {
// visit next cell
crawler = qq.front();
qq.pop();
// if destination reached, return
if (crawler == std::pair<int, int>(grid[0].size() - 1, grid.size() - 1)) return nsteps;
// else queue neighbours
for (int nx : {crawler.first - 1, crawler.first, crawler.first + 1}) {
for (int ny : {crawler.second - 1, crawler.second, crawler.second + 1}) {
// check if new indices are valid
if (nx == crawler.first && ny == crawler.second) continue;
if (nx < 0 || nx >= grid[0].size()) continue;
if (ny < 0 || ny >= grid[0].size()) continue;
if (grid[nx][ny] != 0) continue;
if (grid[nx][ny] > 1) continue; // means visited
// queue neighbours
qq.push(std::pair<int, int>(nx, ny));
grid[nx][ny] = 2; //mark as visited
}
}
}
// increment nsteps
nsteps++;
}
// no path found
return -1;
}
};
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (int i = 0; i < v.size(); i++) {
os << v[i] << ",";
}
os << "\b]";
return os;
}
/**
* Test cases
*/
int main(void) {
Solution sol;
std::vector<std::vector<int>> grid;
// test case 1
grid = { {0, 1}, {1, 0} };
std::cout << "shortestPathBinaryMatrix(" << grid << ") = ";
std::cout << sol.shortestPathBinaryMatrix(grid) << std::endl;
// test case 2
grid = { {0, 0, 0}, {1, 1, 0}, {1, 1, 0} };
std::cout << "shortestPathBinaryMatrix(" << grid << ") = ";
std::cout << sol.shortestPathBinaryMatrix(grid) << std::endl;
// test case 3
grid = { {1, 0, 0},{1, 1, 0},{1, 1, 0} };
std::cout << "shortestPathBinaryMatrix(" << grid << ") = ";
std::cout << sol.shortestPathBinaryMatrix(grid) << std::endl;
return 0;
}