forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequential-grid-path-cover.py
More file actions
37 lines (35 loc) · 1.14 KB
/
sequential-grid-path-cover.py
File metadata and controls
37 lines (35 loc) · 1.14 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
# Time: O(m * n * 3^(m * n))
# Space: O(m * n)
# backtracking
class Solution(object):
def findPath(self, grid, k):
"""
:type grid: List[List[int]]
:type k: int
:rtype: List[List[int]]
"""
DIRECTIONS = ((1, 0), (0, 1), (-1, 0), (0, -1))
def backtracking(i, j, curr):
v = grid[i][j]
if v and v != curr:
return False
grid[i][j] = -1
result.append([i, j])
if len(result) == len(grid)*len(grid[0]):
return True
new_curr = curr+1 if v == curr else curr
for di, dj in DIRECTIONS:
ni, nj = i+di, j+dj
if not (0 <= ni < len(grid) and 0 <= nj < len(grid[0]) and grid[ni][nj] != -1):
continue
if backtracking(ni, nj, new_curr):
return True
result.pop()
grid[i][j] = v
return False
result = []
for i in xrange(len(grid)):
for j in xrange(len(grid[0])):
if backtracking(i, j, 1):
return result
return result