Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中,如何相对于矩阵中其他行中的其他元素标记行中的元素#_C#_Arrays_Loops_Multidimensional Array - Fatal编程技术网

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行)你能加上它给你的错误结果吗?它可能会帮助我们帮助你。