Advertisement
simonses

03. BiggerMatrixSum

Jan 23rd, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.52 KB | None | 0 0
  1. using System;
  2.  
  3. class CheckStringEqualSequence
  4. {
  5.     static void Main()
  6.     {
  7.         string[,] matrix = {
  8.             {"01", "02", "03", "11", "06", "06", "07", "15"},
  9.             {"09", "10", "11", "12", "06", "14", "15", "16"},
  10.             {"17", "11", "19", "21", "22", "01", "16", "06"},
  11.             {"26", "17", "28", "29", "15", "16", "32", "33"},
  12.             {"34", "35", "36", "15", "38", "39", "40", "41"},
  13.         };
  14.  
  15.         int countVertical = 0;
  16.         int countHorizontal = 0;
  17.         int countDiagonalLeftUp = 0;
  18.         int countDiagoanlRightUp = 0;
  19.         string strVertical = null;
  20.         string strHorizontal = null;
  21.         string strDiagonalLeftUp = null;
  22.         string strDiagonalRightUp = null;
  23.  
  24.         // Horizontal - row
  25.         for (int row = 0; row < matrix.GetLength(0); row++)
  26.         {
  27.             int currentCount = 0;
  28.             for (int col = 0; col < matrix.GetLength(1) - 1; col++)
  29.             {
  30.                 if (matrix[row, col] == matrix[row, col + 1])
  31.                 {
  32.                     currentCount++;
  33.                 }
  34.                 else
  35.                 {
  36.                     currentCount = 0;
  37.                 }
  38.                 if (countHorizontal < currentCount)
  39.                 {
  40.                     countHorizontal = currentCount;
  41.                     strHorizontal = matrix[row, col];
  42.                 }
  43.             }
  44.         }
  45.  
  46.         // Vertical - col
  47.         for (int col = 0; col < matrix.GetLength(1); col++)
  48.         {
  49.             int currentCount = 0;
  50.             for (int row = 0; row < matrix.GetLength(0) - 1; row++)
  51.             {
  52.                 if (matrix[row, col] == matrix[row + 1, col])
  53.                 {
  54.                     currentCount++;
  55.                 }
  56.                 else
  57.                 {
  58.                     currentCount = 0;
  59.                 }
  60.                 if (countVertical < currentCount)
  61.                 {
  62.                     countVertical = currentCount;
  63.                     strVertical = matrix[row, col];
  64.                 }
  65.             }
  66.         }
  67.  
  68.         // LeftUp - Diagonal --- (last row - 1 ) to (first row)
  69.         int myCount1 = 1;
  70.         for (int row = matrix.GetLength(0) - 2; row >= 0; row--)
  71.         {
  72.             int currentCount = 0;
  73.             for (int col = 0; col < myCount1; col++)
  74.             {
  75.                 if (matrix[row + col, col] == matrix[row + col + 1, col + 1])
  76.                 {
  77.                     currentCount++;
  78.                 }
  79.                 else
  80.                 {
  81.                     currentCount = 0;
  82.                 }
  83.                 if (countDiagonalLeftUp <= currentCount)
  84.                 {
  85.                     countDiagonalLeftUp = currentCount;
  86.                     strDiagonalLeftUp = matrix[row + col, col];
  87.                 }
  88.             }
  89.             if (matrix.GetLength(0) - 1 - row < matrix.GetLength(1) - 1) // Check the count ROW or COL is bigger!!!
  90.             {
  91.                 myCount1++;
  92.             }
  93.             else
  94.             {
  95.                 myCount1 = matrix.GetLength(1) - 1;
  96.             }
  97.         }
  98.  
  99.         // LeftUp - Diagonal --- (first row ) to (last col)
  100.         int myCount2 = 0;
  101.         if (matrix.GetLength(0) >= matrix.GetLength(1))
  102.         {
  103.             myCount2 = matrix.GetLength(1) - 2;
  104.         }
  105.         else
  106.         {
  107.             myCount2 = matrix.GetLength(0) - 1;
  108.         }
  109.        
  110.         for (int col = 1; col < matrix.GetLength(1) - 1; col++)
  111.         {
  112.             int currentCount = 0;
  113.             for (int row = 0; row < myCount2; row++)
  114.             {
  115.                 if (matrix[row, col + row] == matrix[row + 1, col + row + 1])
  116.                 {
  117.                     currentCount++;
  118.                 }
  119.                 else
  120.                 {
  121.                     currentCount = 0;
  122.                 }
  123.                 if (countDiagonalLeftUp <= currentCount)
  124.                 {
  125.                     countDiagonalLeftUp = currentCount;
  126.                     strDiagonalLeftUp = matrix[row, col + row];
  127.                 }
  128.             }
  129.             if (matrix.GetLength(1) - 2 - col < matrix.GetLength(0) - 1) // Check the count ROW or COL is bigger!!!
  130.             {
  131.                 myCount2--;
  132.             }
  133.             else
  134.             {
  135.                 myCount2 = matrix.GetLength(0) - 1;
  136.             }
  137.         }
  138.  
  139.         // RightUp - Diagonal --- (last row - 1 ) to (first row)
  140.         int myCount3 = matrix.GetLength(1) - 2;
  141.        
  142.         for (int row = matrix.GetLength(0) - 2; row >= 0; row--)
  143.         {
  144.             int currentCount = 0;
  145.             for (int col = matrix.GetLength(1) - 1; col > myCount3; col--)
  146.             {
  147.                 int ss = (matrix.GetLength(1) - 1 - col);
  148.                 if (matrix[row + ss, col] == matrix[row + ss + 1, col - 1])
  149.                 {
  150.                     currentCount++;
  151.                 }
  152.                 else
  153.                 {
  154.                     currentCount = 0;
  155.                 }
  156.                 if (countDiagoanlRightUp < currentCount)
  157.                 {
  158.                     countDiagoanlRightUp = currentCount;
  159.                     strDiagonalRightUp = matrix[row + ss, col];
  160.                 }
  161.             }
  162.             if (matrix.GetLength(0) - 1 - row < matrix.GetLength(1) - 1) // Check the count ROW or COL is bigger!!!
  163.             {
  164.                 myCount3--;
  165.             }
  166.             else
  167.             {
  168.                 myCount3 = matrix.GetLength(1) - 1;
  169.             }
  170.         }
  171.  
  172.         // RightUp - Diagonal --- (first row ) to (last col)
  173.         int myCount4 = 0;
  174.         if (matrix.GetLength(0) >= matrix.GetLength(1))
  175.         {
  176.             myCount4 = matrix.GetLength(1) - 2;
  177.         }
  178.         else
  179.         {
  180.             myCount4 = matrix.GetLength(0) - 1;
  181.         }
  182.  
  183.         for (int col = matrix.GetLength(1) - 1; col > 0; col--)
  184.         {
  185.             int currentCount = 0;
  186.             for (int row = 0; row < myCount4; row++)
  187.             {
  188.                 if (matrix[row, col - row - 1] == matrix[row + 1, col - row - 2])
  189.                 {
  190.                     currentCount++;
  191.                 }
  192.                 else
  193.                 {
  194.                     currentCount = 0;
  195.                 }
  196.                 if (countDiagoanlRightUp < currentCount)
  197.                 {
  198.                     countDiagoanlRightUp = currentCount;
  199.                     strDiagonalRightUp = matrix[row, col - row - 1];
  200.                 }
  201.             }
  202.             if (col <= matrix.GetLength(0)) // Check the count ROW or COL is bigger!!!
  203.             {
  204.                 myCount4--;
  205.             }
  206.             else
  207.             {
  208.                 myCount4 = matrix.GetLength(0) - 1;
  209.             }
  210.         }
  211.  
  212.         // Print the result
  213.         Console.Write("The Horizontal is: ");
  214.  
  215.         for (int i = 0; i <= countHorizontal; i++)
  216.         {
  217.             Console.Write(strHorizontal + " ");
  218.         }
  219.         Console.WriteLine();
  220.  
  221.         Console.Write("The Vertical is: ");
  222.  
  223.         for (int i = 0; i <= countVertical; i++)
  224.         {
  225.             Console.Write(strVertical + " ");
  226.         }
  227.         Console.WriteLine();
  228.  
  229.         Console.Write("The LeftUp Diagonal is: ");
  230.         for (int i = 0; i <= countDiagonalLeftUp; i++)
  231.         {
  232.             Console.Write(strDiagonalLeftUp + " ");
  233.         }
  234.         Console.WriteLine();
  235.  
  236.         Console.Write("The RightUp Diagonal is: ");
  237.         for (int i = 0; i <= countDiagoanlRightUp; i++)
  238.         {
  239.             Console.Write(strDiagonalRightUp + " ");
  240.         }
  241.         Console.WriteLine();
  242.     }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement