zgjja


私信TA

用户名:zgjja

访问量:10808

签 名:

X_X

等  级
排  名 147
经  验 7109
参赛次数 0
文章发表 71
年  龄 0
在职情况 学生
学  校
专  业 X_X

  自我简介:

解题思路:
参考:https://blog.csdn.net/u014665013/article/details/45201479


注意事项:
DFS+记忆化搜索

参考代码:

def dfs(x, y, loads: int, max_value: int):
    global map_, n, m, k, dp

    if dp[x][y][loads][max_value + 1] != -1:  # visited
        return dp[x][y][loads][max_value + 1]
    if x == n - 1 and y == m - 1:  # destination
        if loads == k or (loads == k - 1 and map_[x][y] > max_value):
            dp[x][y][loads][max_value + 1] = 1
            return 1
        else:
            dp[x][y][loads][max_value + 1] = 0
            return 0
    a = 0
    current_value = map_[x][y]
    for directions in [[0, 1], [1, 0]]:  # right, down
        if x + directions[0] < n and y + directions[1] < m:  # index check
            if current_value > max_value and loads < k:  # value check
                a += dfs(x + directions[0], y + directions[1], loads + 1, current_value)  # take
            a += dfs(x + directions[0], y + directions[1], loads, max_value)
    dp[x][y][loads][max_value + 1] = a
    return dp[x][y][loads][max_value + 1]

n, m, k = map(int, input().split())
map_ = [list(map(int, input().split())) for _ in range(n)]
dp = [[[[-1 for _ in range(14)] for _ in range(13)] for _ in range(m + 1)] for _ in range(n + 1)]
print(dfs(0, 0, 0, -1) % 1000000007)


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区

a是用来干嘛的啊?没看懂
2022-03-12 10:53:15
  • «
  • 1
  • »