顺时针旋转2D阵列

Rotating 2d array clockwise

本文关键字:阵列 2D 旋转 顺时针      更新时间:2023-10-16

我需要创建AD 2D动态数组,然后创建一个将接收2D动态数组的函数,然后顺时针旋转90度,然后将其返回到MAIN。但是我不确定为什么我没有输出?是因为错误的交换技术吗?我认为索引交换如下:

I J-----------I J
0 0           0 1
0 1           1 1
0 2           2 1

我带来了:

for (int i = 0; i <row;i++)
    for(int j = 0;j<col; j++)
    {
        Roti[i+j][row-i]=arr[i][j];
    }

代码:

#include <iostream>
using namespace std;
void print2DArray(int **arr, int rows, int cols);
int **Rotate(int **arr, int row, int col)
{
    int **Roti = new int *[row];
    for (int i = 0; i <row;i++)
    {
        Roti[i] = new int [col];
    }

    for (int i = 0; i <row;i++)
        for(int j = 0;j<col; j++)
        {
            Roti[i+j][row-i]=arr[i][j];
        }
    return  Roti;
}
int main()
{
    int *A[3];
    for (int i = 0; i < 3; i++)
    {
        A[i] = new int[3];
        for (int j = 0; j < 3; j++)
        {
            A[i][j] = rand() % 20;
        }
    }
    cout << "The array is :n";
    print2DArray(A, 3, 3);
    int **ptr;
    ptr=Rotate(A,3,3);
cout<<"-----"<<endl;
    print2DArray(ptr, 3, 3);


    for (int i = 0; i < 3; i++)
    {
        delete[] A[i];
    }
    return 0;
}
void print2DArray(int **arr, int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
}

希望通过"旋转",您并不意味着常规转置,因为在stackoverflow上已经有很多答案。在C 中转置矩阵的最快方法是什么?

对于矩阵的旋转,我修改了您的代码以使其工作如下所示。逻辑仅仅是原始矩阵的第一行变成了旋转矩阵的最后一列,并使用此逻辑,提出以下代码。希望能帮助到你。

#include <iostream>
using namespace std;
void print2DArray(int **arr, int rows, int cols);
int **Rotate(int **arr, int num_rows, int num_cols);
int main()
{
    int num_rows = 3;
    int num_cols = 3;
    int** A_matrix = new int*[num_rows];
    for (int i = 0; i < num_rows; i++)
        A_matrix[i] = new int[num_cols];
    for (int row = 0; row < num_rows; row++)
    {
        for (int col = 0; col < num_cols; col++)
        {
            A_matrix[row][col] = rand() % 20;
        }
    }
    cout << "The array is :n";
    print2DArray(A_matrix, 3, 3);
    int** roated_matrix;
    roated_matrix = Rotate(A_matrix, num_rows, num_cols);
    cout << "Rotated array:" << endl;
    print2DArray(roated_matrix, 3, 3);

    return 0;
}
int **Rotate(int **arr, int num_rows, int num_cols)
{
    /* Rotated matrix will have dimensions reverse as well. 
       That's why we have rows and cols reversed in the following lines */
    int** rotated_matrix = new int*[num_cols];
    for (int i = 0; i < num_cols; i++)
        rotated_matrix[i] = new int[num_rows];

    for (int row = 0; row < num_rows; row++)
    {
        int col_rotated_matrix = num_rows - 1 - row; // 1st row shall be copied to the last column and so on
        for (int col = 0; col < num_cols; col++)
        {
            int row_rotated_matrix = col; // rows become columns in the rotated matrix
            rotated_matrix[row_rotated_matrix][col_rotated_matrix] = arr[row][col];
        }
    }
    return rotated_matrix;
}
void print2DArray(int **arr, int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
}

它产生以下输出:

The array is :
1 7 14
0 9 4
18 18 2
Rotated array:
18 0 1
18 9 7
2 4 14

尝试使用此;

for (int i = 0; i <row;i++)
k = row.length -1 ;
    for(int j = 0;j<col; j++)
    {
        Roti[j][k]=arr[i][j];
    }
}