用泛洪填充算法计数0的个数

本文关键字:泛洪 填充 算法 | 更新日期: 2023-09-27 18:13:22

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

我有一个这样的矩阵

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;
}

用泛洪填充算法计数0的个数

int zero = apply(row, col);

在您的洪水填充算法中,您只向四个方向移动并覆盖符合您的标准的区域。幸运的是,[row,col]索引有0它从[row, col]中计算出四个0。现在想想如果apply(row,col)row, col索引上有1会怎样。

要得到正确的结果,你需要遍历整个矩阵并在找到array[i,j]==0的地方调用apply(i,j)

改变这一行

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);
       }
   }
}

给定需求,您应该更改搜索条件以搜索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,以避免访问两次。

如果你愿意,你也可以使用linq(实际上我更喜欢):

OpenFileDialog fileDialog = new OpenFileDialog();
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                var lines = File.ReadLines(fileDialog.FileName);
                var splittedValues = lines.Where(l => !string.IsNullOrEmpty(l)).Select(l => l.Split(',')).SelectMany(l => l).ToList();
                var valCount = splittedValues.GroupBy(s => s).Select(s => new { val = s.Key, count = s.Count() }).ToList();     
            }

结果如下:

[0] {val = " 0 ",数= 10}[1]{val ="1",数= 15}