Advertisement
AlexPopov

Multi-arrs. 3

Jan 22nd, 2013
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.38 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Sequences
  5. {
  6.     class Sequences
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string[,] matrix = new string[,] { { "ha", "fifi", "ho", "hi" }, { "fo", "ha", "hi", "xx" }, { "xxx", "ho", "ha", "xx" } };
  11.             //string[,] matrix = new string[,] { { "s", "qq", "s" }, { "pp", "pp", "s" }, { "pp", "qq", "s" } };
  12.             //string[,] matrix = new string[,] {{ "pop", "trip", "hop", "trop" }, { "ha", "hi", "trop", "hi" }, { "ho", "trop", "he", "ha" }, { "trop", "hum", "hum", "hum" }};
  13.             //string[,] matrix = new string[,] { { "pop", "pop", "ha", "ha" }, { "pop", "pop", "ha", "hi" }, { "pop", "ha", "pop", "hi" }, { "ha", "hi", "hi", "pop" } };
  14.  
  15.             int currentSeq = 1;
  16.             int maxSeq = 1;
  17.             string maxElement = "element";
  18.             int direction = 0;
  19.  
  20.             //check all horizontal seqences going from left to right
  21.             for (int rows = 0; rows < matrix.GetLength(0); rows++)
  22.             {
  23.                 for (int cols = 0; cols < matrix.GetLength(1) - 1; cols++)
  24.                 {
  25.                     if (matrix[rows, cols] == matrix[rows, cols + 1])
  26.                     {
  27.                         currentSeq++;
  28.                     }
  29.                     else
  30.                     {
  31.                         currentSeq = 1;
  32.                     }
  33.  
  34.                     if (maxSeq < currentSeq)
  35.                     {
  36.                         maxSeq = currentSeq;
  37.                         maxElement = matrix[rows, cols];
  38.                         direction = 1;
  39.                     }
  40.                 }
  41.                 currentSeq = 1;
  42.             }
  43.  
  44.             //check all vertical sequenes going top down
  45.             for (int cols = 0; cols < matrix.GetLength(1); cols++)
  46.             {
  47.                 for (int rows = 0; rows < matrix.GetLength(0) - 1; rows++)
  48.                 {
  49.                     if (matrix[rows, cols] == matrix[rows + 1, cols])
  50.                     {
  51.                         currentSeq++;
  52.                     }
  53.                     else
  54.                     {
  55.                         currentSeq = 1;
  56.                     }
  57.  
  58.                     if (maxSeq < currentSeq)
  59.                     {
  60.                         maxSeq = currentSeq;
  61.                         maxElement = matrix[rows, cols];
  62.                         direction = 2;
  63.                     }
  64.                 }
  65.                 currentSeq = 1;
  66.             }
  67.  
  68.             //check all diagonals going from top left to down right
  69.             for (int i = 0; i < matrix.GetLength(0) - 1; i++)
  70.             {
  71.                 for (int j = 0; j < matrix.GetLength(1) - 1; j++)
  72.                 {
  73.                     for (int rows = i, cols = j; rows < matrix.GetLength(0) - 1 && cols < matrix.GetLength(1) - 1; rows++, cols++)
  74.                     {
  75.                         if (matrix[rows, cols] == matrix[rows + 1, cols + 1])
  76.                         {
  77.                             currentSeq++;
  78.                         }
  79.                         else
  80.                         {
  81.                             currentSeq = 1;
  82.                         }
  83.  
  84.                         if (maxSeq < currentSeq)
  85.                         {
  86.                             maxSeq = currentSeq;
  87.                             maxElement = matrix[rows, cols];
  88.                             direction = 3;
  89.                         }
  90.                     }
  91.                     currentSeq = 1;
  92.                 }
  93.             }
  94.  
  95.             //check all diagonals going from top right to down left
  96.             for (int i = 0; i < matrix.GetLength(0) - 1; i++)
  97.             {
  98.                 for (int j = 1; j < matrix.GetLength(1); j++)
  99.                 {
  100.                     for (int rows = i, cols = j; rows < matrix.GetLength(0) - 1 && cols > 0; rows++, cols--)
  101.                     {
  102.                         if (matrix[rows, cols] == matrix[rows + 1, cols - 1])
  103.                         {
  104.                             currentSeq++;
  105.                         }
  106.                         else
  107.                         {
  108.                             currentSeq = 1;
  109.                         }
  110.  
  111.                         if (maxSeq < currentSeq)
  112.                         {
  113.                             maxSeq = currentSeq;
  114.                             maxElement = matrix[rows, cols];
  115.                             direction = 4;
  116.                         }
  117.                     }
  118.                     currentSeq = 1;
  119.                 }
  120.             }
  121.  
  122.             switch (direction)
  123.             {
  124.                 case 1:
  125.                     Console.WriteLine("Element \"{0}\" repeats {1} times horizontally.", maxElement, maxSeq);
  126.                     break;
  127.                 case 2:
  128.                     Console.WriteLine("Element \"{0}\" repeats {1} times vertically.", maxElement, maxSeq);
  129.                     break;
  130.                 case 3:
  131.                     Console.WriteLine("Element \"{0}\" repeats {1} times diagonally from top left to bottom right.", maxElement, maxSeq);
  132.                     break;
  133.                 case 4:
  134.                     Console.WriteLine("Element \"{0}\" repeats {1} times diagonally from top right to bottom left.", maxElement, maxSeq);
  135.                     break;
  136.                 default:
  137.                     Console.WriteLine("No repetitions of elements.");
  138.                     break;
  139.             }
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement