Simooo

Sequence in Matrix

Jul 24th, 2015
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.45 KB | None | 0 0
  1. using System;
  2.  
  3. class SequenceInMatrix
  4. {
  5.     private static void PrintingResult(string[,] matrix, int bestRow, int bestCol, string bestDirection, int longestSet )
  6.     {
  7.         int counter = 0; // I use this counter in order to remove the last comma of result.
  8.         if(longestSet > 1)
  9.         {
  10.             if (bestDirection == "horizontal")
  11.             {
  12.  
  13.                 for (int col = bestCol; col < bestCol + longestSet; col++)
  14.                 {
  15.                     counter++;
  16.                     if (counter < longestSet)
  17.                     {
  18.                         Console.Write("{0}, ", matrix[bestRow, col]);
  19.                     }
  20.                     else
  21.                     {
  22.                         Console.Write("{0}, ", matrix[bestRow, col]);
  23.                     }
  24.                    
  25.                 }
  26.  
  27.             }
  28.             else if (bestDirection == "diagonalDown")
  29.             {
  30.                
  31.                 for (int row = bestRow; row < bestRow + longestSet; row++)
  32.                 {
  33.                     for (int col = bestCol; col <= bestCol; col++)
  34.                     {
  35.                         counter++;
  36.                         if (counter < longestSet)
  37.                         {
  38.                             Console.Write("{0}, ", matrix[row, col]);
  39.                         }
  40.                         else
  41.                         {
  42.                             Console.Write("{0} ", matrix[row, col]);
  43.                         }
  44.                     }
  45.                     bestCol += 1;
  46.                 }
  47.             }
  48.             else if (bestDirection == "diagonalUp")
  49.             {
  50.                 for (int row = bestRow; row > bestRow - longestSet; row--)
  51.                 {
  52.                     for (int col = bestCol; col < bestCol + longestSet; col++)
  53.                     {
  54.                         counter++;
  55.                         if (counter < longestSet)
  56.                         {
  57.                             Console.Write("{0}, ", matrix[row, col]);
  58.                         }
  59.                         else
  60.                         {
  61.                             Console.Write("{0} ", matrix[row, col]);
  62.                         }
  63.                     }
  64.                 }
  65.             }
  66.             else
  67.             {
  68.                 for (int row = bestRow; row < bestRow + longestSet; row++)
  69.                 {
  70.                     counter++;
  71.                         if (counter < longestSet)
  72.                         {
  73.                             Console.Write("{0}, ", matrix[row, bestCol]);
  74.                         }
  75.                         else
  76.                         {
  77.                             Console.Write("{0} ", matrix[row, bestCol]);
  78.                         }
  79.                 }
  80.             }
  81.         }
  82.         else
  83.         {
  84.             Console.WriteLine("There isn't a set which is longer than the others");
  85.         }
  86.      
  87.     }
  88.     static void Main()
  89.     {
  90.         // CONDITION OF THE TASK
  91.         // We are given a matrix of strings of size N x M. Sequences in the matrix we define as sets of several neighbour elements.
  92.  
  93.         // getting the details of the matrix
  94.         int rows = int.Parse(Console.ReadLine());
  95.         int cols = int.Parse(Console.ReadLine());
  96.         string[,] matrix = new string[rows, cols];
  97.  
  98.         // reading the matrix
  99.         for (int row = 0; row < rows; row++)
  100.         {
  101.             for (int col = 0; col < cols; col++)
  102.             {
  103.                 matrix[row, col] = Console.ReadLine();
  104.             }
  105.         }
  106.  
  107.         // iterating the matrix and taking the longest set.
  108.        
  109.         int bestRow = 0;
  110.         int bestCol = 0;
  111.         int longestSet = 0;
  112.         string bestDirection = "";
  113.        
  114.         for (int row = 0; row < rows; row++)
  115.         {
  116.             for (int col = 0; col < cols; col++)
  117.             {
  118.                 // horizontal
  119.                 int set = 1;
  120.                 int line = row; // row
  121.                 int position = col; // col
  122.                 string direction = "horizontal";
  123.                 while (direction == "horizontal" && position < cols)
  124.                 {
  125.                     position++;
  126.                     while (position < cols && matrix[row, col] == matrix[line, position])
  127.                     {
  128.                         set++;
  129.                         if (set > longestSet)
  130.                         {
  131.                             longestSet = set;
  132.                             bestDirection = direction;
  133.                             bestRow = row;
  134.                             bestCol = col;
  135.                         }
  136.                         position++;
  137.  
  138.                     }
  139.  
  140.                 }
  141.                 // diagonal down.
  142.                 set = 1; // Restarting all the variables.
  143.                 direction = "diagonalDown";
  144.                 line = row;
  145.                 position = col;
  146.                 while (direction == "diagonalDown" && line < rows && position < cols)
  147.                 {
  148.                     position++;
  149.                     line++;
  150.                     while (position < rows && line < cols && matrix[row, col] == matrix[line, position])
  151.                     {
  152.                         set++;
  153.                         if (set > longestSet)
  154.                         {
  155.                             longestSet = set;
  156.                             bestDirection = direction;
  157.                             bestRow = row;
  158.                             bestCol = col;
  159.                         }
  160.                         position++;
  161.                         line++;
  162.                     }
  163.                 }
  164.                 // diagonal up.
  165.                 set = 1; // Restarting all the variables.
  166.                 direction = "diagonalUp";
  167.                 line = row;
  168.                 position = col;
  169.                 while (direction == "diagonalUp" && position >= 0 && line >= 0)
  170.                 {
  171.                     position--;
  172.                     line--;
  173.                     while (position >= 0 && line >= 0 && matrix[row, col] == matrix[line, position])
  174.                     {
  175.                         set++;
  176.                         if (set > longestSet)
  177.                         {
  178.                             longestSet = set;
  179.                             bestDirection = direction;
  180.                             bestRow = row;
  181.                             bestCol = col;
  182.                         }
  183.                         position--;
  184.                         line--;
  185.                     }
  186.                 }
  187.                 // vertical.
  188.                 set = 1; // Restarting all the variables.
  189.                 direction = "vertical";
  190.                 line = row;
  191.                 position = col;
  192.                 while (direction == "vertical" && line < rows)
  193.                 {
  194.                    
  195.                     line++;
  196.                     while (line < rows && matrix[row, col] == matrix[line, position])
  197.                     {
  198.                         set++;
  199.                         if (set > longestSet)
  200.                         {
  201.                             longestSet = set;
  202.                             bestDirection = direction;
  203.                             bestRow = row;
  204.                             bestCol = col;
  205.                         }
  206.                        
  207.                         line++;
  208.                     }
  209.                 }
  210.             }
  211.         }
  212.  
  213.         // Printing the result.
  214.         PrintingResult(matrix, bestRow, bestCol, bestDirection, longestSet);
  215.     }
  216.  
  217.    
  218. }
Advertisement
Add Comment
Please, Sign In to add comment