Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何在多维数组上进行递归_C_Recursion_Multidimensional Array_Connected Components - Fatal编程技术网

C 如何在多维数组上进行递归

C 如何在多维数组上进行递归,c,recursion,multidimensional-array,connected-components,C,Recursion,Multidimensional Array,Connected Components,我是递归新手,在如何在2D数组上进行递归的问题上,我很难找到正确的解决方案。你能告诉我如何在我的代码上修改我的逻辑吗。我认为我的算法不正确 我试图在某个点上创建递归,但它不起作用 #include <stdio.h> #define HEIGHT 21 #define WIDTH 5 int getLowestVertex(int graph[HEIGHT][WIDTH], int currLabel,int height, int width, int labelCount){

我是递归新手,在如何在2D数组上进行递归的问题上,我很难找到正确的解决方案。你能告诉我如何在我的代码上修改我的逻辑吗。我认为我的算法不正确

我试图在某个点上创建递归,但它不起作用

#include <stdio.h>

#define HEIGHT 21
#define WIDTH 5
int getLowestVertex(int graph[HEIGHT][WIDTH], int currLabel,int height, int width, int labelCount){
    int counter = 0;
    int low_label = 999;
    int j;
    int i;
    int *visitedLabelCheck = NULL;         //This will identify the visted label already
    visitedLabelCheck = (int *)malloc(labelCount * sizeof(int));

    //set all value to 0
    for(i = 0; i < labelCount; i++){
        visitedLabelCheck[i] = 0;
    }
    printf("\n");
    low_label = lowest_label(graph, currLabel, visitedLabelCheck, low_label);

    return low_label;
}

int lowest_label(int link[HEIGHT][WIDTH], int currentLabel, int *visitedLabelCheck, int low_label){
    int currentCounter = 0;
    int lowestLabel = 0;


    while(1) {//loop for the current label vertices  
        if(link[currentLabel][currentCounter] != 0){

            if(visitedLabelCheck[currentLabel] == -1)
                return link[currentLabel][currentCounter];

            if (link[currentLabel][currentCounter] < low_label) {
                low_label = link[currentLabel][currentCounter];
            }
            lowestLabel = lowest_label(link, link[currentLabel][currentCounter], visitedLabelCheck, lowestLabel);
            visitedLabelCheck[currentLabel] = -1;
            if (lowestLabel < low_label){
                return link[currentLabel][currentCounter];
            }
            currentCounter++;
        }else{
            break;
        }
    }
}


int main(int argc, char *argv[]) {

    int testThisVertex;
    int lowestVertex;
    int labelCount;
    int height;
    int width;
    int g[HEIGHT][WIDTH] =   {{1, 21, 0, 0, 0}, //undirected graph
                              {2,  0, 0, 0, 0},
                              {3,  0, 0, 0, 0}, 
                              {4,  5, 0, 0, 0},
                              {5,  4, 0, 0, 0},
                              {6, 21, 0, 0, 0},
                              {7,  8,2, 14, 0},
                              {8,  7, 9, 0, 0},
                              {9,  8,10, 0, 0},
                              {10, 9,11, 0, 0},
                              {11,10, 0, 0, 0},
                              {12,13, 0, 0, 0},
                              {13,12,14, 0, 0},
                              {14,13,15, 7, 0},
                              {15,14, 0, 0, 0},
                              {16, 0, 0, 0, 0},
                              {17, 0, 0, 0, 0},
                              {18, 0, 0, 0, 0},
                              {19,20, 0, 0, 0},
                              {20,19, 0, 0, 0},
                              {21,17,18, 6, 1}};

    //find the least label if input is 12 
    //12 -> 13 
    //      |  \
    //      12  14
    //          | \   \
    //         13  15  7
    //              |   | \  \
    //             14   8  2  14
    //ANSWER: 2
    testThisVertex=12;
    labelCount = 21;
    height = 21;
    width = 5;
    lowestVertex = getLowestVertex(g, testThisVertex, height, width, labelCount);
    printf("\nThe lowest value that is connected to %d vertex is %d", testThisVertex, lowestVertex);

    getchar();
}
#包括
#定义高度21
#定义宽度5
int getLowestVertex(int graph[HEIGHT][WIDTH],int currLabel,int HEIGHT,int WIDTH,int labelCount){
int计数器=0;
int low_label=999;
int j;
int i;
int*visitedLabelCheck=NULL;//这将标识已访问的标签
visitedLabelCheck=(int*)malloc(labelCount*sizeof(int));
//将所有值设置为0
对于(i=0;i 13 
//      |  \
//      12  14
//          | \   \
//         13  15  7
//              |   | \  \
//             14   8  2  14
//答复:二,
testThisVertex=12;
labelCount=21;
高度=21;
宽度=5;
lowestVertex=getLowestVertex(g,testThisVertex,高度,宽度,labelCount);
printf(“\n连接到%d个顶点的最小值是%d”,testThisVertex,lowerstVertex);
getchar();
}

预期输出应该是连接到顶点“testThisVertex”或当前顶点的最低顶点。但在我的结果中,它结束了一个又一个循环。

最低\u标签
在所有情况下都不会返回值,因为
else中断在中间

最后,在一些递归调用访问未初始化值和无效访问链接外和visitedLabelCheck后,如果(link[currentLabel][currentCounter]!=0)
中出现崩溃,因为currentLabel值1991084090`



我鼓励您使用警告编译,如果我使用带有选项-pedantic-Wall的gcc,编译器将在
最低标签
中指示缺少的返回值(以及更多)

int-graph[HEIGHT][WIDTH]
是一个二维数组,它不是
int**link
指针数组。这是一个巨大的差异。编译器应该警告您这一点<代码>图形[a][b]
等于
*(图形+a*宽度+b)
链接[a][b]
等于
*(*(链接+a)+b)
对不起,我再次编辑了函数,将其更改为链接[HEIGHT][WIDTH],请不要破坏您的帖子。通过在堆栈溢出上发布,您已经为SO授予了不可撤销的权利,以便在下分发该内容。根据SO政策,任何故意破坏行为都将恢复原状。