Created
February 11, 2019 18:17
-
-
Save igorLisovitskiy/cecea9eb047b0e126e5ff4922ba9e534 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
namespace RotateMatrix | |
{ | |
class Program | |
{ | |
public static int[] GetRow(int[,] matrix, int rowNumber) | |
{ | |
return Enumerable.Range(0, matrix.GetLength(1)) | |
.Select(x => matrix[rowNumber, x]) | |
.ToArray(); | |
} | |
public static void Print1DArray(int[] arr) | |
{ | |
foreach (int el in arr) | |
{ | |
Console.Write($"{el}\t"); | |
} | |
Console.WriteLine(); | |
} | |
public static void Print2DArray(int[,] arr) | |
{ | |
int numOfRows = arr.GetUpperBound(0) + 1; | |
int numOfcolumns = arr.Length / numOfRows; | |
for (int i = 0; i < numOfRows; i++) | |
{ | |
Print1DArray(GetRow(arr, i)); | |
} | |
} | |
public static void Swap(int[,] arr, int x1, int y1, int x2, int y2) | |
{ | |
int temp = arr[x1, y1]; | |
arr[x1, y1] = arr[x2, y2]; | |
arr[x2, y2] = temp; | |
} | |
static void Main(string[] args) | |
{ | |
int[,] matrix = new int[4, 4] { | |
{ 1, 2, 3, 4}, | |
{ 5, 6, 7, 8}, | |
{ 9, 10, 11, 12}, | |
{ 13, 14, 15, 16} | |
}; | |
//int[,] matrix = new int[3, 3] { | |
// { 1, 2, 3}, | |
// { 4, 5, 6}, | |
// { 7, 8, 9} | |
//}; | |
//int[,] matrix = new int[2, 2] { | |
// { 1, 2}, | |
// { 4, 5} | |
// }; | |
// The size of a matrix is defined by the number of rows and columns that it contains | |
int matrixSize = (matrix.GetLength(0) + matrix.GetLength(1)) / 2; | |
int lastIndex = matrixSize - 1; | |
Console.WriteLine($"Matrix size = {matrixSize}"); | |
int level = 0; | |
int totalNumberOfLevels = matrixSize / 2; // число слоев в матрице | |
/* | |
Для матрицы 2*2 алгоритм следующий: | |
Поворачиваем матрицу вправо на 90 градусов | |
{ { | |
{1, 2}, -> {4, 1}, | |
{4, 5} {5, 2} | |
} } | |
Индексы | |
{[0, 0], [0, 1]} | |
{[1, 0], [1, 1]} | |
Делаем это через серию свапов, инкрементно меняя значение верхнего левого значения | |
top left moves to top right | |
Swap([0, 0], [0, 1]); | |
top left moves to bottom right | |
Swap([0, 0], [1, 1]); | |
top left moves to bottom left | |
Swap([0, 0], [1, 0]); | |
Для матрицы 3*3 добавляется понятие level - число слоев в матрице. В матрице 3*3 всего 1 level, но он двигает порядок свапов | |
для level = 1; | |
нас интересуют с индексами | |
{ ., 2, .} { ., [0, 1], . } | |
{ 4, ., 6} { [1, 0], ., [1, 2]} | |
{ ., 8, .} { ., [2, 1], .} | |
Меняем свапы | |
top center moves to center right | |
Swap([0, 1], [1, 2]); | |
top center moves to bottom center | |
Swap([0, 1], [2, 1]); | |
top center moves to right center | |
Swap([0, 1], [1, 0]); | |
*/ | |
Console.WriteLine("Before rotation"); | |
Print2DArray(matrix); | |
// arr.GetLength(0) Length of first dimension: rows | |
// arr.GetLength(1)Length of second dimension: columns | |
while (level < totalNumberOfLevels) { | |
for (int i = level; i < lastIndex; i++) | |
{ | |
Swap(matrix, level, i, i, lastIndex); | |
Swap(matrix, level, i, lastIndex, lastIndex - i + level); | |
Swap(matrix, level, i, lastIndex - i + level, level); | |
} | |
++level; | |
--lastIndex; | |
} | |
Console.WriteLine("Rotated 90 degrees to the right"); | |
Print2DArray(matrix); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment