"""
给定一个二维网格和一个单词,找出该单词是否存在于网格中。
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
示例:
board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
给定 word = "ABCCED", 返回 true.
给定 word = "SEE", 返回 true.
给定 word = "ABCB", 返回 false.
"""
directions = [(0, -1), (-1, 0), (0, 1), (1, 0)]
def exist(board, word):
m = len(board)
if m == 0:
return False
n = len(board[0])
marked = [[False for _ in range(n)] for _ in range(m)]
for i in range(m):
for j in range(n):
if _search_word(board, word, 0, i, j, marked, m, n):
return True
return False
def _search_word(board, word, index, start_x, start_y, marked, m, n):
# 先写递归终止条件
if index == len(word)-1:
return board[start_x][start_y] == word[index]
# 中间匹配了,再继续搜索
if board[start_x][start_y] == word[index]:
# 先占住这个位置,搜索不成功的话, 要释放掉
marked[start_x][start_y] = True
for direction in directions:
new_x = start_x + direction[0]
new_y = start_y + direction[1]
if 0 <= new_x < m and 0 <= new_y <n and not marked[new_x][new_y] \
and _search_word(board, word, index+1, new_x, new_y, marked, m, n):
return True
marked[start_x][start_y] = False
return False
board = [['A', 'B', 'C', 'E'],
['S', 'F', 'C', 'S'],
['A', 'D', 'E', 'E']]
word = "SEE"
print(exist(board, word))
79_单词搜索
最新推荐文章于 2021-04-13 10:10:17 发布