我尝试了一些方法,当带有 Point 对象(需要移动)的数组 按降序排序时,它实际上可以工作:
public static void main(String[] args) {
// first an example matrix with chars to fit your example
char[][] matrix = new char[3][];
matrix[0] = new char[]{'A', 'B', '0', '1'};
matrix[1] = new char[]{'C', 'D', '2', '3'};
matrix[2] = new char[]{'4', '5', '6', '7'};
// then the elements you want to move (the highest index first!)
Point[] elements = new Point[]{
new Point(1, 1),
new Point(1, 0),
new Point(0, 1),
new Point(0, 0)
};
// the direction indicates with where the element has to go to from it's current index. So (1,1) means one right, one down
Point directionPoint = new Point(1, 1);
// print the matrix to see what the original looks like
printMatrix(matrix);
// iterate through the elements that have to be moved
for (Point p : elements) {
move(p, directionPoint, matrix);
printMatrix(matrix);
}
}
// this method takes one element, the direction and the matrix and moves this one element by switching it with the element that is at its destination index
public static void move(Point elementToMove, Point direction, char[][] matrix) {
char temp = matrix[elementToMove.x][elementToMove.y];
matrix[elementToMove.x][elementToMove.y] = matrix[elementToMove.x + direction.x][elementToMove.y + direction.y];
matrix[elementToMove.x + direction.x][elementToMove.y + direction.y] = temp;
}
// just a simple print method to see the current matrix
public static void printMatrix(char[][] matrix) {
for (char[] row : matrix) {
String line = "";
for (int i = 0; i < row.length; i++) {
line += row[i] + " ";
}
System.out.println(line);
}
System.out.println("---");
}
当我运行它时,这是结果(第一个矩阵是原始矩阵,最后一个矩阵是结果):
A B 0 1
C D 2 3
4 5 6 7
---
A B 0 1
C 6 2 3
4 5 D 7
---
A B 0 1
5 6 2 3
4 C D 7
---
A 2 0 1
5 6 B 3
4 C D 7
---
6 2 0 1
5 A B 3
4 C D 7
现在我知道这可能不是最优雅的解决方案,而且我几乎可以肯定,它并不适用于所有可能的情况,但它表明,当元素被排序时,“转移”是有效的。你只需要从最后一个元素开始移动(当你想向右移动时)。如果你当然想向左移动(也许direction=(0,-1)
)你需要从第一个元素开始......等等