Advertisement
stak441

MultidimensionalArrays - problem 3

Jan 15th, 2013
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _03.LongestSequence
  7. {
  8.     class LongestSequence
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string[,] matrix = {                                    //Used for testing purposes
  13.                             { "ha", "fifi", "ho", "hi" },
  14.                             { "fo", "ha", "hi", "xx" },
  15.                             { "xxx", "ho", "ha", "xx" }
  16.                             };
  17.  
  18.             int n = matrix.GetLength(0);
  19.             int m = matrix.GetLength(1);
  20.             int maxLength = 1;
  21.             string mostFrequent = matrix[0, 0];
  22.  
  23.             for (int row = 0; row < n; row++)
  24.             {
  25.                 for (int col = 0; col < m; col++)
  26.                 {
  27.                     int tempRow = row;
  28.                     int tempCol = col;
  29.                     int counter = 1;
  30.                     //=========================================== checking rows
  31.                     while (tempRow < n-1)
  32.                     {
  33.                         if (matrix[tempRow + 1, col] == matrix[tempRow, col])
  34.                         {
  35.                             counter++;
  36.                         }
  37.                         else
  38.                         {
  39.                             counter = 1;
  40.                             break;
  41.                         }
  42.  
  43.                         if (counter > maxLength)
  44.                         {
  45.                             maxLength = counter;
  46.                             mostFrequent = matrix[tempRow, col];
  47.                         }
  48.                         tempRow++;
  49.                     }
  50.  
  51.                     tempRow = row;
  52.                     tempCol = col;
  53.                     counter = 1;
  54.  
  55.                     //=========================================== checking columns
  56.                     while (tempCol < m - 1)
  57.                     {
  58.                         if (matrix[row, tempCol + 1] == matrix[row, tempCol])
  59.                         {
  60.                             counter++;
  61.                         }
  62.                         else
  63.                         {
  64.                             counter = 1;
  65.                             break;
  66.                         }
  67.  
  68.                         if (counter > maxLength)
  69.                         {
  70.                             maxLength = counter;
  71.                             mostFrequent = matrix[row, tempCol];
  72.                         }
  73.                         tempCol++;
  74.                     }
  75.  
  76.                     tempRow = row;
  77.                     tempCol = col;
  78.                     counter = 1;
  79.  
  80.                     //=========================================== checking diagonals right-down
  81.                     while (tempRow < n - 1 && tempCol < m - 1)
  82.                     {
  83.                         if (matrix[tempRow, tempCol] == matrix[tempRow + 1, tempCol + 1])
  84.                         {
  85.                             counter++;
  86.                         }
  87.                         else
  88.                         {
  89.                             counter = 1;
  90.                             break;
  91.                         }
  92.  
  93.                         if (counter > maxLength)
  94.                         {
  95.                             maxLength = counter;
  96.                             mostFrequent = matrix[tempRow, tempCol];
  97.                         }
  98.                         tempRow++;
  99.                         tempCol++;
  100.                     }
  101.  
  102.                     //=========================================== checking diagonals right-up
  103.                     while (tempRow >= 1 && tempCol < m - 1)
  104.                     {
  105.                         if (matrix[tempRow, tempCol] == matrix[tempRow - 1, tempCol + 1])
  106.                         {
  107.                             counter++;
  108.                         }
  109.                         else
  110.                         {
  111.                             counter = 1;
  112.                             break;
  113.                         }
  114.  
  115.                         if (counter > maxLength)
  116.                         {
  117.                             maxLength = counter;
  118.                             mostFrequent = matrix[tempRow, tempCol];
  119.                         }
  120.                         tempRow--;
  121.                         tempCol++;
  122.                     }
  123.  
  124.                 }
  125.             }
  126.  
  127.             //Print result===================================
  128.  
  129.             Console.WriteLine("Max length is: {0}", maxLength);
  130.             for (int i = 0; i < maxLength; i++)
  131.             {
  132.                 Console.Write(mostFrequent + ", ");
  133.             }
  134.             Console.WriteLine();
  135.            
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement