Advertisement
Filkolev

Matrix Shuffle

May 27th, 2015
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5.  
  6. public class MatrixShuffle
  7. {
  8.     public static void Main()
  9.     {
  10.         int size = int.Parse(Console.ReadLine());
  11.         string text = Console.ReadLine();
  12.  
  13.         char[,] matrix = new char[size, size];
  14.  
  15.         FillMatrix(text, size, matrix);
  16.  
  17.         string sentence = GetSentence(size, matrix);
  18.  
  19.         Console.WriteLine(
  20.             "<div style='background-color:{0}'>{1}</div>",
  21.             IsPalidrome(sentence) ? "#4FE000" : "#E0000F",
  22.             sentence);
  23.     }
  24.  
  25.     private static bool IsPalidrome(string sentence)
  26.     {
  27.         string original = Regex.Replace(sentence, @"[^a-zA-Z]+", string.Empty).ToLower();
  28.  
  29.         string reversed = new string(original.Reverse().ToArray());
  30.  
  31.         return original == reversed;
  32.     }
  33.  
  34.     private static string GetSentence(int size, char[,] matrix)
  35.     {
  36.         StringBuilder sentence = new StringBuilder();
  37.  
  38.         for (int row = 0; row < size; row++)
  39.         {
  40.             for (int col = 0; col < size; col++)
  41.             {
  42.                 if (row % 2 == 0 && col % 2 == 0)
  43.                 {
  44.                     sentence.Append(matrix[row, col]);
  45.                 }
  46.                 else if (row % 2 != 0 && col % 2 != 0)
  47.                 {
  48.                     sentence.Append(matrix[row, col]);
  49.                 }
  50.             }
  51.         }
  52.  
  53.         for (int row = 0; row < size; row++)
  54.         {
  55.             for (int col = 0; col < size; col++)
  56.             {
  57.                 if (row % 2 == 0 && col % 2 != 0)
  58.                 {
  59.                     sentence.Append(matrix[row, col]);
  60.                 }
  61.                 else if (row % 2 != 0 && col % 2 == 0)
  62.                 {
  63.                     sentence.Append(matrix[row, col]);
  64.                 }
  65.             }
  66.         }
  67.  
  68.         return sentence.ToString();
  69.     }
  70.  
  71.     private static void FillMatrix(string text, int size, char[,] matrix)
  72.     {
  73.         int currentIndex = 0;
  74.         int currentRow = 0;
  75.         int currentCol = 0;
  76.  
  77.         while (currentIndex < text.Length)
  78.         {
  79.             while (currentCol < size && matrix[currentRow, currentCol] == '\0')
  80.             {
  81.                 matrix[currentRow, currentCol] = text[currentIndex];
  82.                 currentIndex++;
  83.                 currentCol++;
  84.             }
  85.  
  86.             currentCol--;
  87.             currentRow++;
  88.  
  89.             while (currentRow < size && matrix[currentRow, currentCol] == '\0')
  90.             {
  91.                 matrix[currentRow, currentCol] = text[currentIndex];
  92.                 currentIndex++;
  93.                 currentRow++;
  94.             }
  95.  
  96.             currentRow--;
  97.             currentCol--;
  98.  
  99.             while (currentCol >= 0 && matrix[currentRow, currentCol] == '\0')
  100.             {
  101.                 matrix[currentRow, currentCol] = text[currentIndex];
  102.                 currentIndex++;
  103.                 currentCol--;
  104.             }
  105.  
  106.             currentCol++;
  107.             currentRow--;
  108.  
  109.             while (currentRow >= 0 && matrix[currentRow, currentCol] == '\0')
  110.             {
  111.                 matrix[currentRow, currentCol] = text[currentIndex];
  112.                 currentIndex++;
  113.                 currentRow--;
  114.             }
  115.  
  116.             currentRow++;
  117.             currentCol++;
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement