leetcode-mid-backtracking -22. Generate Parentheses-79 Word Search -NO

mycode error because draws Number of Islands in question method, resulting in even though there have been the answer, and will continue to traverse in a for loop. But not the same two topics, island need to find out all the circumstances, this problem just need to find the first complete results can be returned

 reference:

 
def exist(board, word):
        """
        :type board: List[List[str]]
        :type word: str
        :rtype: bool
        """
        def find(board, word, i, j, m, n):
            if word == '':
                print('if',i,j)
                return True
            if i < 0 or i >= m or j < 0 or j >= n:
                print('if2',i,j)
                return False
            elif word[0] == board[i][j]:
                print('elif',i,j)
                board[i][j] = None #标记
                res = find(board, word[1:], i+1, j, m, n)or find(board, word[1:], i-1, j, m, n)or find(board, word[1:], i, j+1, m, n)or find(board, word[1:], i, j-1, m, n)
                board[i][j] = word[0] #恢复原来的值
                return res
            print(i,j)
        if len(word) == 0:
            return True
        m = len(board)
        if m == 0:
            return False
        n = len(board[0])
        for i in range(m):
            for j in range(n):
                if find(board, word, i, j, m, n):
                    return True
        return False

 

 

Guess you like

Origin www.cnblogs.com/rosyYY/p/10972826.html