-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs_matrix.py
36 lines (33 loc) · 971 Bytes
/
dfs_matrix.py
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
"""
Input: a matrix with all 0s
Output: just the path at every step
"""
def dfs(matrix):
root = (0, 0)
matrix[0][0] = 1
stack = [root]
print(stack)
while len(stack) > 0:
head = stack[-1] # peeking into the stack
neighbors = fetchNeighbors(matrix, head)
if len(neighbors) > 0:
i, j = neighbors[0][0], neighbors[0][1]
matrix[i][j] = 1
stack.append((i, j))
print(stack)
else:
stack.pop()
def fetchNeighbors(matrix, pt):
neighbors = []
i, j = pt[0], pt[1]
if i > 0 and matrix[i - 1][j] != 1:
neighbors.append((i-1, j))
if j > 0 and matrix[i][j - 1] != 1:
neighbors.append((i, j-1))
if i < len(matrix) - 1 and matrix[i + 1][j] != 1:
neighbors.append((i+1, j))
if j < len(matrix[0]) - 1 and matrix[i][j + 1] != 1:
neighbors.append((i, j+1))
return neighbors
x = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
dfs(x)