-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo2.py
50 lines (43 loc) · 1.35 KB
/
demo2.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import helper
class Solution:
def __init__(self, matrix):
self.matrix = matrix
self.dp = []
def findPath(self):
n, m = len(self.matrix), len(self.matrix[0])
self.dp = [[0] * m for i in range(n)] # watchvar self.dp
self.dp[-1][-1] = 4
ans = self.hasPath(0, 0)
helper.DEBUG = 0
return ans
def hasPath(self, i, j):
i # watchvar i
j # watchvar j
n, m = len(self.matrix), len(self.matrix[0])
helper.dbg(self.dp + [i, j]) # Function call to debug
if i >= n or i < 0 or j >= m or j < 0: # Out of bounds
return False
if self.matrix[i][j] == 0:
self.dp[i][j] = 3
return False
if self.dp[i][j] == 1: # Gray
return False
if self.dp[i][j] == 3:
return False
if self.dp[i][j] == 4:
return True
ans = False
self.dp[i][j] = 1
right = self.hasPath(i + 1, j)
left = self.hasPath(i - 1, j)
up = self.hasPath(i, j + 1)
down = self.hasPath(i, j - 1)
ans = right or left or down or up
self.dp[i][j] = 4 if ans else 3
return ans
def main():
s = Solution([[1, 0, 1, 1, 0],
[1, 1, 0, 0, 1],
[0, 1, 1, 1, 0],
[0, 0, 0, 1, 1]])
s.findPath()