C# 使用泛洪填充算法计算0的数量
我想用泛光填充算法从2d数组中计算0和1的数目……但不幸的是……它显示了错误的结果 我有一个这样的矩阵C# 使用泛洪填充算法计算0的数量,c#,algorithm,flood-fill,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() {
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;
}
int[][]输入;
公共静态int[,]reult;
公共静态整数计数=0,列,行;
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
字符串路径;
OpenFileDialog文件=新建OpenFileDialog();
if(file.ShowDialog()==DialogResult.OK)
{
输入=File.ReadLines(File.FileName)
.Skip(0)
.选择(l=>l.Split(','))
.Select(n=>int.Parse(n))
.ToArray())
.ToArray();
}
reult=JaggedtomMultidimensional(输入);
int p=reult.GetLength(0);
int q=reult.GetLength(1);
row=p-1;
col=q-1;
int-one=p*q;
int zero=应用(行、列);
标签1.Text=”“+零;
label2.Text=“”+(一-零);
}
公共T[,]JaggedTo多维(T[][]jaggedArray)
{
int rows=jaggedArray.Length;
int cols=jaggedArray.Max(subArray=>subArray.Length);
T[,]数组=新的T[行,列];
对于(int i=0;i0),则应用(x,y-1);
}
返回计数;
}
私有静态int getValueAt(int x,int y)
{
if(x<0 | | y<0 | | x>行| | y>列)
{
返回-1;
}
其他的
{
返回reult[x,y];
}
}
私人静态无效访问(整数x,整数y)
{
reult[x,y]=1;
}
在洪水填充算法中,您只能向四个方向移动,并覆盖符合条件的区域。幸运的是,[row,col]
索引有0
,它从[row,col]
中计算出所有四个0。现在想想如果应用(行,列)
在行,列
索引上有1
会怎么样
为了正确处理这个问题,您需要在整个矩阵中循环并调用apply(i,j)
换行
int zero = apply(row, col);
到
intzero=0;
对于(int i=0;i给定要求,您应该更改搜索条件以搜索0和1
您需要做的是在矩形内搜索,以便矩形的边界限制搜索
i、 e
int[]计数={0,0};
专用静态整型应用(整型x,整型y)
{
int currentColor=getValueAt(x,y);
如果(currentColor!=-1)
{
访问(x,y);
计数[当前颜色]+;
如果(x0),则应用(x,y-1);
}
返回计数;
}
然后更改您的访问功能,将单元格设置为-1,以避免访问两次。如果您愿意,也可以使用linq(实际上我更喜欢):
它将为您提供以下结果:
[0]{val=“0”,计数=10}[1]{val=“1”,计数=15}
这是整体填充,因此它只访问给定参数内的相邻单元(在本例中,currentColor==0
).你从右下角开始,该单元格的组中有4个0。你的算法正在做它应该做的事情,而你只是误解了结果。那么我该怎么办?你的目标是使用泛洪填充还是计算1和0?使用泛洪填充算法计算0和1…这是一个大学prjct…我的主管刚才对你说的我们询问如何使用泛洪填充算法遍历整个矩阵,但泛洪填充并不是这样做的。它只会根据您的条件访问特定区域,因此它访问整个矩阵的唯一方式是如果您的整个矩阵为0。如果您的目标只是计算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);
}
}
}
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;
}
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();
}