Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created December 7, 2016 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/6fabef7436097552e35633a549b0268a to your computer and use it in GitHub Desktop.
Save jianminchen/6fabef7436097552e35633a549b0268a to your computer and use it in GitHub Desktop.
HackerRank - Matrix Rotation - score 80 - maximum score 80 - fix the bug on line 114, change variable name to actualSteps
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RotateArray2
{
class Program
{
/*
* Int16 maximum value is 32676, less than 4 * 10^9
*/
static void Main(string[] args)
{
matrixRotation();
//testcase1();
}
private static void testcase1()
{
// 10 8 40
int row = 10, col = 8, T = 40;
string input = @"9718805 60013003 5103628 85388216 21884498 38021292 73470430 31785927 69999937 71783860 10329789 96382322 71055337 30247265 96087879 93754371 79943507 75398396 38446081 34699742 1408833 51189 17741775 53195748 79354991 26629304 86523163 67042516 54688734 54630910 6967117 90198864 84146680 27762534 6331115 5932542 29446517 15654690 92837327 91644840 58623600 69622764 2218936 58592832 49558405 17112485 38615864 32720798 49469904 5270000 32589026 56425665 23544383 90502426 63729346 35319547 20888810 97945481 85669747 88915819 96642353 42430633 47265349 89653362 55349226 10844931 25289229 90786953 22590518 54702481 71197978 50410021 9392211 31297360 27353496 56239301 7071172 61983443 86544343 43779176";
int[] arr = Array.ConvertAll(input.Split(' '), int.Parse);
int[,] matrix = new int[row, col];
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
{
matrix[i, j] = arr[i * col + j];
}
RotateArrayOneStepAntiClockwise(matrix, T);
for (int i = 0; i < row; i++)
{
string s = "";
for (int j = 0; j < col; j++)
s += matrix[i, j] + " ";
Console.WriteLine(s.Trim());
}
}
private static void matrixRotation()
{
string s1 = Console.ReadLine();
string[] sA = s1.Split(' ');
int row = Convert.ToInt32(sA[0].Trim());
int col = Convert.ToInt32(sA[1].Trim());
int T = Convert.ToInt32(sA[2].Trim());
int[,] arr = new int[row, col];
int[][] mx = new int[row][];
for (int i = 0; i < row; i++)
{
mx[i] = GetInts(Console.ReadLine(), col);
for (int j = 0; j < col; j++)
arr[i, j] = mx[i][j];
}
RotateArrayOneStepAntiClockwise(arr, T);
for (int i = 0; i < row; i++)
{
string s = "";
for (int j = 0; j < col; j++)
s += arr[i, j] + " ";
Console.WriteLine(s.Trim());
}
}
static int[] GetInts(string inp, int cnt)
{
string[] vals = inp.Split(new char[] { ' ' });
int[] res = new int[cnt];
for (int i = 0; i < cnt; i++)
{
res[i] = Convert.ToInt32(vals[i]);
}
return res;
}
/*
* Dec. 6, 2016
* Review the code,
* follow the code style -
* 1. Do not use abstract variable name
*
*
*/
public static bool RotateArrayOneStepAntiClockwise(int[,] matrix, int steps)
{
if (matrix == null) return false;
int row = matrix.GetLength(0); // row
int col = matrix.GetLength(1); // column
int startRow = 0;
int startCol = 0;
int rows = row;
int cols = col;
while (rows > 1 && cols > 1)
{
int circleLength = 2 * rows + 2 * cols - 4;
int actualSteps = steps % circleLength;
for (int step = 0; step < actualSteps; step++) // move one step anti-clockwise a time
{
int saved = matrix[startRow, startCol];
for (int colIndex = startCol; colIndex < startCol + cols - 1; colIndex++)
matrix[startRow, colIndex] = matrix[startRow, colIndex + 1];
int lastCol = startCol + cols - 1;
for (int rowIndex = startRow; rowIndex < startRow + rows - 1; rowIndex++)
matrix[rowIndex, lastCol] = matrix[rowIndex + 1, lastCol];
int lastRow = startRow + rows - 1;
for (int colIndex = startCol + cols - 1; colIndex >= startCol + 1; colIndex--)
matrix[lastRow, colIndex] = matrix[lastRow, colIndex - 1];
for (int rowIndex = startRow + rows - 1; rowIndex >= 1 + startRow; rowIndex--)
matrix[rowIndex, startCol] = matrix[rowIndex - 1, startCol];
matrix[startRow + 1, startCol] = saved;
}
startRow++;
startCol++;
rows -= 2;
cols -= 2;
}
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment