使用我的DFS计算连接块的错误结果

时间:2016-04-21 13:09:41

标签: c matrix output depth-first-search

我有一个随机生成的5 x 5矩阵,由1和0组成,我应该找到1的块。

MessageAndMetadataFunction.call是一个块本身,如果它从任何方向连接到另一个1,我的算法应该找到所有连接的1并将它们全部计为一个块。

问题是我无法获得一致的结果,有时候计数器会转到1

这是actual code.

这是主要的,我删除了随机化功能,只是放置了一个程序遇到问题的数组。

-1

DFS函数应该标记1,它在任何方向找到

struct parent {
    int row;
    int col;
}

int ar[5][5] = { 0,0,0,0,1,
                 1,0,0,0,1,
                 0,0,0,0,0,
                 0,0,0,0,0,
                 0,0,0,0,0 };
int countblock=0,num=0;
struct parent pos[30] = { 0 };
srand(time(NULL));
printf("      ---Find the connected blocks---     \n\n");
printf("Creating a random array:\n");
//random5x5(ar);
for (int row = 0, col; row < 5; row++)
{
    for (col = 0; col < 5; col++)
    {
        if (ar[row][col] == 1)
        {
            countblock++;
            ar[row][col] = -1;
            flag = 0;
            check(ar, row, col, pos, &num);
        }
        printf("%d ", ar[row][col]);  // Had problems with this loop not checking 1's
    }  //So I printed them to try to make sure
    printf("\n");
}
printf("\n\n Found the connected blocks! I see only %d of them.", countblock);
return 0;

编辑:这是错误

int flag=0; // declared in global

void check(int ar[5][5], int row, int col, struct parent pos[30],int *num){
if (col - 1 >= 0 && row - 1 >= 0)   //Checking Upper-left
{
    if (ar[row - 1][col - 1] == 1) {
        ar[row - 1][col - 1] = -1;
        (*num)+=1;
        pos[*num].row = row; pos[*num].col = col; //Store this position
        check(ar, row - 1, col - 1, pos, num);  //Jump to the found 1
    }
}
if (col - 1 >= 0)       //Checking Left
{
    if (ar[row][col - 1] == 1) {
        ar[row][col - 1] = -1;
        (*num)+=1;
        pos[*num].row = row; pos[*num].col = col;  //Store this position
        check(ar, row, col - 1, pos, num);  //Jump to the found 1
    }
}
if (col - 1 >= 0 && row + 1 <= 4)   //Checking Bottom-Left
{
    if (ar[row + 1][col - 1] == 1) {
        ar[row + 1][col - 1] = -1;
        (*num)+=1;
        pos[*num].row = row; pos[*num].col = col; //
        check(ar, row + 1, col - 1, pos, num); //
    }
}
if (row + 1 <= 4)       //Checking Bottom
{
    if (ar[row + 1][col] == 1) {
        ar[row + 1][col] = -1;
        (*num)+=1;
        pos[*num].row = row; pos[*num].col = col; //
        check(ar, row + 1, col, pos, num); //
    }
}
if (col + 1 <= 4 && row + 1 <= 4)   //Checking Bottom-Right
{
    if (ar[row + 1][col + 1] == 1) {
        ar[row + 1][col + 1] = -1;
        (*num)+=1;
        pos[*num].row = row; pos[*num].col = col; //
        check(ar, row + 1, col + 1, pos, num); //
    }
}
if (col + 1 <= 4)       //Checking Right
{
    if (ar[row][col + 1] == 1) {
        ar[row][col + 1] = -1;
        (*num)+=1;
        pos[*num].row = row; pos[*num].col = col; //
        check(ar, row, col + 1, pos, num); //
    }
}
if (col + 1 <= 4 && row - 1 >= 0)      //Checking Upper-Right
{
    if (ar[row - 1][col + 1] == 1) {
        ar[row - 1][col + 1] = -1;
        (*num)+=1;
        pos[*num].row = row; pos[*num].col = col; //
        check(ar, row - 1, col + 1, pos, num); //
    }
}
if (row - 1 >= 0)  //Checking Up
{
    if (ar[row - 1][col] == 1) {
        ar[row - 1][col] = -1;
        (*num)+=1;
        pos[*num].row = row; pos[*num].col = col; //
        check(ar, row - 1, col, pos, num); //
    }
}
if (*num == 0)  //There were no 1's around
    return;
else  //Reached the end of the path, call back
{
    flag++;
    if (flag >= 2) //On first call back the pos is still on parent call
        (*num)-=1;  // On second call it should go back
    check(ar, pos[*num].row, pos[*num].col, pos, num);
}

通过制作

来修复它
flag++;
    if (flag >= 2) //On first call back the pos is still on parent call
        (*num)-=1;  // On second call it should go back
    check(ar, pos[*num].row, pos[*num].col, pos, num);

基本上* num会在flag++; if (flag >= 2) (*num)-=1; if (*num == 0) return; check(ar, pos[*num].row, pos[*num].col, pos, num); 中变为零并调用错误的父位置。 再做一次检查就可以了。

1 个答案:

答案 0 :(得分:0)

您在检查范围与您检查的邻居之间存在一些不匹配。例如(这不是唯一一个)看一下:

if (row + 1 <= 4)       //Checking Bottom
{
    if (ar[row][col + 1] == 1) {
        ar[row][col + 1] = -1;
        (*num)+=1;
        pos[*num].row = row; pos[*num].col = col; //
        check(ar, row, col + 1, pos, num); //
    }
}

您检查row+1是否在范围内......但是您会在col+1上递归。

这种错误可能导致内存损坏(因为你最终写到你并不意味着的位置),这几乎可以使任何事情发生: - )。

如果您仔细检查这种不匹配情况,找到您找到的任何内容,然后重试,会发生什么?