You are on page 1of 2

#include<iostream>

#include<fstream>
using namespace std;
ifstream fin("piramida.in");
ofstream fout("piramida.out");
int matrix[100][100],n,copie[100][100];
int t[100][100];
void read()
{ int i,j;
fin>>n;
for(i=1;i<=n;i++)
for(j=1;j<=i;j++)
fin>>matrix[i][j];
}
// calculating the maximum sum, this is working fine
// calculating it in the matrix copie
int max_sum()
{ int i,j;
for(i=1;i<=n;i++)
copie[n][i]=matrix[n][i];
for(i=n-1;i>=1;i--)
for(j=1;j<=i;j++)
copie[i][j]=matrix[i][j]+max(copie[i+1][j],copie[i+1][j+1]);
}
//finding all the routes that have the maximum sum
// the matrix t is where I keep the routes
// I have to track the routes using their column position number
int track(int t[100][100],int m,int k,int j)
{ int p,i;
// k=1; // m=1; //j=1;
// k = columns' positions // m= the number of line for matrix t // j= for the
line where the bifurcation was found
for(i=j;i<=n;i++)
{
if(copie[i+1][k]>copie[i+1][k+1]) t[m][i+1]=k;
if(copie[i+1][k]<copie[i+1][k+1]) t[m][i+1]=k+1;
if(copie[i+1][k]==copie[i+1][k+1]){ t[m][i+1]=k; m=m+1;
/* copy the previous element
s until where I met the bifurcation */
for(p=1;p<=i;p++) t[m+1][p]=
t[m][p]; t[m+1][i+1]=k+1;
track(t,m,k+1,i+1); }
}
}
int main()
{ int i,j;

read();
max_sum();
for(i=1;i<=n;i++){fout<<'\n';
for(j=1;j<=i;j++)
fout<<copie[i][j]<<" ";
}
fout<<'\n'<<copie[1][1];
t[1][1]=1;
track(t,1,1,1);
for(i=1;i<=6;i++){ fout<<'\n';
for(j=1;j<=n;j++)
fout<<t[i][j]<<" ";
}
fin.close();
fout.close();
return 0;
}

You might also like