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

相关推荐
GalaxyPokemon8 小时前
LeetCode - 704. 二分查找
数据结构·算法·leetcode
liuqun031913 小时前
开心灿烂go开发面试题
算法·leetcode·golang
এ᭄画画的北北13 小时前
力扣-279.完全平方数
数据结构·算法·leetcode
GalaxyPokemon14 小时前
LeetCode - LCR 173. 点名
算法·leetcode·职场和发展
爱coding的橙子18 小时前
每日算法刷题Day31 6.14:leetcode二分答案2道题,结束二分答案,开始枚举技巧,用时1h10min
算法·leetcode·职场和发展
IC 见路不走21 小时前
LeetCode 第73题:矩阵置零
算法·leetcode·矩阵
黑听人1 天前
【力扣 简单 C】141. 环形链表
c语言·开发语言·数据结构·算法·leetcode
愚润求学1 天前
【递归、搜索与回溯】FloodFill算法(一)
c++·算法·leetcode
愚润求学1 天前
【递归、搜索与回溯】FloodFill算法(二)
c++·算法·leetcode
南枝异客1 天前
四数之和-力扣
java·算法·leetcode