【发布时间】:2017-12-16 14:24:55
【问题描述】:
我使用以下方法根据给定索引(行和列)检查特定单元格周围的单元格是否为零,请参阅此数组:
我需要检查零周围的单元格(水平、垂直或对角线)可以位于第一行、最后一行、第一列、最后一列或介于两者之间,即它可以是矩形阵列中的任何单元格,具体取决于到传递给方法的“rowIndex”和“colIndex”。
static Boolean TestZero(int[,] array,int colIndex, int rowIndex)
{
/*Check Corners*/
//First Corner
if ((rowIndex == 0) && (colIndex == 0))
{
if (array[1, 0] == 1 || array[0, 1] == 1 || array[1, 1] == 1) return false;
}
//Second Corner
if ((rowIndex == 0) && colIndex >= array.GetUpperBound(0))
{
if (array[array.GetUpperBound(0) - 1, 0] == 1 || array[array.GetUpperBound(0),1] == 1 || array[array.GetUpperBound(0)-1,1 ] == 1) return false;
}
//Third Corner
if ((rowIndex >= array.GetUpperBound(1)) && (colIndex == 0))
{
if (array[0, array.GetUpperBound(1) - 1] == 1 || array[1, array.GetUpperBound(1)] == 1 || array[1, array.GetUpperBound(1)-1] == 1) return false;
}
//Fourth Corner
if ((rowIndex >= array.GetUpperBound(1)) && (colIndex >= array.GetUpperBound(0)))
{
if (array[array.GetUpperBound(0), array.GetUpperBound(1) - 1] == 1 || array[array.GetUpperBound(0) - 1, array.GetUpperBound(1) - 1] == 1 || array[array.GetUpperBound(0) -1, array.GetUpperBound(1)] == 1) return false;
}
/* Check Boundries But Not Corners */
//First Row
if ((rowIndex == 0) && (colIndex != array.GetUpperBound(0)) && (colIndex != 0))
{
for (int i = rowIndex; i <= rowIndex + 1; i++)
{
for (int j = colIndex - 1; j <= colIndex + 1; j++)
{
if ((i != rowIndex) && (j != colIndex))
{
if (array[j,i] == 1) return false;
}
}
}
}
//Last Row
if ((rowIndex >= array.GetUpperBound(1)) && (colIndex != array.GetUpperBound(0)) && (colIndex != 0))
{
for (int i = rowIndex; i <= rowIndex - 1; i--)
{
for (int j = colIndex - 1; j <= colIndex + 1; j++)
{
if ((i != rowIndex) && (j != colIndex))
{
if (array[j,i] == 1) return false;
}
}
}
}
//First & Last Columns
if ((rowIndex != array.GetUpperBound(1)) && ((rowIndex != 0)))
{
//First column
if(colIndex==0)
{
for (int i = rowIndex-1; i <= rowIndex + 1; i++)
{
for (int j = colIndex; j <= colIndex + 1; j++)
{
if ((i != rowIndex) && (j != colIndex))
{
if (array[j,i] == 1) return false;
}
}
}
}
//Last Column
if (colIndex == array.GetUpperBound(0))
{
for (int i = rowIndex -1; i <= rowIndex + 1; i++)
{
for (int j = colIndex; j <= colIndex - 1; j--)
{
if ((i != rowIndex) && (j != colIndex))
{
if (array[j,i] == 1) return false;
}
}
}
}
}
/* In Between i.e. Not the Array Boundries */
if(colIndex!=0 && colIndex != array.GetUpperBound(0) && rowIndex !=0 && rowIndex != array.GetUpperBound(1)) {
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[j,i] == 1) return false;
}
}
}
} // end if statment
return true;
}
我得到了一些错误的结果,我试图找出问题所在,但我不能!。
结果:
1- 矩形阵列(遗传算法群体中的染色体):
2- 我们需要检查其周围环境的单元格的索引:
|(2,3)||(2,3)||(0,1)||(1,3)||(0,3)||(1,3)|
3- 在每个预期单元格的至少一个周围单元格中包含零的数组:
0 号染色体:真 1 号染色体:真 2 号染色体:假 3号染色体:真 4号染色体:假5号染色体:真
任何帮助弄清楚为什么我得到了一些错误的结果!!。
【问题讨论】:
-
您是否尝试过在调试器中单步执行?除非你非常小心,否则这已经有点太复杂了,无法“用肉眼”分析。概括和简化逻辑会有所帮助(例如,创建一个像
GetSafe这样的方法来进行边界检查,这样您就可以安全地进行GetSafe(-1, 1)而不是进行另一个显式的 if 检查)。它还允许您创建一个简单的相对位置数组来检查和循环,而不是执行if (GetSafe(...) && GetSafe(...) && GetSafe(...) && ...)或其他任何操作。 -
@Kushina,这是 ACM 问题吗?如果不是(或者如果您不关心性能),请使用 try-catch 处理边界情况,并使您的代码更易于调试。 (与@Luaan 类似的想法)
-
@Kushina,好的,看看我的答案,我现在要测试更多的案例。但请注意,它依赖于异常机制,这意味着性能不佳。
标签: c# arrays loops multidimensional-array