Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace SpiralMatrix
- {
- using System;
- using System.Text;
- public class SpiralMatrix
- {
- public static char[,] matrix;
- public static void Main()
- {
- int dimensionsOfTheMatrix = int.Parse(Console.ReadLine());
- string keyword = Console.ReadLine().ToLower();
- matrix = new char[dimensionsOfTheMatrix, dimensionsOfTheMatrix];
- FillTheMatrixWithTheKeyword(keyword, dimensionsOfTheMatrix * dimensionsOfTheMatrix);
- Tuple<int, int> bestRowAndWeight = GetTheBestRowWeight();
- Console.WriteLine($"{bestRowAndWeight.Item1} - {bestRowAndWeight.Item2}");
- }
- private static Tuple<int, int> GetTheBestRowWeight()
- {
- int bestWeight = int.MinValue;
- int bestRow = 0;
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- int currWeight = 0;
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- char symbol = matrix[row, col];
- int weightOfSymbol = (symbol - 'a' + 1) * 10;
- currWeight += weightOfSymbol;
- if (currWeight > bestWeight)
- {
- bestWeight = currWeight;
- bestRow = row;
- }
- }
- }
- Tuple<int, int> toReturn = Tuple.Create(bestRow, bestWeight);
- return toReturn;
- }
- private static void FillTheMatrixWithTheKeyword(string keyword, int boundary)
- {
- int row = 0;
- int col = 0;
- int currRowIncrementor = 1;
- int currColIncrementor = 1;
- int indexOfKeyword = 0;
- bool directionIsLeftOrRight = true;
- while (true)
- {
- matrix[row, col] = keyword[indexOfKeyword % keyword.Length];
- indexOfKeyword++;
- if (indexOfKeyword == boundary)
- {
- break;
- }
- DirectionIsLeftOrRight:
- if (directionIsLeftOrRight)
- {
- col += currColIncrementor;
- bool needToChangeDirection = CheckIfWeNeedToChangeDirection(row, col);
- if (needToChangeDirection)
- {
- col -= currColIncrementor;
- currColIncrementor = currColIncrementor == 1 ? -1 : 1;
- directionIsLeftOrRight = false;
- }
- }
- if (!directionIsLeftOrRight)
- {
- row += currRowIncrementor;
- bool needToChangeDirection = CheckIfWeNeedToChangeDirection(row, col);
- if (needToChangeDirection)
- {
- row -= currRowIncrementor;
- currRowIncrementor = currRowIncrementor == 1 ? -1 : 1;
- directionIsLeftOrRight = true;
- goto DirectionIsLeftOrRight;
- }
- }
- }
- }
- private static bool CheckIfWeNeedToChangeDirection(int row, int col)
- {
- bool needToChangeDirection = false;
- if (row == matrix.GetLength(0) ||
- row == -1 ||
- col == matrix.GetLength(1) ||
- col == -1 ||
- matrix[row, col] != '\0')
- {
- needToChangeDirection = true;
- }
- return needToChangeDirection;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement