C#在2D大数组中查找2D小数组
如何在大型2D阵列中搜索小型2D阵列?根据阵列的大小和您的要求,简单的暴力算法可能就足够了 从大数组中的[0,0]开始,检查此坐标上的所有项目是否与小数组中的项目相等。C#在2D大数组中查找2D小数组,c#,arrays,multidimensional-array,C#,Arrays,Multidimensional Array,如何在大型2D阵列中搜索小型2D阵列?根据阵列的大小和您的要求,简单的暴力算法可能就足够了 从大数组中的[0,0]开始,检查此坐标上的所有项目是否与小数组中的项目相等。 如果他们是你已经找到了匹配的。如果没有,您将转到大数组中的下一个位置。创建一个助手方法,检查大数组是否包含从给定位置开始的小数组(行,列)。制作两个嵌套循环,在小数组可以容纳的大数组中的所有(行、列)对上迭代,并查看是否有任何对会产生匹配 int[,] data = new int[,] { { 0, 0
如果他们是你已经找到了匹配的。如果没有,您将转到大数组中的下一个位置。创建一个助手方法,检查大数组是否包含从给定位置开始的小数组
(行,列)
。制作两个嵌套循环,在小数组可以容纳的大数组中的所有(行、列)
对上迭代,并查看是否有任何对会产生匹配
int[,] data = new int[,] {
{ 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0 },
{ 0, 1, 1, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
};
int[,] find = new int[,] {
{ 0, 1 },
{ 1, 1 }
};
bool result = Check2DArray (data, find);
start\e
)start_e
从大数组复制小数组大小的子数组,然后您可以尝试以下代码段:
static bool EqualAtPosition(int[,] big, int[,] small, int row, int col) {
var rowCount = small.GetLength(0);
var colCount = small.GetLength(1);
if (row+rowCount > big.GetLength(0) || col+colCount > big.GetLength(1)) {
return false;
}
for (var r = 0 ; r != rowCount ; r++) {
for (var c = 0 ; c != colCount ; c++) {
if (big[row+r, col+c] != small[r, c]) {
return false;
}
}
}
return true;
}
static bool Check2DArray(int[,]数据,int[,]查找)
{
int dataLen=data.Length;//整个数据的长度
int findLen=find.Length;//整个查找的长度
for(int i=0;i=data.GetLength(0)| | checkedY>=data.GetLength(1))
{
//我们在数据边界之外
//将此数据行和列的标志设置为false并中断检查
好=假;
打破
}
//我们仍在数据边界内,因此请检查值是否匹配
OK=data[dataY+findY,dataX+findX]==find[findY,findX];//检查是否匹配
}
if(ok)//如果两个片段中的所有值都相等
return true;//返回true
}
返回false;
}
你可以查一下
请给我一些反馈,如果不够清楚,我可以详细解释:)
编辑:发现此方法未检查
data
参数中的最后一行和最后一列的问题。现在,经过小的修复,所有的工作都很完美
EDIT2:
谢谢你指出我的错误。某些计算存在问题,可能导致
索引自动失效
异常和比较错误输入。现在一切都正常了。基本上,你只需循环所有较小数组可以放入较大数组的“开始”位置,然后循环较小数组的值,并与较大数组中的相对位置进行比较,如果没有匹配,你需要继续到较大数组中的下一个位置,或者,如果所有匹配项都匹配,则可以返回true
,如果完成了较大数组的搜索,但没有找到匹配项,则返回false
static bool Check2DArray(int[,] data, int[,] find)
{
int dataLen = data.Length; // length of the whole data
int findLen = find.Length; // length of the whole find
for(int i = 0; i < dataLen; i++) // iterate through data
{
int dataX = i % data.GetLength(0); // get current column index
int dataY = i / data.GetLength(0); // get current row index
bool okay = true; // declare result placeholder for that check
for (int j = 0; j < findLen && okay; j++) // iterate through find
{
int findX = j % find.GetLength(1); // current column in find
int findY = j / find.GetLength(1); // current row in find
int checkedX = findX + dataX; // column index in data to check
int checkedY = findY + dataY; // row index in data to check
// check if checked index is not going outside of the data boundries
if ( checkedX >= data.GetLength(0) || checkedY >= data.GetLength(1))
{
// we are outside of the data boundries
// set flag to false and break checks for this data row and column
okay = false;
break;
}
// we are still inside of the data boundries so check if values matches
okay = data[dataY + findY, dataX + findX] == find[findY, findX]; // check if it matches
}
if(okay) // if all values from both fragments are equal
return true; // return true
}
return false;
}
publicstaticboolcheck2darray(int[,]数据,int[,]查找)
{
for(int-dRow=0;dRow
如果您愿意,还可以使用@dasblinkenlight的解决方案替换内部的两个循环。这项任务没有银弹。只要为循环编写两个代码,就完成了。如果其中一个匹配,它就匹配了底线,但我可以not@gdir更像是4。列和行是什么意思?二维数组不足以进行操作?@SefaTunçkanat
equalaposition
检查一个位置是否匹配。您可以从行和列上的两个嵌套循环调用此方法来查找重叠的位置。很好的编译器,感谢您的答案和注释行,但是当我乘以搜索值时,我得到一个错误int[,]data=new int[,]{{0,0,0,0,0,0,0},{0,0,1,0,0},{0,1,1,0,0}, { 0, 0, 1, 0, 0 }, { 0, 0, 1, 0, 0 }, }; int[,]find=newint[,]{0,1},{1,1},{0,1},{0,1},{0,1}代码>@SefaTunçkanat是的,我这边有个错误。检查编辑的答案。还编辑了联机检查的链接,其中包含您发布的值comment@SefaTunçkanat这花费了比预期更长的时间,但现在应该可以正常工作了。再次感谢你找到我并指出我的错误。
public static bool Check2DArray(int[,] data, int[,] find)
{
for (int dRow = 0; dRow < data.GetLength(0) - find.GetLength(0); dRow++)
{
for (int dCol = 0; dCol < data.GetLength(1) - find.GetLength(1); dCol++)
{
bool found = true;
for (int fRow = 0; fRow < find.GetLength(0); fRow++)
{
for (int fCol = 0; fCol < find.GetLength(1); fCol++)
{
if (data[dRow + fRow, dCol + fCol] != find[fRow,fCol])
{
found = false;
break;
}
}
if (!found) break;
}
if (found) return true;
}
}
return false;
}