-
Notifications
You must be signed in to change notification settings - Fork 0
/
79.py
71 lines (68 loc) · 2.44 KB
/
79.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Solution(object):
def exist(self, board, word):
"""
:type board: List[List[str]]
:type word: str
:rtype: bool
"""
if len(word)==0:
return False
m = len(board)
n = len(board[0])
mark = [[0 for _ in range(n)] for _ in range(m)]
for i in range(m):
for j in range(n):
if board[i][j]==word[0]:
mark[i][j]=1
if self.recursive(board, mark, word[1:], i, j, m, n):
return True
else:
mark[i][j]=0
return False
def recursive(self, board, mark, word, i, j, m, n):
if len(word)==0:
return True
if len(word)==1:
if i>0 and mark[i-1][j]==0 and board[i-1][j]==word[0]:
return True
if i<m-1 and mark[i+1][j]==0 and board[i+1][j]==word[0]:
return True
if j>0 and mark[i][j-1]==0 and board[i][j-1]==word[0]:
return True
if j<n-1 and mark[i][j+1]==0 and board[i][j+1]==word[0]:
return True
return False
flag = False
if i > 0 and mark[i - 1][j] == 0 and board[i - 1][j] == word[0]:
mark[i-1][j]=1
flag = self.recursive(board, mark, word[1:], i-1, j, m, n)
if flag == True:
return flag
else:
mark[i - 1][j] = 0
if i < m - 1 and mark[i + 1][j] == 0 and board[i + 1][j] == word[0]:
mark[i + 1][j] = 1
flag = self.recursive(board, mark, word[1:], i + 1, j, m, n)
if flag == True:
return flag
else:
mark[i + 1][j] = 0
if j > 0 and mark[i][j - 1] == 0 and board[i][j - 1] == word[0]:
mark[i][j-1] = 1
flag = self.recursive(board, mark, word[1:], i, j-1, m, n)
if flag == True:
return flag
else:
mark[i][j - 1] = 0
if j < n - 1 and mark[i][j + 1] == 0 and board[i][j + 1] == word[0]:
mark[i][j+1] = 1
flag = self.recursive(board, mark, word[1:], i, j+1, m, n)
if flag == True:
return flag
else:
mark[i][j + 1] = 0
return flag
if __name__ == '__main__':
solution = Solution()
print solution.exist([["C","A","A"],["A","A","A"],["B","C","D"]],
"AAB")