Java中,通过在任意方向二维数组循环 [英] Java, loop through 2d array in any direction
问题描述
我想在二维数组中指定任意方向移动元素。按shift我的意思是,如果离开是pressed,数组中的每个元素就向左移动,因为它可以。
I'm trying to shift elements in an 2d array in any direction specified. By shift I mean, if left is pressed, each element of the array is moved as far to the left as it can.
所以这(0表示一个空的空间),
so this (0 indicated an empty space),
1 0 2 0
0 3 0 1
1 0 6 9
1 0 0 0
将成为
1 2 0 0
3 1 0 0
1 6 9 0
1 0 0 0
结果
这是什么我有如果向上是$ P $ sudo的pssed code;
This is sudo code of what I have for if 'up' is pressed;
for col = 0 to SIZE
for i = 0 to SIZE - 1
if array[i][col] == 0
for j = i to SIZE
if array[j][col] != 0
array[i][col] = array[row][j]
array[j][col] = 0
break
实际code是一个较长的(对于碰撞两个相同的要素即支票),我不希望有code(高达本节的4份,下,左,右),但我无法弄清楚如何做到这一点任何其他方式。
The actual code is a longer (ie. a check for two identical elements that collide) and I don't want to have 4 copies of this section of code (up, down, left, right), but I can't figure out how to do it any other way.
推荐答案
虽然技术上可以覆盖同样的方法所有的这些移位操作,它可能不像建立一个专门为易于理解(和效率不高)方法为每个方向。
While it is technically possible to cover all these shifting operations with the same methods, it's probably not as easy to understand (and not as efficient) as creating a dedicated method for each direction.
在一方面,这是震撼人心的,并粉碎了的<em>真正的程序员,但在另一方面......哎,你可以肯定的是永远不会有前所未有的超过这四个方向。
On the one hand, this is humbling and shattering for a real programmer, but on the other hand ... hey, you can be sure that there will never-ever be more than these four directions.
或者......好吧,对角线,也许? ; - )
Or... well, diagonals, maybe? ;-)
public class ArrayShiftTest
{
public static void main(String[] args)
{
int array[][] = new int[][]
{
{ 1, 0, 2, 0 },
{ 0, 3, 0, 1 },
{ 1, 0, 6, 9 },
{ 1, 0, 0, 0 },
};
System.out.println("Original");
print(array);
System.out.println("Left");
int arrayL[][] = clone(array);
shift(arrayL, 0, -1);
print(arrayL);
System.out.println("Right");
int arrayR[][] = clone(array);
shift(arrayR, 0, 1);
print(arrayR);
System.out.println("Up");
int arrayU[][] = clone(array);
shift(arrayU, -1, 0);
print(arrayU);
System.out.println("Down");
int arrayD[][] = clone(array);
shift(arrayD, 1, 0);
print(arrayD);
// Bonus :-) :
System.out.println("Left Up");
int arrayLU[][] = clone(array);
shift(arrayLU, -1, -1);
print(arrayLU);
System.out.println("Right Up");
int arrayRU[][] = clone(array);
shift(arrayRU, -1, 1);
print(arrayRU);
System.out.println("Left Down");
int arrayLD[][] = clone(array);
shift(arrayLD, 1, -1);
print(arrayLD);
System.out.println("Right Down");
int arrayRD[][] = clone(array);
shift(arrayRD, 1, 1);
print(arrayRD);
}
private static void shift(int array[][], int dr, int dc)
{
boolean shifted = true;
while (shifted)
{
shifted = false;
for (int r=0; r<array.length; r++)
{
for (int c=0; c<array[r].length; c++)
{
if (array[r][c] != 0)
{
shifted |= shift(array, r, c, dr, dc);
}
}
}
}
}
private static boolean shift(int[][] array, int r, int c, int dr, int dc)
{
int value = array[r][c];
array[r][c] = 0;
int cr = r;
int cc = c;
while (isValid(array, cr, cc))
{
int tr = cr + dr;
int tc = cc + dc;
if (!isValid(array, tr, tc) || array[tr][tc] != 0)
{
break;
}
cr = tr;
cc = tc;
}
array[cr][cc] = value;
return cr != r || cc != c;
}
private static boolean isValid(int array[][], int r, int c)
{
return r>=0 && r<array.length && c>=0 && c<array[r].length;
}
private static int[][] clone(int array[][])
{
int result[][] = array.clone();
for (int r=0; r<array.length; r++)
{
result[r] = array[r].clone();
}
return result;
}
private static void print(int array[][])
{
for (int r=0; r<array.length; r++)
{
for (int c=0; c<array[r].length; c++)
{
System.out.printf("%3d", array[r][c]);
}
System.out.println("");
}
}
}
这篇关于Java中,通过在任意方向二维数组循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!