|
发表于 2022-5-4 17:32:00
|
显示全部楼层
我尝试用递归做出动态规划,没想到也行,但不知其它数据可以不可以正常 AC 呢?
动态规划公式:sum[x][y]=max(sum[x+1][y],sum[x+1][y+1])+arr[x][y]
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using std::vector, std::max;
- int foo(vector<vector<short>> arr, int x = 0, int y = 0) {
- if (x >= arr.size()) return 0;
- return arr[x][y] + max(foo(arr, x + 1, y), foo(arr, x + 1, y + 1));
- }
- using std::cin, std::cout, std::endl;
- int main(void) {
- short r, num;
- cin >> r;
- vector<short> temp;
- vector<vector<short>> arr(r, temp);
- for (int i = 0; i < r; ++i) {
- for (int j = 0; j < i + 1; ++j) {
- cin >> num;
- arr[i].push_back(num);
- }
- }
- cout << foo(arr) << endl;
- return 0;
- }
复制代码- 5
- 7
- 3 8
- 8 1 0
- 2 7 4 4
- 4 5 2 6 5
- 30
复制代码 |
|