真的神奇

'str' object does not support item assignment

class Solution:
    def hasPath(self, grid, r, c,  path):
        # write code here
        #r, c = len(grid), len(grid[0])
        def dfs(i, j, p):
            print(i, j, p)
            if len(p) == 0: return True
            if not (0<=i<r and 0<=j<c):
                return False
            if not grid[i][j] == p[0]: return False

            restore = grid[i][j]
            grid[i][j] = ' '

            ans = dfs(i+1, j, p[1:]) or dfs(i-1, j, p[1:]) or dfs(i, j+1, p[1:]) or dfs(i, j-1, p[1:])
            grid[i][j] = restore

            return ans

        head = path[0]

        for i in range(r):
            for j in range(c):
                if grid[i][j] == head:
                    if dfs(i, j, path): return True

        return False
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务