Skip to content

Instantly share code, notes, and snippets.

@unwosu
Created March 2, 2019 23:45
Show Gist options
  • Save unwosu/ad37ea0ff1dea384ad63179d29c78e0d to your computer and use it in GitHub Desktop.
Save unwosu/ad37ea0ff1dea384ad63179d29c78e0d to your computer and use it in GitHub Desktop.
Zombie Cluster Problem
mat = [
[1, 0, 0, 1],
[0, 1, 0, 0],
[1, 0, 1, 0],
[0, 1, 0, 1],
]
def dfs(mat, x: int, y: int) -> int:
if ((x < 0) or
(x > len(mat) - 1) or
(y < 0) or
(y > len(mat[0]) - 1) or
(mat[x][y] == 0)):
return
mat[x][y] = 0
dfs(mat, x, y+1)
dfs(mat, x, y-1)
dfs(mat, x-1, y)
dfs(mat, x+1, y)
def find_clusters(mat):
m = len(mat)
n = len(mat[0])
clusters = 0
for i in range(0, m):
for k in range(0, n):
if mat[i][k] == 1:
clusters = clusters + 1
dfs(mat, i, k)
return clusters
if __name__ == "__main__":
clusters = find_clusters(mat)
print("clusters == {}".format(clusters))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment