题目链接
应该用DP来解,对于题给三角形,我们可以这么看:
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
从上到下坐标x递增,从左到右坐标y递增。
我们要求的是全局最优解,每个位置只可能到它下一层的同坐标y或y+1,这样就可以从下向上递推,写出状态转移方程: r e s u l t [ i ] [ j ] = m a x ( r e s u l t [ i + 1 ] [ j + 1 ] , r e s u l t [ i + 1 ] [ j ] ) + n u m s [ i ] [ j ] result[i][j] = max(result[i+1][j+1], result[i+1][j]) + nums[i][j] result[i][j]=max(result[i+1][j+1],result[i+1][j])+nums[i][j]。
然后循环遍历即可求解,注意 i i i和 j j j的循环方向。
AC代码#include
#include
using namespace std;
int nums[1001][1001], result[1001][1001];
int main() {
int r;
cin >> r;
for (int i = 0; i nums[i][j];
}
}
for (int i = r-1; i >= 0; i--) {
for (int j = 0; j
关注
打赏
- 【Linux】Ubuntu20.04安装和卸载MySQL8
- 【Linux】Ubuntu 20.04 报错 curl: (23) Failure writing output to destination 的解决方法
- 【Java】JUnit 4.13.2 警告 ‘assertEquals(double, double)‘ is deprecated 的解决方法
- 【JavaScript】处理 @parcel/transformer-js: Browser scripts cannot have imports or exports.
- 【Node.js】Windows环境安装配置NVM和Node.js
- 【Python】处理TypeError: Plain typing.NoReturn is not valid as type argument
- 【Python】Matplotlib可视化50例
- 【C语言】C语言修改MySQL数据库
- 【Java】从默认包导入类和对象报错的解决方法
- 【Java】panel.getGraphics()报错空指针异常的解决方法