🟡 剑指 Offer 12. 矩阵中的路径
LeetCode 提示
题目难度 中等
原题链接 🔗 leetcode
#
题解 1.pyclass Solution: def exist(self, board: List[List[str]], word: str) -> bool: def dfs(x, y, idx): if idx == len(word): return True if x < 0 or x >= len(board[0]) or y < 0 or y >= len(board): return False if board[y][x] == word[idx]: board[y][x] = '.' res = dfs(x, y-1, idx+1) or dfs(x, y+1, idx+1) or dfs(x+1, y, idx+1) or dfs(x-1, y, idx+1) board[y][x] = word[idx] return res return False for yy in range(len(board)): for xx in range(len(board[0])): if dfs(xx, yy, 0): return True return False