oop 運動動態規劃思想
#include<iostream>
#include <string.h>
using namespace std;
const int m = 350;
class Angel
{
public:
Angel();
int intput();
int solve();
const int getResult() const;
private:
int n;
int a[m][m];
int sum;
};
Angel::Angel()
{
int sum =0;
this->n = 0;
this->a[m][m] = {0};
}
int Angel::intput()
{
cin>>this->n;
for(int i =0;i < n; i++)
{
for (int j = 0;j<= i; j++)
cin>>this->a[i][j];
}
return 0;
}
int Angel::solve()
{
for(int i =1;i < n; i++)
{
for (int j = 0;j<= i; j++)
{
if (j == 0)
{
a[i][j]=a[i-1][j]+a[i][j];
}
else if (j == i)
{
a[i][j]=a[i-1][j-1]+a[i][j];
}
else
{
a[i][j] =max(a[i-1][j],a[i-1][j-1])+a[i][j];
}
}
}
for (int i = 0; i <n ; i++)
{
if (a[n-1][i]>sum)
{
sum = a[n-1][i];
}
}
return 0;
}
const int Angel::getResult() const
{
return this->sum;
}
template <typename T>
T max (T a,T b)
{
return a>b? a:b;
}
int main(int argc , const char * argv[])
{
Angel * a = new Angel();
a->intput();
a->solve();
cout<<a->getResult();
}