• [leetcode]Word Search @ Python


    原题地址:https://oj.leetcode.com/problems/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 cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

    For example,
    Given board =

    [
      ["ABCE"],
      ["SFCS"],
      ["ADEE"]
    ]
    

    word = "ABCCED", -> returns true,
    word = "SEE", -> returns true,
    word = "ABCB", -> returns false.

    解题思路:使用dfs来搜索,为了避免已经用到的字母被重复搜索,将已经用到的字母临时替换为'#'就可以了。不知道用bfs可行否。

    代码:

    class Solution:
        # @param board, a list of lists of 1 length string
        # @param word, a string
        # @return a boolean
        def exist(self, board, word):
            def dfs(x, y, word):
                if len(word)==0: return True
                #up
                if x>0 and board[x-1][y]==word[0]:
                    tmp=board[x][y]; board[x][y]='#'
                    if dfs(x-1,y,word[1:]):
                        return True
                    board[x][y]=tmp
                #down
                if x<len(board)-1 and board[x+1][y]==word[0]:
                    tmp=board[x][y]; board[x][y]='#'
                    if dfs(x+1,y,word[1:]):
                        return True
                    board[x][y]=tmp
                #left
                if y>0 and board[x][y-1]==word[0]:
                    tmp=board[x][y]; board[x][y]='#'
                    if dfs(x,y-1,word[1:]):
                        return True
                    board[x][y]=tmp
                #right
                if y<len(board[0])-1 and board[x][y+1]==word[0]:
                    tmp=board[x][y]; board[x][y]='#'
                    if dfs(x,y+1,word[1:]):
                        return True
                    board[x][y]=tmp
                return False
                    
            for i in range(len(board)):
                for j in range(len(board[0])):
                    if board[i][j]==word[0]:
                        if(dfs(i,j,word[1:])):
                            return True
            return False
                
            
  • 相关阅读:
    PHP和JS判断手机还是电脑访问
    apache 一个站点配置多个域名
    拾遗
    提高工作效率的工具
    PHP urlencode
    JS encodeURI和encodeURIComponent
    Linux实践:ELF文件格式分析
    Linux实践:文件破解
    Linux实践:模块
    Linux内核学习期末总结(网课)
  • 原文地址:https://www.cnblogs.com/zuoyuan/p/3769767.html
Copyright © 2020-2023  润新知