C 泛洪填充函数调用不工作

C 泛洪填充函数调用不工作,c,C,为了解决家庭作业中的任务,我第一次不得不使用洪水填充算法。主要问题是,我编写的洪水填充函数的调用似乎不起作用 我的任务与这里描述的任务非常相似: 我在这里使用了一个算法:我根据需要对它进行了调整 例如,我有一个矩阵: 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 1 0 0 1 1 1 1 1 0 0 1 0 0 0 1 1 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

为了解决家庭作业中的任务,我第一次不得不使用洪水填充算法。主要问题是,我编写的洪水填充函数的调用似乎不起作用

我的任务与这里描述的任务非常相似:

我在这里使用了一个算法:我根据需要对它进行了调整

例如,我有一个矩阵:

0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 0 0 0 0 0
0 0 1 1 1 1 1 0 1 0
0 1 1 1 1 1 0 0 1 0
0 0 1 1 1 0 0 0 1 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 0 0 0
0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
我想把它变成这样:

0 0 0 0 0 0 0 0 0 0
0 0 0 2 2 0 0 0 0 0
0 0 2 2 2 2 2 0 3 0
0 2 2 2 2 2 0 0 3 0
0 0 2 2 2 0 0 0 3 0
0 0 0 0 2 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 4 4 4 4 4 4 0 0 0
0 0 0 4 4 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
以及计算每个“区域”或簇的元素

在主程序中,我搜索等于1的第一个元素,并将其位置(x,y)发送到函数“color”(泛光填充函数)以成为“seed”

我尝试使用gdb查找bug-什么也没找到。 我在代码中添加了一些printf(),以查看发生了什么-在调用函数“color”之前,一切正常,然后什么也没有发生。 我在互联网上搜索了一个解决方案,但没有找到解决这个问题的方法

以下是洪水填充的代码:

int color(int x,int y,int n,int**sim,int nr, int h, int w)
{
if (x < h && y < w)
if( sim[x][y] == 1 )
    { 
    sim[x][y] = n;
    printf ("%d %d   ", x, y);//it does not print anything here

    if (sim[x-1][y] != 1 && sim[x+1][y] != 1 && sim[x][y-1] != 1 && sim[x][y+1] != 1)
        return nr; /*this happens when everything around is equal to 0 or n, so there is nothing to modify and the program should end*/
    else
        {
            nr++;
            color(x-1,y,n,sim,nr,h,w);
            color(x+1,y,n,sim,nr,h,w);
            color(x,y-1,n,sim,nr,h,w);
            color(x,y+1,n,sim,nr,h,w);
        }
    }
}
int颜色(int x,int y,int n,int**sim,int nr,int h,int w)
{
如果(x
以及主函数中的调用:

int **s;
s = malloc(m*sizeof(int *));
for(i=1; i <= h; i++)
    s[i] = malloc(m*sizeof(int));
a=0;
b=0;
while (a <= h && b <= w)
    {
    k = 0;
    for(i=a; i < h && k == 0; i++)
        for(j=b; j < w; j++)
            if( s[i][j] == 1 ) //find the first element = 1 and stop to its position
                {k = 1; printf("%d %d ", i, j);}
    printf("\n");
    if(k == 1)
        {
        a = i;
        b = j;
        nr = color(i,j,c,s,0,h,w); //call the function
        printf("%d,%d,%d,%d ", k, c, i, j);
        cluster[c] = nr;
        c++;
        }
    if (k == 0)
        break; //if it is no area left to modify
    }
int**s;
s=malloc(m*sizeof(int*);

对于(i=1;i而言,运算代码中存在大量错误,例如使用交换的H和V数组方向,并且不检查数组边界以防止递归从数组边缘移动,从而在发现“1”时继续,或者更糟的是,使用未定义的指针访问内存

#include <stdio.h>
#include <stdlib.h>

#define h  11   // height
#define w  10   // width

void color(int x, int y, int n, int **sim, int *nr) {
    if (x>=0 && x<w && y>=0 && y<h && n>1) {
        if( sim[y][x] == 1 ) { 
            sim[y][x] = n;
            (*nr)++;
            color(x-1, y,   n, sim, nr);
            color(x+1, y,   n, sim, nr);
            color(x,   y-1, n, sim, nr);
            color(x,   y+1, n, sim, nr);
        }
    }
}

void show(int **sim) {
    int i, j;
    for (j=0; j<h; j++) {
        for (i=0; i<w; i++) {
            printf ("%3d", sim[j][i]);
        }
        printf ("\n");
    }
    printf ("\n");
}

int main() {
    int **s;
    int n, i, j, nr;
    s = malloc(h*sizeof(int *));
    for(j=0; j<h; j++)
        s[j] = malloc(w*sizeof(int));
    for (j=0; j<h; j++)
        for (i=0; i<w; i++)
            s[j][i] = rand() % 2;
    show(s);

    n = 2;
    for (j=0; j<h; j++) {
        for (i=0; i<w; i++) {
            if (s[j][i] == 1) {
                nr = 0;
                color(i, j, n, s, &nr);
                printf("%3d,%3d,%3d,%3d\n", i, j, n, nr);
                n++;
            }
        }
    }
    printf ("\n");
    show(s);

    for(i=h-1; i>=0; i--)
        free (s[i]);
    free (s);
    return 0;
}

什么是
m
?您已经使用
m
为数组的宽度和高度分配内存,但是您的示例中的数组不是正方形的。我将为
h
行分配内存,每个行的宽度
w
。我将使用循环将数组索引为0..(h-1)和0..(w-1)。在
颜色()
function,我会检查索引边界,因为递归可以将索引传递到数组边界之外。一般来说,要小心从internet复制的所有示例。这同样适用于。您发现的许多示例都不正确或包含重大错误。m是矩阵ca的最大行数和列数最初我为h行和w列分配了内存,但我遇到了分段错误(内核转储)…我与gdb进行了检查,结果显示:“3996 malloc.c:没有这样的文件或目录。”它只在我分配m时起作用。我不知道为什么它不让我分配更少。可能是因为你没有检查数组边界。顺便说一句,
color()
应该返回一个值。我更改了代码,使所有内容都从0开始到h-1或w-1。我还在color()中添加了一个条件所以它检查x1 1 0 0 1 0 0 0 0 0 1 1 1 1 1 1 1 0 1 0 1 0 0 1 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 0 1 1 0 1 1 0 1 1 1 0 1 0 0 1 1 1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 1 0 1 1 0 0 0 0 0 0 1 0 0 1 0 1 1 0 0 0 1 1 1 1 1 0 0 0 1 0 1 0 0, 0, 2, 32 8, 1, 3, 1 9, 2, 4, 1 2, 3, 5, 1 1, 6, 6, 8 3, 7, 7, 1 7, 7, 8, 6 3, 9, 9, 2 6, 10, 10, 1 2 2 0 0 2 0 0 0 0 0 2 2 2 2 2 2 2 0 3 0 2 0 0 2 0 0 2 0 0 4 2 0 5 0 2 0 2 2 2 0 2 2 0 2 2 0 2 2 2 0 2 0 0 2 2 2 2 2 2 0 0 6 0 0 0 0 0 0 0 0 0 6 0 7 0 0 0 8 8 0 6 6 0 0 0 0 0 0 8 0 0 6 0 9 9 0 0 0 8 8 6 6 6 0 0 0 10 0 8 0