旋转M*N矩阵(90度)

Sas*_*sky 6 c# algorithm rotation matrix

我该如何旋转矩阵

|3 4 5 6 8|
|5 4 3 2 6|
|3 3 7 8 9|
Run Code Online (Sandbox Code Playgroud)

|8 6 9|            
|6 2 8|
|5 3 7|
|4 4 3|
|3 5 3|
Run Code Online (Sandbox Code Playgroud)

因为我见过的所有算法都是N*N矩阵.

wjm*_*wjm 13

如果矩阵由数组表示matrix[i, j],其中i行是行,j而列是列,则实现以下方法:

static int[,] RotateMatrixCounterClockwise(int[,] oldMatrix)
{
    int[,] newMatrix = new int[oldMatrix.GetLength(1), oldMatrix.GetLength(0)];
    int newColumn, newRow = 0;
    for (int oldColumn = oldMatrix.GetLength(1) - 1; oldColumn >= 0; oldColumn--)
    {
        newColumn = 0;
        for (int oldRow = 0; oldRow < oldMatrix.GetLength(0); oldRow++)
        {
            newMatrix[newRow, newColumn] = oldMatrix[oldRow, oldColumn];
            newColumn++;
        }
        newRow++;
    }
    return newMatrix;
}
Run Code Online (Sandbox Code Playgroud)

这适用于各种尺寸的矩阵.