前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >动态规划应用--“杨辉三角”最短路径 & LeetCode 120

动态规划应用--“杨辉三角”最短路径 & LeetCode 120

作者头像
Michael阿明
发布2021-02-20 10:50:54
发布2021-02-20 10:50:54
80700
代码可运行
举报
运行总次数:0
代码可运行

1. 问题描述

对“杨辉三角"进行一些改造。每个位置的数字可以随意填写,经过某个数字只能到达下面一层相邻的两个数字。 假设你站在第一层,往下移动,我们把移动到最底层所经过的所有数字之和,定义为路径的长度。请你编程求出从最高层移动到最底层的最短路径。

2. DP算法代码

代码语言:javascript
代码运行次数:0
复制
/**
 * @description: 改造的杨辉三角,从顶层下来的最小和
 * @author: michael ming
 * @date: 2019/7/17 23:03
 * @modified by: 
 */
#include <cstring>
#include <algorithm>
#include <iostream>

using namespace std;
const int high = 5;
int YHTriangle[high][high] = {{5},{7,8},{2,1,4},{4,2,6,1},{2,7,3,4,5}};

void shortestPath()
{
    int states[high][high];//存放路径距离(存较小的)
//    memset(states,65535,high*high*sizeof(int));
    states[0][0] = YHTriangle[0][0];//第一个点的距离
    int i, j;
    for(i = 1; i < high; ++i)//动态规划
    {
        for(j = 0; j <= i; ++j)
        {
            if(j == 0)//在两边,上一个状态没得选,只有一个
                states[i][j] = states[i-1][j]+YHTriangle[i][j];
            else if(j == i)//在两边,上一个状态没得选,只有一个
                states[i][j] = states[i-1][j-1]+YHTriangle[i][j];
            else//在中间,上一个状态有两个,选路径短的
                states[i][j] = min(states[i-1][j],states[i-1][j-1])+YHTriangle[i][j];
        }
    }
    int mins = 65535, idx;
    for(j = 0; j < high; ++j)
    {
        if(mins > states[high-1][j])
        {
            mins = states[high-1][j];//选出最短的
            idx = j;//记录最短的路径位置
        }
    }
    cout << "最短的路径是:" << mins << endl;
    //-----------打印路径-----------------------
    cout << "从底向上走过的点的值是:" << endl;
    cout << YHTriangle[high-1][idx] << " ";
    for(i = high-1,j = idx; i > 0; --i)
    {
        if(j == 0)
            cout << YHTriangle[i-1][j] << " ";
        else if(j == i)
        {
            cout << YHTriangle[i-1][j-1] << " ";
            j--;
        }
        else
        {
            if(states[i-1][j-1] < states[i-1][j])
            {
                cout << YHTriangle[i-1][j-1] << " ";
                j--;
            }
            else
                cout << YHTriangle[i-1][j] << " ";
        }
    }
}
int main()
{
    shortestPath();
    return 0;
}

更改数据再测试

3. LeetCode 120 三角形最小路径和

https://leetcode-cn.com/problems/triangle/

以下解法是O(n)空间复杂度

代码语言:javascript
代码运行次数:0
复制
class Solution 
{
public:
    int minimumTotal(vector<vector<int>>& triangle) 
    {
    	int n = triangle.size();
        int states[n];
        int temp_states[n];
        states[0] = triangle[0][0];
        int i, j, k, minsum = INT_MAX;
        for (i = 1; i < n; i++)
        {
        	for(j = 0; j < i+1; j++)
        	{
        		if(j == 0)
        			temp_states[0] = states[0] + triangle[i][j];
        		else if(j == i)
        			temp_states[j] = states[j-1] + triangle[i][j];
        		else
        			temp_states[j] = min(states[j-1], states[j]) + triangle[i][j];
        	}
        	for(k = 0; k < i+1; k++)
        		states[k] = temp_states[k];//更新states
        }
        for(j = 0; j < n; j++)//求最小的值
        {
        	if(states[j] < minsum)
        		minsum = states[j];
        }
        return minsum;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/07/17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 问题描述
  • 2. DP算法代码
  • 3. LeetCode 120 三角形最小路径和
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档