from IPython import embed #length:棋盘长度 class Solution(): def modernLudo(self, length, connections): ans = [i for i in range(length + 1)] for i in range(length + 1): for j in range(1, 7): if i - j >= 0: ans[i] = min(ans[i], ans[i - j] + 1) for j in connections: if i == j[1]: ans[i] = min(ans[i], ans[j[0]]) return ans[length] if __name__ == '__main__': length = 15 solution = Solution() connections = [[2,13]] print("棋盘长度:",length) print("连接:",connections) print("最小需要:",solution.modernLudo(length, connections)) 棋盘长度: 15 连接: [[2, 13]] 最小需要: 2