简体   繁体   中英

Rotate 3 x 3 matrix in C++

How can I rotate by 90 degrees a 3 x 3 matrix in cpp...

//create the matrix
const int MATRIX_SIZE = 3;
int m[MATRIX_SIZE][MATRIX_SIZE];
int last = MATRIX_SIZE - 1;

int count = 0;
int level = 0;


for(int i =0; i< MATRIX_SIZE; i++)
{
    for(int j = 0; j< MATRIX_SIZE; j++)
    {
        m[i][j] = count++;
    }
}

//rotate matrix
   for(int i = 0; i <last; ++i)
    {
        Swap(m[level][i], m[i][last]);
        Swap(m[level][i], m[last][last-i]);
        Swap(m[level][i], m[last-i][level]);
    }

I get a lot of 1's added and I do not know where they are coming from.

OUTPUT:

------ Original Matrix ------ 
0       1       2       
3       4       5       
6       7       8       
 ------ Rotated Matrix ------ 
6       3       1       
1       4       1       
1       1       1     

I am using "level" to swal through the inner part of the matrix. Is there any other way to do this using STD?

Presented for your consideration. Using templates to infer the size of the matrix as it's passed into functions to print it, and to rotate it 90 degrees counter-clockwise.

In the rotate_matrix function, the outer loop iterates over "shell levels" using sl . A matrix of 3x3 dimensions has 3 / 2 (or 1 ) shell levels that need to be rotated. I've tested with a 6x6 matrix, which has three shell levels.

To avoid repetition, each shell has a dimension of N - sl * 2 , and we subtract one from that to avoid repetition of work on the corners.

Then we just need to map out the four values that need to be moved on each iteration, and do a four-way swap.

#include <iostream>

template <typename T, std::size_t N>
void print_matrix(T m[N][N]) {
    for (std::size_t i = 0; i < N; ++i) {
        for (std::size_t j = 0; j < N; ++j) {
            std::cout << m[i][j] << " ";
        }

        std::cout << std::endl;
    }
}

template <typename T, std::size_t N>
void rotate_matrix(T m[N][N]) {
    for (std::size_t sl = 0; sl < N / 2; ++sl) {
        for (std::size_t i = 0; i < N - 1 - sl * 2; ++i) {
            T temp                       = m[sl][sl + i];          
            m[sl][sl + i]                = m[sl + i][N - 1 - sl];
            m[sl + i][N - 1 - sl]        = m[N - 1 - sl][N - 1 -sl - i];
            m[N - 1 - sl][N - 1 -sl - i] = m[N - 1 - sl - i][sl];
            m[N - 1 - sl - i][sl]        = temp;
        }
    }
}

int main() {
    int m[6][6] = {
        { 1,  2,  3,  4,  5,  6},
        { 7,  8,  9, 10, 11, 12},
        {13, 14, 15, 16, 17, 18},
        {19, 20, 21, 22, 23, 24},
        {25, 26, 27, 28, 29, 30},
        {31, 32, 33, 34, 35, 36}
    };

    print_matrix(m);

    std::cout << std::endl;

    rotate_matrix(m);
    print_matrix(m);

    return 0;
}

Result:

1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36

6 12 18 24 30 36
5 11 17 23 29 35
4 10 16 22 28 34
3 9 15 21 27 33
2 8 14 20 26 32
1 7 13 19 25 31

the swap() fucntion had an typo. got it to work

void Swap(int& i, int& j)
{
    int temp = i; 
    i = j; 
    j = temp;
}

Also, check this out for other solutions. How to rotate a N x N matrix by 90 degrees?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM