簡體   English   中英

說明將NXN矩陣旋轉90度的算法

[英]Explain an algorithm to rotate a N X N matrix by 90 degree

我有一種算法可以將NXN矩陣旋轉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
            }
        }
    }

提交供您批准。 按原樣運行並研究輸出,並考慮順時針運行基准。 認真。

讓我知道是否有幫助。 對我來說探索很有趣。

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);
 }

}

例如:

在此處輸入圖片說明

我將使用以下數據:

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

並在rotate的最后一個matrix行之后添加以下代碼:

            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);
 }

並觀察每次迭代中哪些元素“移動”:

編輯忘記上面的內容。

請參閱我的第二個答案以進行視覺解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM