forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6fe2ab7
commit ed5df87
Showing
2 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include <iostream> | ||
#include <queue> | ||
using namespace std; | ||
|
||
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }(); | ||
class Solution | ||
{ | ||
private: | ||
vector<pair<int, int>> edges; | ||
void getEdges(vector<vector<int>>& A, int x, int y) | ||
{ | ||
if (x < 0 || y < 0 || x >= A.size() || | ||
y >= A[0].size() || A[x][y] != 1) return; | ||
if ((x > 0 && A[x-1][y] == 0) || (y+1 < A[0].size() && A[x][y+1] == 0) || | ||
(x+1 < A.size() && A[x+1][y] == 0) || (y > 0 && A[x][y-1] == 0)) | ||
edges.push_back(make_pair(x, y)); | ||
A[x][y] = 2; | ||
getEdges(A, x-1, y); | ||
getEdges(A, x, y+1); | ||
getEdges(A, x+1, y); | ||
getEdges(A, x, y-1); | ||
} | ||
bool valid(vector<vector<int>>& A, vector<pair<int, int>>& next, int x, int y) | ||
{ | ||
if (x < 0 || y < 0 || x >= A.size() || | ||
y >= A[0].size() || A[x][y] == 2) | ||
return false; | ||
if (A[x][y] == 1) return true; | ||
A[x][y] = 2; | ||
next.push_back(make_pair(x, y)); | ||
return false; | ||
} | ||
public: | ||
int shortestBridge(vector<vector<int>>& A) | ||
{ | ||
for (int i=0; i<A.size(); ++i) | ||
{ | ||
for (int j=0; j<A[i].size(); ++j) | ||
{ | ||
if (A[i][j]) | ||
{ | ||
getEdges(A, i, j); | ||
goto forward; | ||
} | ||
} | ||
} | ||
forward: | ||
int step = 0; | ||
while (!edges.empty()) | ||
{ | ||
step += 1; | ||
vector<pair<int, int>> next; | ||
for (int i=0; i<edges.size(); ++i) | ||
{ | ||
int x = edges[i].first; | ||
int y = edges[i].second; | ||
if (valid(A, next, x-1, y) || valid(A, next, x, y+1) || | ||
valid(A, next, x+1, y) || valid(A, next, x, y-1)) | ||
goto end; | ||
} | ||
edges = next; | ||
} | ||
end: | ||
return step - 1; | ||
} | ||
}; | ||
int main() | ||
{ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
class Solution: | ||
def shortestBridge(self, A): | ||
""" | ||
:type A: List[List[int]] | ||
:rtype: int | ||
""" | ||
x, y, r, c = 0, 0, len(A), len(A[0]) | ||
for i in range(r): | ||
for j in range(c): | ||
if A[i][j] == 1: | ||
x, y = i, j | ||
break | ||
else: | ||
continue | ||
break | ||
|
||
d = [(-1, 0), (0, 1), (1, 0), (0, -1)] | ||
visited = [[0]*c for _ in range(r)] | ||
islandA, result = list(), 0 | ||
def _floodfill(i, j): | ||
if 0 <= i < r and 0 <= j < c and not visited[i][j] and A[i][j] == 1: | ||
visited[i][j] = 1 | ||
islandA.append((i, j)) | ||
for k in range(4): | ||
_floodfill(i + d[k][0], j + d[k][1]) | ||
|
||
_floodfill(x, y) | ||
while islandA: | ||
stack = list() | ||
for i, j in islandA: | ||
if i - 1 >= 0 and not visited[i-1][j]: | ||
if A[i-1][j] == 1: | ||
return result | ||
else: | ||
stack.append((i-1, j)) | ||
visited[i-1][j] = 1 | ||
if i + 1 < r and not visited[i+1][j]: | ||
if A[i+1][j] == 1: | ||
return result | ||
else: | ||
stack.append((i+1, j)) | ||
visited[i+1][j] = 1 | ||
if j - 1 >= 0 and not visited[i][j-1]: | ||
if A[i][j-1] == 1: | ||
return result | ||
else: | ||
stack.append((i, j-1)) | ||
visited[i][j-1] = 1 | ||
if j + 1 < c and not visited[i][j+1]: | ||
if A[i][j+1] == 1: | ||
return result | ||
else: | ||
stack.append((i, j+1)) | ||
visited[i][j+1] = 1 | ||
islandA = stack | ||
result += 1 | ||
|
||
return result | ||
|
||
|
||
if __name__ == "__main__": | ||
A = [[0,1,0],[0,0,0],[0,0,1]] | ||
print(Solution().shortestBridge(A)) |