So on this drawback I’m given an NxN matrix, during which every worth of the matrix represents an altitude. I want to search out one of the best path from the top-left nook to the bottom-right one, my strikes being restricted to down and proper.

Additionally, the trail must be chosen in such means that the variations in altitude are the smallest, principally discovering the trail that has the smallest sum of variations in altitude.

I believe I’m utilizing dynamic programming in my answer.(I’m nonetheless a newbie, I’m not so certain of this)

First I start by creating one other matrix that has the sums of the variations in altitude of the sides of the matrix, being the simplest to calculate as a result of there’s a single means that these sums could be computed. (S is the brand new matrix, whereas A is the matrix that’s given)

```
S[0][0]=0;
for(int i=1;i<=n-1;i++)
{S[0][i]=S[0][i-1]+abs(A[0][i]-A[0][i-1]);
S[i][0]=S[i-1][0]+abs(A[i][0]-A[i-1][0]);}
```

Secondly, I’m traversing the matrix by evaluating the sums of the already calculated paths(the sides at first)+ the 2 methods (up or down). Whichever one is smallest, can be put within the new matrix.

```
for(int i=1;i<=n-1;i++)
for(int j=1;j<=n-1;j++)
if(S[i-1][j]+abs(A[i][j]-A[i-1][j])<S[i][j-1]+abs(A[i][j]-A[i][j-1]))
S[i][j]=S[i-1][j]+abs(A[i][j]-A[i-1][j]);
else
S[i][j]= S[i][j-1]+abs(A[i][j]-A[i][j-1]);
```

This drawback labored for the examples that I attempted, however I’m questioning if there’s a extra environment friendly means. Additionally, i might have made a mistake (or many 🙂 ), so please be happy to inform me should you suppose it’s a good answer, a nasty one and when you’ve got any recommendation for me.

Thanks for the eye in any case!

The whole code:

```
#embody <iostream>
#embody <fstream>
#embody <cmath>
utilizing namespace std;
ifstream fin("knowledge.in");
ofstream fout("knowledge.out");
int A[21][21],n,S[21][21],D[42][4];
int principal()
{ //studying the matrix
fin>>n;
for(int i=0;i<=n-1;i++)
for(int j=0;j<=n-1;j++)
fin>>A[i][j];
//computing the sides of the brand new matrix
S[0][0]=0;
for(int i=1;i<=n-1;i++)
{S[0][i]=S[0][i-1]+abs(A[0][i]-A[0][i-1]);
S[i][0]=S[i-1][0]+abs(A[i][0]-A[i-1][0]);}
//evaluating the sums
for(int i=1;i<=n-1;i++)
for(int j=1;j<=n-1;j++)
if(S[i-1][j]+abs(A[i][j]-A[i-1][j])<S[i][j-1]+abs(A[i][j]-A[i][j-1]))
S[i][j]=S[i-1][j]+abs(A[i][j]-A[i-1][j]);
else
S[i][j]= S[i][j-1]+abs(A[i][j]-A[i][j-1]);
//a brand new matrix to determine the trail and memorize the down/proper strikes
int i,j,x;
i=n-1;
j=n-1;
x=1;
D[0][0]=A[n-1][n-1];
whereas(i!=0&&j!=0)
{if(S[i-1][j]<S[i][j-1])
{
D[x][0]=A[i-1][j];
D[x][1]=1; //1 for down
x++;
i=i-1;}
else
{
D[x][0]=A[i][j-1];
D[x][1]=2; //2 for proper
x++;
j=j-1;
}}
if(S[1][0]>S[0][1])
D[x][1]=2;
else
D[x][1]=1;
D[x][0]=A[0][0];
//exhibiting the trail
fout<<"The trail:"<<endl;
for(i=x;i>=0;i--)
{ fout<<endl<<D[i][0];
if(i!=0)
{if(D[i][1]==1)
fout<<endl<<"down";
else
if(D[i][1]==2)
fout<<endl<<"proper";}
}
return 0;
}
```