本文发布且更新于个人博客:https://www.xerrors.fun/leetcode/word-search/
1. 题目
题目链接:https://leetcode-cn.com/problems/word-search/
2. 分析思路
第一部分是自己的分析思路,后一部分是参考资料之后的完善的思路,记录自己的思考过程的同时记录优秀的解法,把这个题目弄明白
思考过程
对于图搜索问题,我印象中就是广度搜索,深入搜索,启发式等等依靠队列、栈、优先队列等实现的搜索。
首先使用深度优先搜索来实现一下:
class Solution:
def exist(self, board: List[List[str]], word: str) -> bool:
queue = []
m, n, l = len(board), len(board[0]), len(word)
for i in range(m):
for j in range(n):
if board[i][j] == word[0]:
queue.append([1, i, j, [i*200+j]])
while len(queue) != 0:
cur = queue.pop()
ind, i, j, path = cur
if ind == l:
return True
temp = path[-1]+200
if i+1 < m and board[i+1][j] == word[ind] and temp not in path