http://train.usaco.org/usacoprob2?a=Svf4STUJArF&S=numtri
经典数塔问题
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
//动态归化
int a[1005][1005];
int main()
{
ifstream fin("numtri.in");
ofstream fout("numtri.out");
int n;
fin >> n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= i; j++)
fin >> a[i][j];
for(int i = n; i >= 1; i--)
for(int j = 1; j <= i; j++)
a[i][j] += a[i + 1][j] > a[i + 1][j + 1] ? a[i + 1][j] : a[i + 1][j + 1];
fout << a[1][1] << endl;
fout.close();
return 0;
}
本文介绍了一个经典的数塔问题,并提供了一种通过动态规划解决该问题的方法。代码使用C++编写,通过从底部向上更新每一步的最大路径和,最终得出位于数塔顶部的最大路径和。
487

被折叠的 条评论
为什么被折叠?



