开发者

How to rotate a N x N matrix by 90 degrees? [closed]

开发者 https://www.devze.com 2022-12-31 03:18 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 8 years ago.

Improve 开发者_JS百科this question

How to rotate a N x N matrix by 90 degrees. I want it to be inplace?


for(int i=0; i<n/2; i++)
   for(int j=0; j<(n+1)/2; j++)
       cyclic_roll(m[i][j], m[n-1-j][i], m[n-1-i][n-1-j], m[j][n-1-i]);


void cyclic_roll(int &a, int &b, int &c, int &d)
{
   int temp = a;
   a = b;
   b = c;
   c = d;
   d = temp;
}

Note I haven't tested this, just compoosed now on the spot. Please test before doing anything with it.


here is my solution: (rotate pi/2 clockwise)

  1. do the transpose of the array, (like matrix transpose)

  2. reverse the elements for each row

    cons int row = 10;
    cons int col = 10;
    //transpose
    for(int r = 0; r < row; r++) {
      for(int c = r; c < col; c++) {  
        swap(Array[r][c], Array[c][r]);
      }
    }
    //reverse elements on row order
    for(int r = 0; r < row; r++) {
        for(int c =0; c < col/2; c++) {
          swap(Array[r][c], Array[r][col-c-1])
        }
    }
    

if rotate pi/2 in counter-clockwise

  1. transpose the array

  2. reverse the elements on column order

never test the code! any suggestion would be appreciated!


A complete C program which illustrates my approach. Essentially it's recursive algo. At each recursion you rotate the outer layer. Stop when your matrix is 1x1 or 0x0.

#include <stdio.h>

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

void print_matrix(int n)
{
   for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
         printf(" %d ", matrix[i][j]);
      }
      printf("\n");
   }
}

int *get(int offset, int x, int y)
{
   return &matrix[offset + x][offset + y];
}

void transpose(int offset, int n)
{
   if (n > 1) {
      for (int i = 0; i < n - 1; i++) {
         int *val1 = get(offset, 0, i);
         int *val2 = get(offset, i, n - 1);
         int *val3 = get(offset, n - 1, n - 1 - i);
         int *val4 = get(offset, n - 1 - i, 0);

         int temp = *val1;
         *val1 = *val4;
         *val4 = *val3;
         *val3 = *val2;
         *val2 = temp;
      }

      transpose(offset + 1, n - 2);
   }
}

main(int argc, char *argv[])
{
   print_matrix(4);
   transpose(0, 4);
   print_matrix(4);
   return 0;
}


//Java version, fully tested

public class Rotate90degree {


        public static void reverseElementsRowWise(int[][] matrix) {
            int n = matrix.length;
            for(int i = 0; i < n; ++i) {
                for(int j = 0; j < n / 2; ++j) {
                    int temp = matrix[i][n - j - 1];
                    matrix[i][n - j - 1] = matrix[i][j];
                    matrix[i][j] = temp;
                }
            }
        }

        public static void transpose(int[][] matrix) {
            int n = matrix.length;
            for(int i = 0; i < n; ++i) {
                for(int j = i + 1; j < n; ++j) {
                    int temp = matrix[i][j];
                    matrix[i][j] = matrix[j][i];
                    matrix[j][i] = temp;
                }
            }
        }

        public static void rotate90(int[][] matrix) {
            transpose(matrix);
            reverseElementsRowWise(matrix);
        }

        public static void print(int[][] matrix) {
            int n = matrix.length;
            for(int i = 0; i < n; ++i) {
                for(int j = 0; j < n; ++j) {
                    System.out.print(matrix[i][j]);
                    System.out.print(' ');
                }
                System.out.println();
            }
        }

        public static void main(String[] args) {
            int[][] matrix = {
                    {1, 2, 3, 4},
                    {5, 6, 7, 8},
                    {9, 10, 11, 12},
                    {13, 14, 15, 16}};
            System.out.println("before");
            print(matrix);
            rotate90(matrix);
            System.out.println("after");
            print(matrix);
        }
}


You could create a second array and then copy the first one into the second one by reading row-major in the first one and writing column-major to the second one.

So you would copy:

1  2  3
4  5  6
7  8  9

and you would read the first row then write it back up starting like:

3
2
1
0

精彩评论

暂无评论...
验证码 换一张
取 消