Advertisement
simonradev

SpiralMatrix

Mar 31st, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.69 KB | None | 0 0
  1. namespace SpiralMatrix
  2. {
  3.     using System;
  4.     using System.Text;
  5.  
  6.     public class SpiralMatrix
  7.     {
  8.         public static char[,] matrix;
  9.  
  10.         public static void Main()
  11.         {
  12.             int dimensionsOfTheMatrix = int.Parse(Console.ReadLine());
  13.             string keyword = Console.ReadLine().ToLower();
  14.  
  15.             matrix = new char[dimensionsOfTheMatrix, dimensionsOfTheMatrix];
  16.             FillTheMatrixWithTheKeyword(keyword, dimensionsOfTheMatrix * dimensionsOfTheMatrix);
  17.  
  18.             Tuple<int, int> bestRowAndWeight = GetTheBestRowWeight();
  19.  
  20.             Console.WriteLine($"{bestRowAndWeight.Item1} - {bestRowAndWeight.Item2}");
  21.         }
  22.  
  23.         private static Tuple<int, int> GetTheBestRowWeight()
  24.         {
  25.             int bestWeight = int.MinValue;
  26.             int bestRow = 0;
  27.             for (int row = 0; row < matrix.GetLength(0); row++)
  28.             {
  29.                 int currWeight = 0;
  30.                 for (int col = 0; col < matrix.GetLength(1); col++)
  31.                 {
  32.                     char symbol = matrix[row, col];
  33.  
  34.                     int weightOfSymbol = (symbol - 'a' + 1) * 10;
  35.  
  36.                     currWeight += weightOfSymbol;
  37.  
  38.                     if (currWeight > bestWeight)
  39.                     {
  40.                         bestWeight = currWeight;
  41.                         bestRow = row;
  42.                     }
  43.                 }
  44.             }
  45.  
  46.             Tuple<int, int> toReturn = Tuple.Create(bestRow, bestWeight);
  47.  
  48.             return toReturn;
  49.         }
  50.  
  51.         private static void FillTheMatrixWithTheKeyword(string keyword, int boundary)
  52.         {
  53.             int row = 0;
  54.             int col = 0;
  55.             int currRowIncrementor = 1;
  56.             int currColIncrementor = 1;
  57.             int indexOfKeyword = 0;
  58.             bool directionIsLeftOrRight = true;
  59.             while (true)
  60.             {
  61.                 matrix[row, col] = keyword[indexOfKeyword % keyword.Length];
  62.  
  63.                 indexOfKeyword++;
  64.  
  65.                 if (indexOfKeyword == boundary)
  66.                 {
  67.                     break;
  68.                 }
  69.  
  70.                 DirectionIsLeftOrRight:
  71.                 if (directionIsLeftOrRight)
  72.                 {
  73.                     col += currColIncrementor;
  74.  
  75.                     bool needToChangeDirection = CheckIfWeNeedToChangeDirection(row, col);
  76.  
  77.                     if (needToChangeDirection)
  78.                     {
  79.                         col -= currColIncrementor;
  80.                         currColIncrementor = currColIncrementor == 1 ? -1 : 1;
  81.                         directionIsLeftOrRight = false;
  82.                     }
  83.                 }
  84.  
  85.                 if (!directionIsLeftOrRight)
  86.                 {
  87.                     row += currRowIncrementor;
  88.  
  89.                     bool needToChangeDirection = CheckIfWeNeedToChangeDirection(row, col);
  90.  
  91.                     if (needToChangeDirection)
  92.                     {
  93.                         row -= currRowIncrementor;
  94.                         currRowIncrementor = currRowIncrementor == 1 ? -1 : 1;
  95.                         directionIsLeftOrRight = true;
  96.  
  97.                         goto DirectionIsLeftOrRight;
  98.                     }
  99.                 }
  100.             }
  101.         }
  102.  
  103.         private static bool CheckIfWeNeedToChangeDirection(int row, int col)
  104.         {
  105.             bool needToChangeDirection = false;
  106.  
  107.             if (row == matrix.GetLength(0) ||
  108.                 row == -1 ||
  109.                 col == matrix.GetLength(1) ||
  110.                 col == -1 ||
  111.                 matrix[row, col] != '\0')
  112.             {
  113.                 needToChangeDirection = true;
  114.             }
  115.  
  116.             return needToChangeDirection;
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement