解释将N X N矩阵旋转90度的算法

时间:2015-07-17 20:33:21

标签: java algorithm

我有一个算法可以将N X N矩阵旋转90度。它有效,但我很难理解。任何人都可以向我详细解释一下吗?谢谢。

 public static void rotate(int[][] matrix, int n) {

        for (int layer = 0; layer < n / 2; ++layer) {

            int first = layer;
            int last = n - 1 - layer;

            for(int i = first; i < last; ++i) {

                int offset = i - first;
                int top = matrix[first][i]; // save top

                // left -> top
                matrix[first][i] = matrix[last-offset][first];          

                // bottom -> left
                matrix[last-offset][first] = matrix[last][last - offset]; 

                // right -> bottom
                matrix[last][last - offset] = matrix[i][last]; 

                // top -> right
                matrix[i][last] = top; // right <- saved top
            }
        }
    }

2 个答案:

答案 0 :(得分:5)

提交您的批准。按原样运行并研究输出并考虑顺时针运行基座。严重。

如果有帮助,请告诉我。为我探索很有趣。

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class NewMain1 {

 static int[][] m ;

 public static void rotate(int[][] matrix, int n) {

        for (int layer = 0; layer < n / 2; ++layer) {

            int first = layer;
            int last = n - 1 - layer;

            for(int i = first; i < last; ++i) {

                int offset = i - first;
                int top = matrix[first][i]; // save top
                // left -> top
                matrix[first][i] = matrix[last-offset][first]; 
                printmove(last-offset,first,first,i);

                // bottom -> left
                printmove(last,last-offset,last-offset,first);
                matrix[last-offset][first] = matrix[last][last - offset]; 

                // right -> bottom
                printmove(i,last,last,last-offset);
                matrix[last][last - offset] = matrix[i][last]; 

                // top -> right
                printmove(first,i,i,last);
                matrix[i][last] = top; // right <- saved top
                System.out.println("");
                printmatrix(matrix,n);
                System.out.println("");
              try{
                int s = System.in.read();
              } catch (IOException ex){ }
            }
        }
    }

 static void printmove(int r1, int c1, int r2, int c2){
    System.out.println("["+(r1+1)+"]["+(c1+1)+ "] moves to [" + (r2+1) + "][" + (c2+1) + "]");
 }

 static void printmatrix(int[][] m, int n){
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        System.out.print(m[i][j] + " ");
       }
      System.out.println("");
   }
 }

  static void makematrix(int[][] m, int n){
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        m[i][j] = 10*(i+1) + j+1;
       }
    }
  }

  public static void main(String[] args) {
    int n = 6;
    int[][] m = new int[n][n];
    makematrix(m, n);
    printmatrix(m, n);
    rotate(m,n);
    System.out.println("");
    printmatrix(m, n);
 }

}

E.g:

enter image description here

答案 1 :(得分:0)

我使用这些数据:

 static int[][] m = {{11,12,13,14},{21,22,23,24},{31,32,33,34},{41,42,43,44}};

并在matrix中的最后一行rotate之后添加此代码:

            System.out.println("");
            printmatrix(matrix,n);
            System.out.println("");
          try{
            int s = System.in.read();
          } catch (IOException ex){

          }

并使用此例程在每次迭代时打印矩阵:

 static void printmatrix(int[][] m, int n){
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        System.out.print(m[i][j] + " ");
       }
      System.out.println("");
   }

 }

使用此main

  public static void main(String[] args) {

    printmatrix(m, 4);
    rotate(m,4);
    System.out.println("");
    printmatrix(m, 4);
 }

然后观察哪些元素&#34;移动&#34;在每次迭代期间:

编辑忘掉上述内容。

请参阅我的第二个答案以获得视觉解释。