2

我正在尝试将矩阵中的选定元素组水平和垂直移动一段距离。

我正在使用 Java,作为输入,我有:

  • 二维元素数组
  • 具有要移动的元素坐标的数组(Point对象数组)
  • 移动距离(作为Point对象,Point.x- 水平移动,Point.y- 垂直)

例如,将所有字母向右移动 1 个位置如下所示:

From:
A B 0 1
2 C 3 4
5 6 7 8

To:
0 A B 1
2 3 C 4
5 6 7 8

另一个例子,将字母水平移动 1 个位置,垂直移动 1 个位置:

From:
A B 0 1
C D 2 3
4 5 6 7

To:
6 2 0 1
5 A B 3
4 C D 7

(注意数字如何填补创造的空白)

长距离移动元素很容易,因为没有重叠,但现在我坚持上面的例子。

另一个例子(简单的情况,没有重叠)。将同一个正方形移动 (2,2):

From:
A  B  0  1
C  D  2  3
4  5  6  7
8  9  10 11

To:
6  7  0  1
10 11 2  3
4  5  A  B
8  9  C  D
4

2 回答 2

1

只需复制您的矩阵。

让 a = 你的原始矩阵

让 b = 你的目标矩阵

在移动之前,设 b = a;

如果您想将 a[i][j] 移动到 a[p][q],只需首先将 a[i][j] 移动到 b[p][q] 即可。

我们应该决定用什么数字来填补空白:

For every cell(i,j):

Step 1. Check if the cell will be a gap after the move, if not do nothing.

Step 2. find where the cell moves(denotes by (x,y)).

Step 3. If (x,y) is not a number before the move then repeat Step 2, 
        else we can say after the move b[i][j] is a[x][y].

移动完成后,只需将数组 b 复制到数组 a 并进行下一步操作。

于 2013-04-26T22:01:12.497 回答
1

我尝试了一些方法,当带有 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))你需要从第一个元素开始......等等

于 2013-04-26T22:43:21.147 回答