试题:Triangle

58 篇文章 0 订阅
46 篇文章 0 订阅

无意中看到别人博客上的这个题目,也来尝试解决下


题目原型:

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

For example, given the following triangle

[
     [2],
    [3,4],
   [6,5,7],
  [4,1,8,3]
]

The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).

Note:
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.


#include <iostream>

const int line = 4;

const int node_weight[line][line] =
{
    { 2 },
    { 3, 4 },
    { 6, 5, 7 },
    { 4, 1, 8, 3 }
    /*
    { 2 },
    { 4, 3 },
    { 7, 5, 6 },
    { 4, 2, 3, 0 }
    */
};

int path_index[line][line] = { 0 };

void solve()
{
    // path_index: 最后一行保存每条路径的总权重
    for (int col = 0; col < line; ++col)
    {
        path_index[line - 1][col] = node_weight[line - 1][col];
    }

    // path_index: 其余行保存最短路径中下个节点的下标
    for (int row = line - 2; row >= 0; --row)
    {
        for (int col = 0; col <= row; ++col)
        {
            if (path_index[line - 1][col] <= path_index[line - 1][col + 1])
            {
                path_index[row][col] = col;
                path_index[line - 1][col] = node_weight[row][col] + path_index[line - 1][col];
            }
            else
            {
                path_index[row][col] = col + 1;
                path_index[line - 1][col] = node_weight[row][col] + path_index[line - 1][col + 1];
            }
        }
    }

    // 打印最短路径的大小和最短路径
    std::cout << "path length: " << path_index[line - 1][0] << std::endl;
    std::cout << "path: " << node_weight[0][0];
    for (int row = 1, col = path_index[0][0]; row < line; ++row)
    {
        std::cout << " -> " << node_weight[row][col];
        col = path_index[row][col];
    }
    std::cout << std::endl;
}

int main(int argc, char * argv[])
{
    solve();
    return(0);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
open3d::geometry::TriangleMesh::CreateFromPointCloudAlphaShape 函数是Open3D库中用来根据给定的点云数据创建一个三角网格表达的函数。但当调用这个函数时如果失败,可能由以下几个原因导致: 1. 点云数据为空:函数需要至少存在一个点云数据才能创建三角网格,如果没有输入点云数据,函数将无法执行。 2. Alpha参数设置错误:这个函数需要一个Alpha参数来定义点云数据中点之间的连接程度。如果Alpha参数的值设置得过小,可能导致连接的面过少,从而无法成功创建三角网格。相反,如果Alpha参数的值设置得过大,可能导致连接的面过多,从而导致函数执行时间过长甚至内存溢出。 3. 输入的点云数据不适用于三角网格表示:这个函数是基于Alpha形状重建方法的,对于某些形状的点云数据可能无法成功创建三角网格。例如,如果点云数据中的点分布非常稀疏或是非常集中,都可能导致函数执行失败。 为了解决上述问题,我们可以检查点云数据是否为空、调整Alpha参数的值或者尝试其他适用于形状重建的方法,如Marching Cubes,Poisson Reconstruction等。此外,我们还可以对点云数据进行预处理,例如进行滤波、重采样等操作,以提高函数的成功率。最后,如果上述方法都无法解决问题,可能需要考虑点云数据本身的质量或者使用其他库或方法来处理点云数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值