[LeetCode题解] 980. 不同路径 III

题目链接: https://leetcode-cn.com/problems/unique-paths-iii/comments/.

第一次自己写的代码版本

class Solution(object):
    def uniquePathsIII(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        start_i, start_j = 0, 0
        invalid_element = 0
        result = []
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                if grid[i][j] == -1:
                    invalid_element += 1
                elif grid[i][j] == 1:
                    start_i = i
                    start_j = j
        self.help(start_i, start_j, grid, len(grid) * len(grid[0]) - invalid_element, [], result)
        return len(result)

    def help(self, curr_i, curr_j, grid, valid_element, tmp, result):
        if curr_i not in range(len(grid)) or curr_j not in range(len(grid[0])): #越界行为
            return
        if (curr_i, curr_j) in tmp or grid[curr_i][curr_j] == -1: #不走重复道路
            return
        if grid[curr_i][curr_j] == 2 and len(tmp) == valid_element - 1: #检查是否遍历完成
            tmp.append((curr_i, curr_j))
            result.append(tmp)
            return
        self.help(curr_i + 1, curr_j, grid, valid_element, tmp + [(curr_i, curr_j)], result)
        self.help(curr_i - 1, curr_j, grid, valid_element, tmp + [(curr_i, curr_j)], result)
        self.help(curr_i, curr_j - 1, grid, valid_element, tmp + [(curr_i, curr_j)], result)
        self.help(curr_i, curr_j + 1, grid, valid_element, tmp + [(curr_i, curr_j)], result)

24 / 39 个通过测试用例,其他用例超时了,以上代码本质就是暴力遍历。

迭代更新版本:

class Solution(object):
    def uniquePathsIII(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        start_i, start_j = 0, 0
        valid_element = 1 #多少个有效节点 (包括出发点以及0的节点)
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                if grid[i][j] == 0:
                    valid_element += 1
                elif grid[i][j] == 1:
                    start_i = i
                    start_j = j
        return self.help(start_i, start_j, grid, valid_element)

    def help(self, curr_i, curr_j, grid, rest_available_element):
        if curr_i not in range(len(grid)) or curr_j not in range(len(grid[0])) or grid[curr_i][curr_j] == -1: #越界行为或无效点
            return 0
        if grid[curr_i][curr_j] == 2:
            if rest_available_element == 0: #检查是否遍历完成
                return 1
            else:
                return 0
        grid[curr_i][curr_j] = -1 #在此次遍历中,节点(curr_i, curr_j)已经被访问过
        tmp = 0
        tmp += self.help(curr_i, curr_j + 1, grid, rest_available_element - 1)
        tmp += self.help(curr_i, curr_j - 1, grid, rest_available_element - 1)
        tmp += self.help(curr_i + 1, curr_j, grid, rest_available_element - 1)
        tmp += self.help(curr_i - 1, curr_j, grid, rest_available_element - 1)
        grid[curr_i][curr_j] = 0 #当遍历结束后,恢复
        return tmp

参考了评论区的大神的评论。

发布了10 篇原创文章 · 获赞 0 · 访问量 128

猜你喜欢

转载自blog.csdn.net/m0_46134602/article/details/103837810