Memory search algorithm (MemorySearch) display path problem

I am a novice, but I recently learned the memory search algorithm of dynamic programming, which is the classic digital triangle problem. The last thing in the question is to display the value of the maximum path (that is, the summation), not to mention the points that are required to travel. The coordinates, I want to output the coordinates by the way, is there any efficient algorithm? At present, I have come up with a method (the efficiency may be very low), which is to use the array d of the values ​​recorded at the beginning to find, traverse the d of each row, find the maximum value and then record the coordinates. But I think it can be done by searching and recording while sifting.

Here is my code to try the latter

#include<iostream>
#include<algorithm>
using namespace std;
int d[107][107],a[107][107],n,njc,Roadx[100],Roady[100],Road_jc=1;
void PrintRoad(){
for(int i=1;i<=Road_jc;i++){
cout<<"第"<<i<<"组是"<<"("<<Roadx[i]<<","<<Roady[i]<<")"<<endl;
}
}
void Get_Road(int a,int b){
Roadx[Road_jc]=a;
Roady[Road_jc]=b;
Road_jc++;
}
void Get_Putin(){
for(int wjc=1;wjc<=n;wjc++){
njc=1;
for(int njc=1;njc<=wjc;njc++){
cin>>a[wjc][njc];
d[wjc][njc]=0;
}
}
}
int dfs(int x,int y){
if(d[x][y]!=0){
return d[x][y];
}
if(d[x][y]==0){
d[x][y]=a[x][y]+max(dfs(x+1,y),dfs(x+1,y+1));
if(dfs(x+1,y)>=dfs(x+1,y+1))Get_Road(x+1,y);
else Get_Road(x+1,y+1);
return d[x][y];
}
}
void Start_Chu(){
for(int jc=1;jc<=n;jc++)d[n][jc]=a[n][jc];
}
void De_Bug(){
for(int wjc=1;wjc<=n;wjc++){
njc=1;
for(int njc=1;njc<=wjc;njc++){
cout<<"a["<<wjc<<"]"<<"["<<njc<<"]"<<a[wjc][njc]<<" ";
cout<<"d["<<wjc<<"]"<<"["<<njc<<"]"<<d[wjc][njc]<<" ";
cout<<endl;
}
}
}
int main(){
cin>>n;
Get_Putin();
Start_Chu();
cout<<dfs(1,1)<<endl;
PrintRoad();
system("pause");
return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325375219&siteId=291194637