📜  将方阵原地旋转90度的C++程序|设置 1

📅  最后修改于: 2022-05-13 01:54:37.026000             🧑  作者: Mango

将方阵原地旋转90度的C++程序|设置 1

给定一个方阵,在不使用任何额外空间的情况下将其逆时针方向旋转 90 度。
例子 :

Input:
Matrix:
 1  2  3
 4  5  6
 7  8  9
Output:
 3  6  9 
 2  5  8 
 1  4  7 
The given matrix is rotated by 90 degree 
in anti-clockwise direction.

Input:
 1  2  3  4 
 5  6  7  8 
 9 10 11 12 
13 14 15 16 
Output:
 4  8 12 16 
 3  7 11 15 
 2  6 10 14 
 1  5  9 13
The given matrix is rotated by 90 degree 
in anti-clockwise direction.

这里已经讨论了需要额外空间的方法。
方法:要在没有多余空间的情况下解决问题,请将数组旋转为正方形,将矩阵划分为正方形或循环。例如,
一个 4 X 4 矩阵将有 2 个周期。第一个循环由它的第一行、最后一列、最后一行和第一列组成。第二个循环由第二行、倒数第二列、倒数第二行和第二列组成。这个想法是对于每个方形循环,以逆时针方向交换与矩阵中相应单元格相关的元素,即从上到左、从左到下、从下到右和从右到上一次,只使用一个临时变量来实现这一点。
示范:

First Cycle (Involves Red Elements)
 1  2  3 4 
 5  6  7 8 
 9 10 11 12 
 13 14 15 16 

Moving first group of four elements (First
elements of 1st row, last row, 1st column 
and last column) of first cycle in counter
clockwise. 
 4  2  3 16
 5  6  7 8 
 9 10 11 12 
 1 14  15 13 
 
Moving next group of four elements of 
first cycle in counter clockwise 
 4  8  3 16 
 5  6  7  15  
 2  10 11 12 
 1  14  9 13 

Moving final group of four elements of 
first cycle in counter clockwise 
 4  8 12 16 
 3  6  7 15 
 2 10 11 14 
 1  5  9 13 


Second Cycle (Involves Blue Elements)
 4  8 12 16 
 3  6 7  15 
 2  10 11 14 
 1  5  9 13 

Fixing second cycle
 4  8 12 16 
 3  7 11 15 
 2  6 10 14 
 1  5  9 13

算法:

  1. 在 N 边的矩阵中有 N/2 个正方形或循环。一次处理一个正方形。运行一个循环,一次循环遍历矩阵一个循环,即从0循环到N/2-1,循环计数器为i
  2. 考虑当前正方形中的 4 个元素组,一次旋转 4 个元素。因此,一个循环中此类组的数量为 N – 2*i。
  3. 所以在从 x 到 N - x - 1 的每个循环中运行一个循环,循环计数器为y
  4. 当前组中的元素是 (x, y), (y, N-1-x), (N-1-x, N-1-y), (N-1-y, x),现在旋转这 4 个元素,即 (x, y) 打印矩阵。

C++
// C++ program to rotate a matrix
// by 90 degrees
#include 
#define N 4
using namespace std;
  
void displayMatrix(
    int mat[N][N]);
  
// An Inplace function to
// rotate a N x N matrix
// by 90 degrees in
// anti-clockwise direction
void rotateMatrix(int mat[][N])
{
    // Consider all squares one by one
    for (int x = 0; x < N / 2; x++) {
        // Consider elements in group
        // of 4 in current square
        for (int y = x; y < N - x - 1; y++) {
            // Store current cell in
            // temp variable
            int temp = mat[x][y];
  
            // Move values from right to top
            mat[x][y] = mat[y][N - 1 - x];
  
            // Move values from bottom to right
            mat[y][N - 1 - x]
                = mat[N - 1 - x][N - 1 - y];
  
            // Move values from left to bottom
            mat[N - 1 - x][N - 1 - y]
                = mat[N - 1 - y][x];
  
            // Assign temp to left
            mat[N - 1 - y][x] = temp;
        }
    }
}
  
// Function to print the matrix
void displayMatrix(int mat[N][N])
{
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++)
            printf("%2d ", mat[i][j]);
  
        printf("
");
    }
    printf("
");
}
  
/* Driver program to test above functions */
int main()
{
    // Test Case 1
    int mat[N][N] = {
        { 1, 2, 3, 4 },
        { 5, 6, 7, 8 },
        { 9, 10, 11, 12 },
        { 13, 14, 15, 16 }
    };
  
    // Tese Case 2
    /* int mat[N][N] = {
                        {1, 2, 3},
                        {4, 5, 6},
                        {7, 8, 9}
                    };
     */
  
    // Tese Case 3
    /*int mat[N][N] = {
                    {1, 2},
                    {4, 5}
                };*/
  
    // displayMatrix(mat);
  
    rotateMatrix(mat);
  
    // Print rotated matrix
    displayMatrix(mat);
  
    return 0;
}


输出 :

4  8 12 16 
 3  7 11 15 
 2  6 10 14 
 1  5  9 13 

复杂性分析:

  • 时间复杂度: O(n*n),其中 n 是数组的边。
    需要对矩阵进行一次遍历。
  • 空间复杂度: O(1)。
    因为需要一个恒定的空间

请参考完整的文章 Inplace 将方阵旋转 90 度 |设置 1 了解更多详情!