-
Notifications
You must be signed in to change notification settings - Fork 30
/
search.py
44 lines (35 loc) · 1.57 KB
/
search.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
from typing import List
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
if len(board) == 0 or len(word) == 0 or len(word) > len(board) * len(board[0]):
return False
visited = [[False] * len(board[0]) for i in range(len(board))]
for i in range(len(board)):
for j in range(len(board[0])):
if self.dfs(board, i, j, word, visited, ''):
return True
return False
def dfs(self, board, row, col, word, visited, path):
if visited[row][col]:
return False
if board[row][col] != word[len(path)]:
return False
path = path + board[row][col]
if path == word:
return True
visited[row][col] = True
if col - 1 >= 0 and self.dfs(board, row, col - 1, word, visited, path):
return True
if row - 1 >= 0 and self.dfs(board, row - 1, col, word, visited, path):
return True
if col + 1 < len(board[0]) and self.dfs(board, row, col + 1, word, visited, path):
return True
if row + 1 < len(board) and self.dfs(board, row + 1, col, word, visited, path):
return True
visited[row][col] = False
if __name__ == '__main__':
Instant = Solution()
Solve = Instant.exist([["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "ABCCED")
# board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED" -> true
# board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], "SEE" -> true
print(Solve)