--- title: "#79 Word Search" tags: LeetCode, Top100 --- #79 Word Search == 題目描述 -- Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cells, where "adjacent" cells are horizontally or vertically neighboring. The same letter cell may not be used more than once. Example 1: -- ![](https://i.imgur.com/fV5097P.png) >Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED" Output: true 解題思維 -- DFS DFS -- ```python= class Solution: def exist(self, board: List[List[str]], word: str) -> bool: def dfs(point, i, j): if point == k: return True if i < 0 or i >= m or j < 0 or j >= n: return False tmp = board[i][j] if tmp != word[point]: return False board[i][j] = '#' for x, y in [[0, -1], [0, 1], [-1, 0], [1, 0]]: if dfs(point+1, i+x, j+y): return True board[i][j] = tmp m = len(board) n = len(board[0]) k = len(word) for i in range(m): for j in range(n): if dfs(0, i, j): return True return False ```