简体   繁体   English

确定2D数组中所有相等且彼此相邻的值

[英]Determining all values that are equal & next to one another in a 2D array

I'm new to stackoverflow as well as programming so pardon me if I ask this question poorly. 我对stackoverflow和编程都是陌生的,所以请问我这个问题不好的话。

So say I have a 2-D array with integer values from 1 through 4. It might look something like this: 假设我有一个二维数组,其整数值为1到4。它可能看起来像这样:

3 3 3 1 3 1 3 1 2 1
1 3 1 3 3 1 1 1 1 1
1 4 3 3 1 3 3 4 3 4
1 4 1 1 3 3 1 4 2 4
1 1 1 4 1 3 3 1 1 3
1 2 3 3 3 3 3 3 1 1
4 1 4 3 3 2 1 1 4 1
1 3 3 3 4 1 4 2 2 3

Let me rewrite it so that I can isolate a part of the array I want us to observe: 让我重写它,以便可以隔离要我们观察的数组的一部分:

x x x x x x x x x x
x x x x x x x x x x
x x x x x 3 3 x x x
x x x x 3 3 x x x x
x x x x x 3 3 x x x 
x x 3 3 3 3 3 3 x x
x x x 3 3 x x x x x
x 3 3 3 x x x x x x

We see that the 3's shown here are not only all equal in value but also directly "next to" one another and are all "connected". 我们看到,这里显示的3不仅价值相等,而且彼此之间直接“相邻”,并且全部“相连”。

I have 4 methods that determine whether or not the space to the right, left, top, or bottom of a space in the array is equal to a given start position: 我有4种方法来确定数组中空格的右边,左边,顶部或底部的空间是否等于给定的起始位置:

boolean isEqualRight(int row, int column) {
    return array[row][column] == array[row][column + 1];
}
boolean isEqualLeft(int row, int column) {
    return array[row][column] == array[row][column - 1];
}
boolean isEqualUp(int row, int column) {
    return array[row][column] == array[row - 1][column];
}
boolean isEqualDown(int row, int column) {
    return array[row][column] == array[row + 1][column];
}

But, even if I know if a position in the array is equal and next to a start position, I can't think of a solution to determine all of the positions in the array that are equal and connected to the start. 但是,即使我知道数组中的位置是否相等并且在起始位置旁边,我也无法想到一种确定数组中所有相等并连接到起始位置的解决方案。

I tried developing a loop that would cycle through a space next to a starting 3, determine if it is next and equal to it, and if so then do the same for the 3 that is next to it, and so on, and so on. 我尝试开发了一个循环,该循环将在起始3旁边的空间中循环,确定它是否是下一个并等于它,然后对旁边的3进行相同的操作,依此类推,依此类推。 But I faced an issue when the 3's started branching. 但是当3开始分支时,我遇到了一个问题。 Any thoughts? 有什么想法吗?

This is a standard problem in computer graphics. 这是计算机图形学中的标准问题。 The algorithm is called Flood Fill and is covered in detail in Wikipedia . 该算法称为“洪水填充”,在Wikipedia中进行了详细介绍。

There are also many Java implementations out there if you wanted to study one (just google). 如果您想研究一种(只是google),也有很多Java实现。

Your methods seem correct but you should add come code to catch any possible IndexOutOfBoundErrors. 您的方法看似正确,但是您应该添加代码以捕获任何可能的IndexOutOfBoundErrors。 For example if you take the first element in your array and check up or left you will get an IndexOutOfBound Error as it will reference an element that does not exist, this goes for all boundary elements. 例如,如果您获取数组中的第一个元素并向上或向左检查,则会出现IndexOutOfBound错误,因为它将引用不存在的元素,这适用于所有边界元素。 This will prevent your code from running efficiently. 这将阻止您的代码有效运行。

Just something to always consider when dealing with arrays. 这是处理数组时始终要考虑的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM