如何在矩阵中移动元素组?

时间:2013-04-26 21:50:47

标签: java algorithm matrix

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

我正在使用Java作为输入:

  • 2d元素数组
  • 包含要移动的元素坐标的数组(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

2 个答案:

答案 0 :(得分:1)

只需制作矩阵的副本。

让a =原始矩阵

设b =目的地矩阵

在移动之前,让b = a;

如果你想将[i] [j]移动到[p] [q],只需先将[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并进行下一步移动。

答案 1 :(得分: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)),则需要从第一个元素开始...依此类推