I have a C++ problem that i just can not get right. I has been driving me crazy for quite some time.
The problem is as follows :
Numbers have been randomly inputed into an integer 2D array by the user. The program has to find a path from the top left corner to the bottom right corner but also keeping in mind to find the path/s whose sum is minimum. Only up and down and sideways movement is allowed.Diagonal movement is not allowed.
I have already written a code but it just hangs or goes into an infinite recursions.The code I have used is :
int pathSum(int a[MAX][MAX],int r,int c, int row,int col,int sum)
{int ci,ri;
static int min=0;
for(ri=1;ri<=1;ri++)
{if(r+ri<0r+ri>=row)
continue;
for(ci=1;ci<=1;ci++)
{if(c+ci<0c+ci>=col(ci+ri)%2==0)
continue;
if(r!=rowc!=col)
{sum+=a[r][c];
pathSum(a, r+ri,c+ci,row,col,sum);
}
else
{if(sum<min)
min=sum;
}
}
}
return min;
}
I am still a student but this problem has taken a firm hold on me. Please help.
