Advertisement
simonses

lastMatrix

Jan 22nd, 2013
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.80 KB | None | 0 0
  1. using System;
  2.  
  3. class CheckStringEqualSequence
  4. {
  5.     static void Main()
  6.     {
  7.         string[,] matrix = {
  8.             {"ha", "ha", "hi", "hi"},
  9.             {"fo", "ha", "ha", "xx"},
  10.             {"xxx", "fo", "ha", "ha"},
  11.             {"xxx", "1", "fo", "ha"},
  12.             {"xxx", "1", "4", "fo"},
  13.            
  14.         };
  15.         //string[,] matrix = {
  16.         //    {"s", "qq", "s"},
  17.         //    {"pp", "pp", "s"},
  18.         //    {"pp", "qq", "s"},
  19.         //};
  20.  
  21.         int countVertical = 0;
  22.         int countHorizontal = 0;
  23.         int countDiagonalLeftUp = 0;
  24.         int countDiagoanlRightUp = 0;
  25.         string strVertical = null;
  26.         string strHorizontal = null;
  27.         string strDiagonalLeftUp = null;
  28.         string strDiagonalRightUp = null;
  29.  
  30.         // Horizontal - row
  31.         for (int row = 0; row < matrix.GetLength(0); row++)
  32.         {
  33.             int currentCount = 0;
  34.             for (int col = 0; col < matrix.GetLength(1) - 1; col++)
  35.             {
  36.                 if (matrix[row, col] == matrix[row, col + 1])
  37.                 {
  38.                     currentCount++;
  39.                 }
  40.                 else
  41.                 {
  42.                     currentCount = 0;
  43.                 }
  44.                 if (countHorizontal < currentCount)
  45.                 {
  46.                     countHorizontal = currentCount;
  47.                     strHorizontal = matrix[row, col];
  48.                 }
  49.             }
  50.         }
  51.  
  52.         // Vertical - col
  53.         for (int col = 0; col < matrix.GetLength(1); col++)
  54.         {
  55.             int currentCount = 0;
  56.             for (int row = 0; row < matrix.GetLength(0) - 1; row++)
  57.             {
  58.                 if (matrix[row, col] == matrix[row + 1, col])
  59.                 {
  60.                     currentCount++;
  61.                 }
  62.                 else
  63.                 {
  64.                     currentCount = 0;
  65.                 }
  66.                 if (countVertical < currentCount)
  67.                 {
  68.                     countVertical = currentCount;
  69.                     strVertical = matrix[row, col];
  70.                 }
  71.             }
  72.         }
  73.  
  74.         // LeftUp - Diagonal --- (last row - 1 ) to (first row)
  75.         int myCount1 = 1;
  76.         for (int row = matrix.GetLength(0) - 2; row >= 0; row--)
  77.         {
  78.             int currentCount = 0;
  79.             for (int col = 0; col < myCount1; col++)
  80.             {
  81.                 if (matrix[row + col, col] == matrix[row + col + 1, col + 1])
  82.                 {
  83.                     currentCount++;
  84.                 }
  85.                 else
  86.                 {
  87.                     currentCount = 0;
  88.                 }
  89.                 if (countDiagonalLeftUp <= currentCount)
  90.                 {
  91.                     countDiagonalLeftUp = currentCount;
  92.                     strDiagonalLeftUp = matrix[row + col, col];
  93.                 }
  94.             }
  95.             if (matrix.GetLength(0) - 1 - row < matrix.GetLength(1) - 1) // Check the count ROW or COL is bigger!!!
  96.             {
  97.                 myCount1++;
  98.             }
  99.             else
  100.             {
  101.                 myCount1 = matrix.GetLength(1) - 1;
  102.             }
  103.         }
  104.  
  105.         // LeftUp - Diagonal --- (first row ) to (last col)
  106.         int myCount2 = matrix.GetLength(0) - 1;
  107.         for (int col = 1; col < matrix.GetLength(1) - 1; col++)
  108.         {
  109.             int currentCount = 0;
  110.             for (int row = 0; row < myCount2; row++)
  111.             {
  112.                 if (matrix[row, col + row] == matrix[row + 1, col + row + 1])
  113.                 {
  114.                     currentCount++;
  115.                 }
  116.                 else
  117.                 {
  118.                     currentCount = 0;
  119.                 }
  120.                 if (countDiagonalLeftUp <= currentCount)
  121.                 {
  122.                     countDiagonalLeftUp = currentCount;
  123.                     strDiagonalLeftUp = matrix[row, col + row];
  124.                 }
  125.             }
  126.             if (matrix.GetLength(1) - 1 - col < matrix.GetLength(0) - 1) // Check the count ROW or COL is bigger!!!
  127.             {
  128.                 myCount2--;
  129.             }
  130.             else
  131.             {
  132.                 myCount2 = matrix.GetLength(0) - 1;
  133.             }
  134.         }
  135.  
  136.         // Print the result
  137.         Console.Write("The Horizontal is: ");
  138.  
  139.         for (int i = 0; i <= countHorizontal; i++)
  140.         {
  141.             Console.Write(strHorizontal + " ");
  142.         }
  143.         Console.WriteLine();
  144.  
  145.         Console.Write("The Vertical is: ");
  146.  
  147.         for (int i = 0; i <= countVertical; i++)
  148.         {
  149.             Console.Write(strVertical + " ");
  150.         }
  151.         Console.WriteLine();
  152.  
  153.         Console.Write("The LeftUp Diagonal is: ");
  154.         for (int i = 0; i <= countDiagonalLeftUp; i++)
  155.         {
  156.             Console.Write(strDiagonalLeftUp + " ");
  157.         }
  158.         Console.WriteLine();
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement