【问题标题】:FloodFill Function call not workingFloodFill 函数调用不起作用
【发布时间】:2015-02-23 08:53:11
【问题描述】:

我必须第一次使用洪水填充算法来解决家庭作业中的任务。主要问题是我写的flood fill函数的调用好像不行。

我的任务与此处描述的任务非常相似: How can I find hole in a 2D matrix?

我使用了这里的算法:http://www.codeproject.com/Articles/6017/QuickFill-An-efficient-flood-fill-algorithm,并根据我的需要对其进行了调整。

例如,我有这个矩阵:

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”(填充函数)成为“种子”。

我尝试使用 gdb 查找错误 - 什么也没找到。 我在代码中添加了一些 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);
        }
    }
}

以及main函数中的调用:

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
    }

我是初学者,以前从未使用过填充。我什至不确定出了什么问题:洪水填充代码,函数的调用,或者我在函数中传递矩阵 sim[][] 的方式。怎么了?

【问题讨论】:

  • 什么是m?您已使用 m 为数组的宽度和高度分配内存,但您示例中的数组不是正方形的。我将为h 行分配内存,每行宽度为w。我会使用将数组索引为 0..(h-1) 和 0..(w-1) 的循环。在color() 函数中,我会检查索引边界,因为递归可以将索引传递到数组边界之外。
  • 一般来说,请警惕您从互联网上复制的所有示例。这同样适用于codproject.com。您发现的许多示例都是不正确的,或者包含重大错误。
  • m 是矩阵可以拥有的最大行数和列数。最初我为 h 行和 w 列分配了内存,但我遇到了分段错误(核心转储)......我用 gdb 进行了检查,这给了我类似的信息:“3996 malloc.c:没有这样的文件或目录。”它仅在我分配 m 时才有效。我不要t know why it doesnt 让我分配更少。
  • 可能是因为您没有检查数组边界。 BTW color() 应该返回一个值。
  • 我更改了代码,使所有内容都从 0 开始到 h-1 或 w-1。我还在 color() 中添加了一个条件,以便它检查 x

标签: c


【解决方案1】:

OP 代码中有许多错误,例如使用交换的 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;
}

程序输出。

1  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

【讨论】:

    猜你喜欢
    • 2015-08-19
    • 1970-01-01
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多