cpp
#include<iostream>
using namespace std;
//从下向上得到最优值
void dtower(int a[][100],int s[][100],int n)
{
for(int i=n; i>=1; i--)
{
for(int j=1; j<=i; j++)
{
if(i==n)
s[i][j]=a[i][j];
else
{
int t=s[i+1][j];
if(t<s[i+1][j+1])
t=s[i+1][j+1];
s[i][j]=a[i][j]+t;
}
}
}
}
void Traceback(int a[][100],int s[][100],int n,int i,int j)
{
if(i==n)
cout<<i<<" "<<j<<endl;
else
{
cout<<i<<" "<<j<<endl;
if(s[i][j]==a[i][j]+s[i+1][j])
Traceback(a,s,n,i+1,j);
else
Traceback(a,s,n,i+1,j+1);
}
}
int main()
{
int n;cin>>n;
int a[100][100]={0},s[100][100]={0};
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
cin>>a[i][j];
dtower(a,s,n);
cout<<"s[i][j]:"<<endl;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
cout<<s[i][j]<<" ";
cout<<endl;
}
Traceback(a,s,n,1,1);
return 0;
}
5
30
23 21
20 13 10
7 12 10 22
4 5 2 6 5