动态规划的核心往往是一个状态转移方程,而何谓状态呢?就是对于某特定事物在某一个位置时具有的属性,比如所携带的数值或者是其他的一些特性。状态转移又是什么呢?就是与当前状态相关的若干的状态通过某种特定方式转移到了当前状态上,在这期间所携带的特性可能发生了一些改变。比如说一个楼梯,我可以从两个台阶前迈过来,也可以从一个台阶前走过来,这都是状态转移的一种。
同样的,在这个题里,既然是要求一条和最大的路线,那么根据加法交换律,从下到上走和从上到下走是一样的效果。所以对于每一i层的元素,总有第i+1层的两个点可以与之连接,如果
#include<pch.h>
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#include <map>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <cstring>
#include <cmath>
#define DETERMINATION main
#define lldin(a) scanf_s("%lld", &a)
#define println(a) printf("%lld\n", a)
#define reset(a, b) memset(a, b, sizeof(a))
const int INF = 0x3f3f3f3f;
using namespace std;
const double PI = acos(-1);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int mod = 1000000007;
const int tool_const = 19991126;
const int tool_const2 = 2000;
inline ll lldcin()
{
ll tmp = 0, si = 1;
char c;
c = getchar();
while (c > '9' || c < '0')
{
if (c == '-')
si = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
{
tmp = tmp * 10 + c - '0';
c = getchar();
}
return si * tmp;
}
///Untersee Boot IXD2(1942)
/**Although there will be many obstructs ahead,
the desire for victory still fills you with determination..**/
/**Last Remote**/
ll a[500][500];
int DETERMINATION()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
ll n;
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
cin >> a[i][j];
for (int i = n - 1; i >= 1; i--)
for (int j = 1; j <= i; j++)
a[i][j] = a[i][j] + max(a[i+1][j], a[i+1][j + 1]);//状态转移方程
cout << a[1][1] << endl;
return 0;
}
需要得到最优答案,一直从这两个状态中选择一个最优状态即可。