POJ 1163:The Triangle(动态规划)
2017-04-09 16:19
344 查看
题目传送门:POJ 1163:The Triangle
简单动态规划,思路:打表
简单动态规划,思路:打表
#include <iostream> #include <cstring> using namespace std; const int maxSize = 105; int n; int arr[maxSize][maxSize]; int d[maxSize][maxSize]; void readInput(int n) // 读入三角形 { for (int i = 0; i < n; i++) for (int j = 0; j <= i; j++) cin >> arr[i][j]; } void showInput(int n) // 显示输入的三角形 { for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) cout << d[i][j]; cout << "\n"; } } int maxSum(int i, int j) { if (n == i) return 0; // 如果已经到最底层的下一层,则返回0 if (d[i][j] >= 0) return d[i][j]; // 如果大于等于0,说明已经计算过,无需重复计算 return d[i][j] = arr[i][j] + max(maxSum(i+1,j), maxSum(i+1,j+1)); // 递归计算最大和 } int main() { while (cin >> n) { memset(d, -1, sizeof(d)); // 将所有节点到最底层的最大和设为-1 readInput(n); cout << maxSum(0, 0) << endl; //showInput(n); } return 0; }
相关文章推荐
- POJ 1163 The Triangle(简单动态规划)
- poj-1163-The Triangle-动态规划dp
- poj 1163 The Triangle (动态规划)
- poj 1163 The Triangle 动态规划
- POJ 1163 The Triangle数塔 动态规划
- poj 1163 The Triangle 动态规划
- POJ1163-The Triangle-动态规划
- 【原】 POJ 1163 The Triangle 三角形最大路径 动态规划 解题报告
- POJ-1163-The Triangle (动态规划1)
- Poj1163 The Triangle(动态规划求最大权值的路径)
- Poj1163 The Triangle(动态规划求最大权值的路径)
- POJ - 1163 The Triangle(动态规划)
- POJ 1163 The Triangle (动态规划)
- Poj1163 The Triangle(动态规划求最大权值的路径)
- POJ-1163-The Triangle-动态规划
- poj 1163-小白算法练习 The Triangle 动态规划
- poj 动态规划DP - 1163 The Triangle
- The Triangle - POJ 1163 水题
- POJ 1163 The Triangle
- DP专题1 POJ 1163 The Triangle