python写算法题:leetcode: 79. Word Search

本文介绍了一个算法,用于在二维字符矩阵中查找指定的单词。通过深度优先搜索和回溯策略,该算法能高效地确定单词是否存在于矩阵中。文章详细解释了方向检查、路径标记和搜索路径更新的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



class Solution(object):
    def checkDirection(self, w, h, board, ch, directions, direction, searchpath, pathmark):
        i,j,_=searchpath[-1]
        pathmark.add((i,j))
        for ind,dd in enumerate(directions[direction:], direction+1):
            di,dj=dd
            if i+di>=0 and i+di<w and j+dj>=0 and j+dj<h \
                and (i+di,j+dj) not in pathmark and board[j+dj][i+di]==ch:
                searchpath.append((i+di,j+dj,ind))
                pathmark.add((i+di,j+dj))
                return 0
        pathmark.remove((i,j))
        return -1
    def exist(self, board, word):
        """
        :type board: List[List[str]]
        :type word: str
        :rtype: bool
        """
        if len(word)==0: return True
        h=len(board)
        if h==0: return False
        w=len(board[0])
        wmap=[]
        for i in xrange(w):
            for j in xrange(h):
                if board[j][i]==word[0]:
                    wmap.append([(i,j,0),])

        for searchpath in wmap:
            ind=1
            pathmark=set()
            direction=0
            while ind < len(word):
                direction=self.checkDirection(w, h, board, word[ind], [(-1,0),(0,-1),(1,0),(0,1),], direction, searchpath, pathmark)
                if direction<0:
                    ind-=1
                    if ind<=0:
                        break
                    _,_,direction=searchpath[-1]
                    del searchpath[-1]
                else:
                    ind+=1
            if ind==len(word): return True
        return False



            # nextwmap=[]
            # for searchpath in wmap:
            #     i,j=searchpath[-1]
            #     if i>0 and (i-1,j) not in searchpath and board[j][i-1]==word[ind]:
            #         newpath=searchpath[:]
            #         newpath.append((i-1,j))
            #         nextwmap.append(newpath)
            #     if j>0 and (i,j-1) not in searchpath and board[j-1][i]==word[ind]:
            #         newpath=searchpath[:]
            #         newpath.append((i,j-1))
            #         nextwmap.append(newpath)
            #     if i<w-1 and (i+1,j) not in searchpath and board[j][i+1]==word[ind]:
            #         newpath=searchpath[:]
            #         newpath.append((i+1,j))
            #         nextwmap.append(newpath)
            #     if j<h-1 and (i,j+1) not in searchpath and board[j+1][i]==word[ind]:
            #         newpath=searchpath[:]
            #         newpath.append((i,j+1))
            #         nextwmap.append(newpath)
            # wmap=nextwmap
        return len(wmap)>0

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值