-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01Matrix.py
More file actions
43 lines (34 loc) · 1.19 KB
/
Copy path01Matrix.py
File metadata and controls
43 lines (34 loc) · 1.19 KB
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
class Solution:
def isFull(self, maxtrix):
for x in range(0, self.row):
for y in range(0, self.collum):
if maxtrix[x][y] == -1:
return False
return True
def set_code(self, matrix, param, x, y):
if y + 1 < self.collum:
matrix[x][y + 1] = param
if y - 1 >= 0:
matrix[x][y - 1] = param
if x + 1 < self.row:
matrix[x + 1][y] = param
if x - 1 >= 0:
matrix[x - 1][y] = param
return matrix
def updateMatrix(self, matrix: List[List[int]]) -> List[List[int]]:
self.row = len(matrix)
self.collum = len(matrix[0])
for x in range(0, self.row):
for y in range(0, self.collum):
if matrix[x][y] == 1:
matrix[x][y] = -1
code = 0
while True:
for x in range(0, self.row):
for y in range(0, self.collum):
if matrix[x][y] == code:
new = code + 1
matrix = self.set_code(self, matrix, new, x, y)
code += 1
if self.isFull(self, matrix):
break