-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0934-Shortest_Bridge.cpp
More file actions
182 lines (165 loc) · 4.68 KB
/
0934-Shortest_Bridge.cpp
File metadata and controls
182 lines (165 loc) · 4.68 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*******************************************************************************
* 0934-Shortest_Bridge.cpp
* Billy.Ljm
* 21 May 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/shortest-bridge/
*
* You are given an n x n binary matrix grid where 1 represents land and 0
* represents water.
*
* An island is a 4-directionally connected group of 1's not connected to any
* other 1's. There are exactly two islands in grid.
*
* You may change 0's to 1's to connect the two islands to form one island.
*
* Return the smallest number of 0's you must flip to connect the two islands.
*
* ===========
* My Approach
* ===========
* We'll use breadth-first search to find the shortest bridge. We start from one
* of the islands, extend 1 cell past its perimeter, then 2 and so on until we
* reach the other island. To execute the breadth-first search, we'll use a
* priority queue to iterate through cells that progressively further from the
* first island.
*
* This has a time complexity of O(n^2) and space complexity of O(1), where n
* is the width and breadth of the grid to search.
******************************************************************************/
#include <iostream>
#include <vector>
#include <queue>
/**
* Solution
*/
class Solution {
private:
/**
* Recrusively explore a land mass and add coasts to a queue
*
* @param i explore from grid[i][j]
* @param j explore from grid[i][j]
* @param grid map of the area, where 1 represents land and 0 water
* @param qq queue to insert coasts into, by flattened queue index
*/
void findLand(int i, int j, std::vector<std::vector<int>>& grid,
std::queue<int>& qq) {
size_t n = grid[0].size(); // size of grid
grid[i][j] = 2; // mark grid as visited
// check if coast
if ((i + 1 < n and grid[i + 1][j] == 0) or
(i - 1 >= 0 and grid[i - 1][j] == 0) or
(j + 1 < n and grid[i][j + 1] == 0) or
(j - 1 >= 0 and grid[i][j - 1] == 0)) {
qq.push(i * n + j);
}
// visit univisted neighbours
if (i + 1 < n and grid[i + 1][j] == 1) {
findLand(i + 1, j, grid, qq);
}
if (i - 1 >= 0 and grid[i - 1][j] == 1) {
findLand(i - 1, j, grid, qq);
}
if (j + 1 < n and grid[i][j + 1] == 1) {
findLand(i, j + 1, grid, qq);
}
if (j - 1 >= 0 and grid[i][j - 1] == 1) {
findLand(i, j - 1, grid, qq);
}
}
public:
/**
* Finds the length of the shortest bridge between two islands in a grid map
*
* @param grid map of the area, where 1 represents land and 0 water
*
* @return length of the shortest bridge between two islands
*/
int shortestBridge(std::vector<std::vector<int>>& grid) {
size_t n = grid[0].size();
std::queue<int> qq; // BFS queue
int elem; // qq element
// find first land mass, and mark it as 2 in grid
for (int i = 0; i < grid.size(); i++) {
for (int j = 0; j < grid[0].size(); j++) {
if (grid[i][j] == 1) {
findLand(i, j, grid, qq);
break;
}
}
if (not qq.empty()) break; // break out of both loops
}
// breadth-first search
while (not qq.empty()) {
// get top element
elem = qq.front();
int i = elem / n;
int j = elem % n;
qq.pop();
// explore i+1, i-1
for (int ii : {i + 1, i - 1}) {
if (ii < 0 or ii >= n) {
; // out of bounds, ignore
}
else if (grid[ii][j] == 1) { // other island found, return
return grid[i][j] - 2;
}
else if (grid[ii][j] == 0) { // else increment distance
grid[ii][j] = grid[i][j] + 1;
qq.push(ii * n + j);
}
}
// explore j+1, j-1
for (int jj : {j + 1, j - 1}) {
if (jj < 0 or jj >= n) {
; // out of bounds, ignore
}
else if (grid[i][jj] == 1) { // other island found, return
return grid[i][j] - 2;
}
else if (grid[i][jj] == 0) { // else increment distance
grid[i][jj] = grid[i][j] + 1;
qq.push(i * n + jj);
}
}
}
// can't find other island
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 << "shortestBridge(" << grid << ") = ";
std::cout << sol.shortestBridge(grid) << std::endl;
// test case 2
grid = { {0, 1, 0}, {0, 0, 0}, {0, 0, 1} };
std::cout << "shortestBridge(" << grid << ") = ";
std::cout << sol.shortestBridge(grid) << std::endl;
// test case 3
grid = { {1,1,1,1,1}, {1,0,0,0,1}, {1,0,1,0,1}, {1,0,0,0,1}, {1,1,1,1,1} };
std::cout << "shortestBridge(" << grid << ") = ";
std::cout << sol.shortestBridge(grid) << std::endl;
return 0;
}