Advertisement
fbinnzhivko

04.00 Spiral Matrix

Apr 14th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class SpiralMatrix
  5. {
  6.     static void Main()
  7.     {
  8.         int size = int.Parse(Console.ReadLine());
  9.         string input = Console.ReadLine().ToLower();
  10.  
  11.         int[][] matrix = new int[size][];
  12.  
  13.         for (int i = 0; i < size; i++)
  14.         {
  15.             matrix[i] = new int[size];
  16.         }
  17.  
  18.         int row = 0;
  19.         int col = 0;
  20.         int letterIndex = 0;
  21.  
  22.         while (letterIndex < size * size)
  23.         {
  24.             while (col < size && matrix[row][col] == 0)
  25.             {
  26.                 matrix[row][col] = input[letterIndex % input.Length];
  27.                 col++;
  28.                 letterIndex++;
  29.             }
  30.  
  31.             col--;
  32.             row++;
  33.  
  34.             while (row < size && matrix[row][col] == 0)
  35.             {
  36.                 matrix[row][col] = input[letterIndex % input.Length];
  37.                 row++;
  38.                 letterIndex++;
  39.             }
  40.  
  41.             row--;
  42.             col--;
  43.  
  44.             while (col >= 0 && matrix[row][col] == 0)
  45.             {
  46.                 matrix[row][col] = input[letterIndex % input.Length];
  47.                 col--;
  48.                 letterIndex++;
  49.             }
  50.  
  51.             col++;
  52.             row--;
  53.  
  54.             while (row >= 0 && matrix[row][col] == 0)
  55.             {
  56.                 matrix[row][col] = input[letterIndex % input.Length];
  57.                 row--;
  58.                 letterIndex++;
  59.             }
  60.  
  61.             row++;
  62.             col++;
  63.         }
  64.  
  65.         int maxSum = 0;
  66.         int maxIndex = 0;
  67.  
  68.         for (int i = 0; i < size; i++)
  69.         {
  70.             int currentSum = matrix[i].Sum();
  71.             if (currentSum > maxSum)
  72.             {
  73.                 maxSum = currentSum;
  74.                 maxIndex = i;
  75.             }
  76.         }
  77.  
  78.         maxSum -= ('a' - 1) * size;
  79.         maxSum *= 10;
  80.  
  81.         Console.WriteLine("{0} - {1}", maxIndex, maxSum);      
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement