使用floodfill算法计数0

时间:2016-10-16 23:23:27

标签: c# algorithm flood-fill

我想用洪水填充算法从2d数组中计算0和1的数量....但不幸的是......它显示错误的结果。

我有一个像这样的矩阵

0,1,1,0,1

1,0,1,1,0

1,0,1,1,0

1,0,1,1,0

1,0,1,1,0

它应该显示0 = 10和1 = 15

的数字

但它显示的数字为0 = 4和1 = 21

这是我的代码

int[][] input;
public static int[,] reult;
public static int count = 0,col,row;
public Form1()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    string path;
    OpenFileDialog file = new OpenFileDialog();
    if (file.ShowDialog() == DialogResult.OK)
    {

        input = File.ReadLines(file.FileName)
                .Skip(0)
                .Select(l => l.Split(',')
                    .Select(n => int.Parse(n))
                    .ToArray())
                    .ToArray();

    }

    reult = JaggedToMultidimensional(input);

    int p = reult.GetLength(0); 
    int q = reult.GetLength(1); 
    row = p-1;
    col = q - 1;
    int one = p * q;
    int zero = apply(row, col);
    label1.Text = "" + zero;
    label2.Text = "" + (one - zero);

}

public T[,] JaggedToMultidimensional<T>(T[][] jaggedArray)
{
    int rows = jaggedArray.Length;
    int cols = jaggedArray.Max(subArray => subArray.Length);
    T[,] array = new T[rows, cols];
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            array[i, j] = jaggedArray[i][j];
        }
    }
    return array;
}

private static int apply(int x, int y)
{
    int currentColor = getValueAt(x, y);
    if (currentColor == 0)
    {
        visit(x, y);
        count++;

        if (x < row) apply(x + 1, y);
        if(y<col) apply(x, y + 1);
       if(x>0) apply(x - 1, y);
       if (y>0) apply(x, y - 1);
    }
    return count;
}

private static int getValueAt(int x, int y)
{
    if (x < 0 || y < 0 || x > row || y > col)
    {
        return -1;
    }
    else
    {
        return reult[x,y];
    }
}

private static void visit(int x, int y)
{
    reult[x,y] = 1;
}

3 个答案:

答案 0 :(得分:1)

int zero = apply(row, col);

在洪水填充算法中,您只能向四个方向前进并覆盖符合条件的区域。幸运的是,[row,col]索引有0,它从[row, col]计算所有四个0。现在想一想,如果apply(row,col)1索引上有row, col该怎么办。

要做到这一点,你需要循环遍历整个矩阵,并在找到apply(i,j)

的地方调用array[i,j]==0

更改此行

int zero = apply(row, col);

int zero = 0;
for(int i=0; i<=row; i++)
{
   for(int j=0; j<=col; j++)
   {
       if(array[i][j]==0)
       {
          count =0;
          zero+= apply(row, col);
       }
   }
}

希望这有帮助。

答案 1 :(得分:0)

根据要求,您应更改搜索条件以搜索0和1。

您需要做的是在矩形内搜索,以便矩形的边框限制搜索。

int[] count = {0,0};

private static int apply(int x, int y)
{
    int currentColor = getValueAt(x, y);
    if (currentColor != -1)
    {
        visit(x, y);
        count[currentColor]++;

        if (x < row) apply(x + 1, y);
        if(y<col) apply(x, y + 1);
       if(x>0) apply(x - 1, y);
       if (y>0) apply(x, y - 1);
    }
    return count;
}

然后更改您的访问函数以将单元格设置为-1,以避免访问它两次。

答案 2 :(得分:0)

你也可以使用linq(我实际上更喜欢):

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
@try
{
    for (UIControl *subview in cell.contentView.subviews) {
        [subview removeFromSuperview];
    }
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    NSString *storedValue, *currValue;
    UILabel *lbl        = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 44)];
    switch (headerTag)
    {
        case 0:
        {
           //conditional code
        }
        break;

        case 1:
        {
            //conditional code
        }
        break;

        case 2:
        {
            //conditional code
        }
        break;

        .
        .
        .
        .

        default:
        break;
    }

    lbl.lineBreakMode   = NSLineBreakByTruncatingMiddle;
    lbl.textColor       = PRIMARY_LABEL_COLOR;
    lbl.backgroundColor = indexPath.row & 1 ? DROPDOWN_SECOND_COLOR : DROPDOWN_FIRST_COLOR ;
    [cell.contentView addSubview:lbl];

    if([currValue isEqualToString:stringvalue])
    {
        UILabel *lblTick = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 44, 44)];
        [lblTick setTextColor:PRIMARY_LABEL_COLOR];
        [lblTick setBackgroundColor:[UIColor clearColor]];
        [lblTick setFont:[UIFont systemFontOfSize:30]];
        [lblTick setText:@"\u2713"];
        [cell.contentView addSubview:lblTick];
    }
}
@catch (NSException *exception) {
    CATCH_NSException
}
return cell;
}

它会给你以下结果:

  

[0] {val =“0”,count = 10} [1] {val =“1”,count = 15}

相关问题