C# 在C中,如何相对于矩阵中其他行中的其他元素标记行中的元素#
我有以下矩形阵列:C# 在C中,如何相对于矩阵中其他行中的其他元素标记行中的元素#,c#,arrays,loops,multidimensional-array,C#,Arrays,Loops,Multidimensional Array,我有以下矩形阵列: 我需要检查是否有任何单元格被所有边的零包围:水平、垂直和对角线,如示例中所示 这里有一个更好的方法 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args)
我需要检查是否有任何单元格被所有边的零包围:水平、垂直和对角线,如示例中所示 这里有一个更好的方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[,] chromosome = {
{1,0,1,1,1,0,1,1,1,1,},
{1,0,1,1,1,1,1,1,1,0,},
{1,0,0,0,0,1,0,0,0,0,},
{0,1,0,1,0,1,0,1,1,1,},
{1,1,1,1,0,0,1,1,1,1,},
{1,1,1,1,0,1,1,1,0,0,},
{1,1,0,0,0,0,0,1,1,1,},
{1,0,1,1,0,0,0,1,1,0,},
{1,1,0,0,0,0,0,0,0,0,},
{0,0,0,1,0,0,0,1,0,0,}
};
Boolean results11 = TestZero(chromosome, 1, 1);
Boolean results85 = TestZero(chromosome, 8, 5);
}
static Boolean TestZero(int[,] array, int rowIndex, int colIndex)
{
if((rowIndex <= 0) || (rowIndex >= array.GetUpperBound(0)) || (colIndex <= 0) || (colIndex >= array.GetUpperBound(1))) return false;
for(int i = rowIndex - 1; i <= rowIndex + 1; i++)
{
for(int j = colIndex - 1; j <= colIndex + 1; j++)
{
if((i != rowIndex) && (j != colIndex))
{
if(array[i,j] == 1) return false;
}
}
}
return true;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
int[,]染色体={
{1,0,1,1,1,0,1,1,1,1,},
{1,0,1,1,1,1,1,1,1,0,},
{1,0,0,0,0,1,0,0,0,0,},
{0,1,0,1,0,1,0,1,1,1,},
{1,1,1,1,0,0,1,1,1,1,},
{1,1,1,1,0,1,1,1,0,0,},
{1,1,0,0,0,0,0,1,1,1,},
{1,0,1,1,0,0,0,1,1,0,},
{1,1,0,0,0,0,0,0,0,0,},
{0,0,0,1,0,0,0,1,0,0,}
};
布尔结果11=TestZero(染色体,1,1);
布尔结果85=TestZero(染色体8,5);
}
静态布尔TestZero(int[,]数组,int行索引,int colIndex)
{
if((rowIndex=array.GetUpperBound(0))| |(colIndex=array.GetUpperBound(1)))返回false;
对于(inti=rowIndex-1;i),这是一场噩梦。您需要学习如何将代码划分为小函数!比如说ProcessNeighbor(introw,intcol)
。有一条经验法则:一段代码不应该超过一个或两个屏幕。(有人说是5-10行)你能加上它给你的错误结果吗?它可能会帮助我们帮助你。