Created
December 7, 2016 06:07
-
-
Save jianminchen/fd5e7e7e4a031971e56dc30a949e5421 to your computer and use it in GitHub Desktop.
MatrixRotation code review - study code - No. 2
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.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net.Mail; | |
class Solution | |
{ | |
static void Display(int[][] a) | |
{ | |
for (int row = 0; row < a.Length; row++) | |
{ | |
for (int column = 0; column < a[row].Length; column++) | |
{ | |
Console.Write(a[row][column]); | |
Console.Write(' '); | |
} | |
Console.WriteLine(); | |
} | |
} | |
static void Rotate(int[][] a, int amount) | |
{ | |
int count = GetRingCount(a); | |
for (int i = 0; i < count; i++) | |
{ | |
Rotate(a, i, amount); | |
} | |
} | |
static void Rotate(int[][] a, int index, int amount) | |
{ | |
int minRow = index; | |
int maxRow = a.Length - 1 - index; | |
int minColumn = index; | |
int maxColumn = a[0].Length - 1 - index; | |
int height = maxRow - minRow + 1; | |
int width = maxColumn - minColumn + 1; | |
amount = amount%(2*height + 2*width - 4); | |
for (int k = 0; k < amount; k++) | |
{ | |
int i = minRow, j = minColumn; | |
int buffer = a[i][j]; | |
do | |
{ | |
if (i < maxRow && j == minColumn) i++; | |
else if (i == maxRow && j < maxColumn) j++; | |
else if (i > minRow && j == maxColumn) i--; | |
else if (i == minRow && j > minColumn) j--; | |
int buffer1 = a[i][j]; | |
a[i][j] = buffer; | |
buffer = buffer1; | |
} while (!(i == minRow && j == minColumn)); | |
} | |
} | |
static int GetRingCount(int[][] a) | |
{ | |
int rows = a.Length; | |
int columns = a[0].Length; | |
return Math.Min(rows, columns) / 2; | |
} | |
static void Main(String[] args) | |
{ | |
var firstLine = Console.ReadLine().Split(' '); | |
var m = int.Parse(firstLine[0]); | |
var n = int.Parse(firstLine[1]); | |
var r = int.Parse(firstLine[2]); | |
var a = new int[m][]; | |
for (int i = 0; i < m; i++) | |
a[i] = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); | |
Rotate(a, r); | |
Display(a); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment