2020-09-13 dfs/回溯

题目链接

https://leetcode-cn.com/problems/word-search/submissions/

参考链接

https://leetcode-cn.com/problems/word-search/solution/shen-du-you-xian-sou-suo-hui-su-xi-jie-xiang-jie-b/

方法 dfs

class Solution:
    def exist(self, board: List[List[str]], word: str) -> bool:
        m ,n, l = len(board), len(board[0]), len(word)
        used = [[0]*n for _ in range(m)]
        bias = [(-1,0), (1,0), (0,-1), (0, 1)]
        def dfs(c, r, location):
            nonlocal used
            flag = False
            # 判断当前字符是否匹配,若不匹配直接返回,剪枝操作
            if board[c][r]==word[location]:
                # 已匹配到最后一个字符且相同,返回True
                if location==l-1: return True
                # 当前字符字符匹配成功,后续递归过程中无法再次使用
                used[c][r] = 1
                # 对当前字符上下左右四个位置中未匹配的字符进行递归           
                for dx, dy in bias:
                    x, y = c+dx, r+dy
                    if 0<=x<m and 0<=y<n and not used[x][y]:
                        flag = flag or dfs(x, y, location+1)
                # 回溯状态返回, 此字符可再次被使用
                used[c][r] = 0
            return flag
        # 遍历二维网格中的每一个字符
        for i in range(m):
            for j in range(n):
                if dfs(i, j, 0): return True
        return False


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。