英文:
How to get all the neihbors neighbors of a point in a 2d array?
问题
public void getNeighborsOfPoint(int x, int y) {
for (int xx = -1; xx <= 1; xx++) {
for (int yy = -1; yy <= 1; yy++) {
if (xx == 0 && yy == 0) {
continue; // You are not a neighbor to yourself
}
if (isOnMap(x + xx, y + yy)) {
System.out.print(grid[x + xx][y + yy] + " ");
}
}
}
}
public boolean isOnMap(int x, int y) {
return x >= 0 && y >= 0 && x < length && y < width;
}
英文:
I am trying to get all the neighbors of a combination of simple one character strings in a 2d array. Meaning, my output looks like this currently in a 3x5:
A B C
D E F
A S D
F S A
G S A
So the neighbor of (1,0) should be = A B E S A .
Currently I have the following:
public void getNeighborsOfPoint(int x, int y) {
for (int xx = -1; xx <= 1; xx++) {
for (int yy = -1; yy <= 1; yy++) {
if (xx == 0 && yy == 0) {
continue; // You are not neighbor to yourself
}
if (Math.abs(xx) + Math.abs(yy) > 1) {
continue;
}
if (isOnMap(x + xx, y + yy)) {
System.out.println(grid[x+xx][y+yy]);
}
}
}
public boolean isOnMap(int x, int y) {
return x >= 0 && y >= 0 && x < length && y < width;
}
However it is only returning A E A in the example I provided.(it is not returning the ones cross-wise)
What is the right code to make it work? Note that the input will not always be 3 x 5. It may be a lot of different combination of x and y s.
答案1
得分: 2
对角线没有被包括进来,因为有了这段代码:
if (Math.abs(xx) + Math.abs(yy) > 1) {
continue;
}
当它在对角线上时,Math.abs(xx) == 1 && Math.abs(yy) == 1
。因此它们的和将大于1。通过将此代码放在这里,您正在跳过对角线。
英文:
The diagonals aren't included because of this code:
if (Math.abs(xx) + Math.abs(yy) > 1) {
continue;
}
When it's on the diagonal Math.abs(xx) == 1 && Math.abs(yy) == 1
. So their sum will be greater than 1. You're skipping over the diagonals by having this code here.
答案2
得分: 0
你的当前组中未获得对角线的原因在于第二个 if
语句。你需要包括,例如,(2, 1)
,此时 xx
为 1
,yy
为 1
。但是 abs(1) + abs(1) = 2
,并且 2 > 1
,所以你没有将它包括进去。
作为重构练习,如果你可以将循环体内部简化为一个条件语句,代码可能会更加清晰。
if (expression) {
continue
};
// 其他操作
等同于
if (!expression) {
// 其他操作。
}
对于你来说,expression
(伪代码中)为 not(xx=0 and yy=0) and isOnMap(xx, yy)
。
英文:
The reason you're not getting the diagonals in your current group is that second if
statement. You need to cover, for example, (2, 1)
which is when xx
is 1
and yy
is 1
. But abs(1) + abs(1) = 2
and 2 > 1
, so you don't include it.
As a refactoring exercise, it might be a little cleaner if you have the inside of that for loop simplified to one conditional.
if (expression) {
continue
};
// other stuff
is equivalent to
if (!expression) {
// other stuff.
}
And for you, expression
(in psuedocode) is not(xx=0 and yy=0) and isOnMap(xx, yy)
答案3
得分: 0
在循环中,continue 关键字表示你将跳到循环的下一次迭代。在你的情况下,你有:
if (Math.abs(xx) + Math.abs(yy) > 1) {
continue;
}
if (isOnMap(x + xx, y + yy)) {
System.out.println(grid[x+xx][y+yy]);
}
所以,如果第一个条件被验证,你将不会打印任何答案,这意味着你的程序不会考虑 A(xx, yy) 作为一个邻居。
在你的 ABESA 示例中,B 和 S 会因此而被忽略。
英文:
In a loop, the continue keyword means that you will skip to the next iteration of the loop. In your case, you have :
if (Math.abs(xx) + Math.abs(yy) > 1) {
continue;
}
if (isOnMap(x + xx, y + yy)) {
System.out.println(grid[x+xx][y+yy]);
}
So if the first condition is verified, you will not print any answer, meaning that your program won't consider A(xx, yy) to be a neighbord.
In your ABESA example, B and S are ignored because of this.
答案4
得分: 0
以下是翻译好的部分:
如果您想要使用行数和列数可变的二维数组,您需要将它们作为参数传递给您的 isOnMap
方法,如下所示:
public static boolean isOnMap(int x, int y, int length, int width) {
return x >= 0 && y >= 0 && x < length && y < width;
}
您可以处理您的二维数组的特殊情况(当元素的行号和列号中至少有一个等于0时),将您的 getNeighborsOfPoint
方法重写为以下方式:
public static void getNeighborsOfPoint(int x, int y, char[][] grid) {
final int length = grid.length;
final int width = grid[0].length;
if (isOnMap(x, y, length, width)) {
for (int i = Math.max(0, x - 1); i < Math.min(length, x + 2); ++i) {
for (int j = Math.max(0, y - 1); j < Math.min(width, y + 2); ++j) {
if (i != x || j != y) {
System.out.println(grid[i][j]);
}
}
}
}
}
英文:
If you want to use 2d arrays with variable number of rows and columns you have to pass them as parameters in your's isOnMap
method like below:
public static boolean isOnMap(int x, int y, int length, int width) {
return x >= 0 && y >= 0 && x < length && y < width;
}
You can handle the special cases of your 2d array (when one or both rownumber and columnnumber of your element are equal to 0) rewriting your getNeighborsOfPoint
method in this way:
public static void getNeighborsOfPoint(int x, int y, char[][] grid) {
final int length = grid.length;
final int width = grid[0].length;
if (isOnMap(x, y, length, width)) {
for (int i = Math.max(0, x - 1); i < Math.min(length, x + 2); ++i) {
for (int j = Math.max(0, y - 1); j < Math.min(width, y + 2); ++j) {
if (i != x || j != y) {
System.out.println(grid[i][j]);
}
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论