Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Text.RegularExpressions;
- class MatrixShuffle
- {
- static void Main()
- {
- var size = int.Parse(Console.ReadLine());
- var text = Console.ReadLine();
- var matrix = new char[size, size];
- var limit = text.Length;
- var index = 0;
- var row = 0;
- var col = 0;
- while (index < limit)
- {
- while (col < size && matrix[row, col] == 0)
- {
- matrix[row, col++] = text[index++];
- }
- col--;
- row++;
- while (row < size && matrix[row, col] == 0)
- {
- matrix[row++, col] = text[index++];
- }
- row--;
- col--;
- while (col >= 0 && matrix[row, col] == 0)
- {
- matrix[row, col--] = text[index++];
- }
- row--;
- col++;
- while (row >= 0 && matrix[row, col] == 0)
- {
- matrix[row--, col] = text[index++];
- }
- row++;
- col++;
- }
- //first we get all whites and then we get all blacks
- var sentence = GetSentence(matrix, true) + GetSentence(matrix, false);
- var color = IsPalindrome(sentence) ? "#4FE000" : "#E0000F";
- Console.WriteLine("<div style='background-color:{0}'>{1}</div>", color, sentence);
- }
- static string GetSentence(char[,] matrix, bool isWhite)
- {
- var output = string.Empty;
- for (var rows = 0; rows < matrix.GetLength(0); rows++)
- {
- for (var cols = 0; cols < matrix.GetLength(1); cols++)
- {
- if (isWhite && cols % 2 == 0)
- {
- output += matrix[rows, cols];
- }
- else if (!isWhite && cols % 2 == 1)
- {
- output += matrix[rows, cols];
- }
- }
- isWhite = !isWhite;
- }
- return output;
- }
- static string GetReversedString(string input)
- {
- var letters = input.ToCharArray();
- letters = letters.Reverse().ToArray();
- return new string(letters);
- }
- static bool IsPalindrome(string input)
- {
- input = Regex.Replace(input, @"[^a-zA-Z]", "");
- var reversed = GetReversedString(input);
- reversed = Regex.Replace(reversed, @"[^a-zA-Z]", "");
- return input.ToLower() == reversed.ToLower();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement