本文介绍了更改数组中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为这个不常见的任务困扰了几天,以找出合适的算法。假设我有2D数组:

I've puzzled over this not usual task for a days to figure out an appropriate algorithm. Let's say I have 2D array:

int[][] jagged = new int[4][];
jagged[0] = new int[4] { 1, 2, 3, 4 };
jagged[1] = new int[4] { 5, 6, 7, 8 };
jagged[2] = new int[4] { 9, 10, 11, 12 };
jagged[3] = new int[4] { 13, 14, 15, 16 };

如何沿逆时针方向旋转此矩阵,如下所示?

How can I rotate this matrix in counter-clockwise direction to be like the following?

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

2D数组可以具有各种尺寸。我的意思是2x3或3x4。

我尝试使用以下算法,但它只能旋转90度:

I've tried to use the following algorithm, but it rotates just to 90 degrees:

int [,] newArray = new int[4,4];

for (int i=3;i>=0;--i)
{
    for (int j=0;j<4;++j)
    {
         newArray[j,3-i] = array[i,j];
    }
}

如果我更改增量,则不会给我任何收益。也许还有其他解决方案?

If I change increments, then it does give me nothing. Maybe is there another solution?

推荐答案

我知道这个问题已经回答了,但是我想对 nxm 。它考虑了矩阵只有一列或一行的特殊情况。

I know this question is answered but I wanted to do it for a general matrix of nxm. It takes into account the corner cases where the matrix has only one column or row.

public static void Rotate(int[,] matrix)
{
    // Specific cases when matrix has one only row or column
    if (matrix.GetLength(0) == 1 || matrix.GetLength(1) == 1)
        RotateOneRowOrColumn(matrix);
    else
    {
        int min = Math.Min(matrix.GetLength(0), matrix.GetLength(1)),
        b = min / 2, r = min % 2;

        for (int d = 0; d < b + r; d++)
        {
            int bkp = matrix[d, d];
            ShiftRow(matrix, d, d, matrix.GetLength(1) - d - 1, true);
            ShiftColumn(matrix, matrix.GetLength(1) - d - 1, d, matrix.GetLength(0) - d - 1, false);
            ShiftRow(matrix, matrix.GetLength(0) - d - 1, d, matrix.GetLength(1) - d - 1, false);
            ShiftColumn(matrix, d, d + 1, matrix.GetLength(0) - d - 1, true);
            if (matrix.GetLength(0) - 2 * d - 1 >= 1)
                matrix[d + 1, d] = bkp;
       }
    }
}

private static void RotateOneRowOrColumn(int[,] matrix)
{
    bool isRow = matrix.GetLength(0) == 1;
    int s = 0, e = (isRow ? matrix.GetLength(1) : matrix.GetLength(0)) - 1;

    while (s < e)
    {
        if (isRow)
            Swap(matrix, 0, s, 0, e);
        else
            Swap(matrix, s, 0, e, 0);
        s++; e--;
    }
}

public static void Swap(int[,] matrix, int from_x, int from_y, int to_x, int to_y)
{
    //It doesn't verifies whether the indices are correct or not
    int bkp = matrix[to_x, to_y];
    matrix[to_x, to_y] = matrix[from_x, from_y];
    matrix[from_x, from_y] = bkp;
}

public static void ShiftColumn(int[,] matrix, int col, int start, int end,  bool down)
{
    for (int i = down ? end - 1 : start + 1; (down && i >= start) || (!down && i <= end); i += down ? -1 : 1)
    { matrix[i + (down ? 1 : -1), col] = matrix[i, col]; }
}

 public static void ShiftRow(int[,] matrix, int row, int start, int end, bool left)
{
    for (int j = left ? start + 1 : end - 1; (left && j <= end) || (!left && j >= start); j += (left ? 1 : -1))
    { matrix[row, j + (left ? -1 : 1)] = matrix[row, j]; }
}

这篇关于更改数组中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 04:16