Skip to content

Commit

Permalink
add 934
Browse files Browse the repository at this point in the history
  • Loading branch information
luliyucoordinate committed Nov 21, 2018
1 parent 6fe2ab7 commit ed5df87
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/0934-Shortest-Bridge/0934.cpp
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;
}
63 changes: 63 additions & 0 deletions src/0934-Shortest-Bridge/0934.py
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))

0 comments on commit ed5df87

Please sign in to comment.