So I want to make a recursive C++ program to find the minimum distance a person should go from top to bottom of a matrix. The person can go downward, diagonal right, and diagonal left. I need to display the total distance and which number he went through. This code works if I'm trying to find the maximum distance, but got an error when i turn it into finding the minimum distance. Any idea? Ps: I must use recursive function, no loops Thanks!
int SearchPath(int row_now, int col_now, int** arr, int row, int col, int total) {
if (row_now == row - 1) {
return total + arr[row_now][col_now];
}
int min = SearchPath(row_now + 1, col_now - 1, arr, row, col, total + arr[row_now][col_now]);
if (col_now > 0) {
int temp = SearchPath(row_now + 1, col_now, arr, row, col, total + arr[row_now][col_now]);
if (temp<min)
min = temp;
}
if (col_now < col - 1) {
int temp = SearchPath(row_now + 1, col_now + 1, arr, row, col, total + arr[row_now][col_now]);
if (temp<min)
min = temp;
}
return min;
}
int Start(int start, int end, int** arr, int row, int col) {
if (start == end)
return 0;
int min = SearchPath(0, start, arr, row, col, 0);
int temp = Start(start + 1, end, arr, row, col);
if (temp < min)
return temp;
else
return min;
}