本文介绍了解释将N X N矩阵旋转90度的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an algorithm that rotates a N X N matrix by 90 degree. It works but slightly hard for me to understand. Anyone can explain it to me in details ? Thanks.

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


推荐答案

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

Submitted for your approval. Run it as is and study the output and think about running bases clockwise. Seriously.

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

Let me know if it helped. It was fun to explore for me.

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

}

例如:

这篇关于解释将N X N矩阵旋转90度的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:31