79. Word Search

79. Word Search

python 复制代码
import numpy as np

class Solution:
    def exist(self, board: List[List[str]], word: str) -> bool:
        m,n=len(board),len(board[0])
        dic=defaultdict(set)
        for i in range(m):
            for j in range(n):
                dic[board[i][j]].add((i,j))

        def dfs(cord,indx):
            if indx==lngth:
                # if word_isr:results.append(word[::-1])
                # else:results.append(word)
                return True
 
            ch=word[indx]
            i,j=cord
            for cand in [(i-1,j),(i+1,j),(i,j-1),(i,j+1)]:
                if cand in dic[ch]:
                    dic[ch].remove(cand)
                    flag=dfs(cand,indx+1)
                    dic[ch].add(cand)
                    if flag:return True
            return False
        

        lngth=len(word)
        for cord in list(dic[word[0]]):
            dic[word[0]].remove(cord)
            flag=dfs(cord,1)
            dic[word[0]].add(cord)
            if flag:return True
        return False

how to store the location that previously visited

相关推荐
叶小鸡5 小时前
小鸡玩算法-力扣HOT100-多维动态规划
算法·leetcode·动态规划
菜菜的顾清寒6 小时前
力扣HOT100(42)链表-随机链表的复制
算法·leetcode·链表
菜菜的顾清寒9 小时前
HOT力扣100(43)二叉树-翻转二叉树
数据结构·算法·leetcode
csdn_aspnet10 小时前
javascript 算法 LeetCode 编号 70 - 爬楼梯
开发语言·javascript·算法·leetcode·ecmascript
Navigator_Z10 小时前
LeetCode //C - 1073. Adding Two Negabinary Numbers
c语言·算法·leetcode
csdn_aspnet11 小时前
PHP 算法 LeetCode 编号 70 - 爬楼梯
算法·leetcode·php
x_xbx11 小时前
LeetCode:5. 最长回文子串
算法·leetcode·职场和发展
小欣加油15 小时前
leetcode 3300 替换为数位和后的最小元素
数据结构·c++·算法·leetcode
人道领域16 小时前
【LeetCode刷题日记】108.将有序数组转换为二叉搜索树
java·算法·leetcode
过期动态16 小时前
【LeetCode 热题 100】无重复字符的最长子串
java·数据结构·spring boot·算法·leetcode·职场和发展