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

标签 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
            }
        }
    }

最佳答案

已提交供您批准。按原样运行并研究输出并考虑顺时针运行基地。说真的。

请告诉我是否有帮助。对我来说探索很有趣。

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

}

例如:

enter image description here

关于java - 解释将 N X N 矩阵旋转 90 度的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31484264/

相关文章:

java - 无法使用 Jackson 和 Spring Boot 实现自定义 JSON 序列化器

algorithm - 如何找到整数数组中的最大非重复数?

algorithm - 霍夫曼编码算法/数据结构

java - LinearLayout 中的居中对象 (Android)

java - 无法初始化类 javax.crypto.SunJCE_b

regex - Bash - grep 提取以指定字符串结尾的单词;在找不到匹配项的地方留下空格

c - 查找二叉树中最左边的节点

algorithm - 如何生成特定长度的随机字符串?

java - TomCat 因 flex3 调用而卡住

bash 脚本中的 Javac 不返回此类文件