寻找矩阵指定长度的路径最大值

问题描述:

给定一个矩阵mat和路径的长度 path_length, 输出最终的路径和最大路径的值:

例子1:

    mat 为5 × 5矩阵,寻找路径长度为path_length = 5的最大路径区域:

        [[ 55, 198,  72, 221,   7],
       [ 24, 247,  14, 210,  49],
       [ 66, 143,  44, 242, 118],
       [189, 131, 136,  43, 218],
       [123,  70, 153, 178,  18]]

输出:路径:

[[0, 3], [1, 3], [2, 3], [2, 4], [3, 4]]

路径总的最大值:1009

例子2:

    mat 为5 × 5矩阵,寻找路径长度为path_length = 5的最大路径区域:

       [[180, 240, 123,  25],
       [ 63,  41,   4, 127],
       [ 19, 203, 233,  60],
       [ 63,  26, 177, 250]]

输出:路径:

[[2, 1], [2, 2], [3, 2], [3, 3]]

路径总的最大值:863

解题代码:

import numpy as np
import sys

def compute_route(mat, i, j, max_points, visited=None):
    """
    计算距离为 max_points 的最大路径
    :param mat: 原始路径矩阵
    :param i: 开始行
    :param j: 开始列
    :param max_points: 路径长度
    :param visited: 最终的路径信息
    :return:
    """
    if visited is None:
        visited = []
    rows = mat.shape[0]  # 行
    cols = mat.shape[1]  # 列
    top = down = left = right = 0
    max_ij = [-1, -1]
    temp_max = 0
    if max_points == 1:
        return 0
    if i > 0:
        top = mat[i - 1][j]
        if temp_max < top and [i - 1, j] not in visited:
            max_ij = [i - 1, j]
            temp_max = top
    if j > 0:
        left = mat[i][j - 1]
        if temp_max < left and [i, j - 1] not in visited:
            max_ij = [i, j - 1]
            temp_max = left
    if i < rows - 1:
        down = mat[i + 1][j]
        if temp_max < down and [i + 1, j] not in visited:
            max_ij = [i + 1, j]
            temp_max = down
    if j < cols - 1:
        right = mat[i][j + 1]
        if temp_max < right and [i, j + 1] not in visited:
            max_ij = [i, j + 1]
            temp_max = right
    visited.append(max_ij)
    compute_route(mat, max_ij[0], max_ij[1], max_points - 1, visited)


def max_rote_value(mat, route_path):
    """
    计算最大路径的值
    :param mat: 原始路径值
    :param route_path: 路径二元列表
    :return: 最大路径值
    """
    max = 0
    for route in route_path:
        max += mat[route[0]][route[1]]
    return max


def found_mat_max_route(mat, path_length):
    """
    寻找出制定长度的最大路径
    :param mat: 需要寻找的矩阵
    :param path_length: 路径的长度
    :return: 最大路径的值,最大路劲:type List[[]]
    """
    temp_route_max = 0  # 存放路径最大值
    res_visited = []
    row = mat.shape[0]  # 行
    col = mat.shape[1]  # 列
    for i in range(row):
        for j in range(col):
            visited = [[i, j]]
            compute_route(mat, i, j, path_length, visited)
            now_route_max = max_rote_value(mat, visited)
            if temp_route_max < now_route_max:
                temp_route_max = now_route_max
                res_visited = visited

    return temp_route_max, res_visited

sys.setrecursionlimit(9000000)  # 这里设置大一些
# 随机生成一个 8*8 的[0, 255]范围内的矩阵
mat = np.random.randint(0, 255, (8, 8))

# 接收最大路径值
route_max = 0
# 接收最大路径
res_visited = []

route_max, res_visited = found_mat_max_route(mat, 5)

print("res_visited: ", res_visited)
print("route_max: ", route_max)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值