このプログラムは、左から右への最短経路の重みを 2 次元配列で返す必要があります (上と下をまたぐことができるため、水平の円柱のようなものです)。(ここに完全なquestion_linkがあります)配列内で最初に上に移動し、次に右に移動し、最後に下に移動して、再帰的にチェックしようとしています。このプログラムを実行すると、行の右方向と下方向のコメントを外すと、「セグメンテーション違反」が発生します。誰かが私の再帰関数で間違っていることを教えてくれれば。前もって感謝します!
#include<iostream>
using namespace std;
int rec_path(int matrix[5][6], int r, int c){
static int sum = 0;
static int weight = -1;
    if (r == -1) 
    r = 4;
if (r == 5) 
    r = 0;
if (c == 6) {
    return weight;
    sum = 0;
}
//calculate sum 
sum += matrix[r][c];    
//check the up direction
rec_path(matrix, --r, ++c);
//check the right direction
//  rec_path(matrix, r, ++c);
//check the bottom direction
//  rec_path(matrix, ++r, ++c);
if (weight == -1) 
    weight = sum;
if ( weight < sum) {
    weight = sum;
}
}
int main(){
const int row = 5;
const int col = 6;
int matrix[row][col] = {{3,4,2,1,8,6},
                        {6,1,8,2,7,4},
                        {5,9,3,9,9,5},
                        {8,4,1,3,2,6},
                        {3,7,2,8,6,4}
                        };
cout << rec_path(matrix,0,0) << endl;
return 0;
}