Advertisement
alidzhikov

SequenceInMatrix

May 8th, 2015
972
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class SequenceInMatrix
  5. {
  6.     static void Main()
  7.     {
  8.         List<List<string>> matrix = new List<List<string>>();
  9.  
  10.         string currentRowContents = Console.ReadLine();
  11.         string[] currentRowTokens;
  12.         for (int row = 0; !String.IsNullOrEmpty(currentRowContents); row++)
  13.         {
  14.             matrix.Add(new List<string>());
  15.             currentRowTokens = currentRowContents.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
  16.             for (int col = 0; col < currentRowTokens.Length; col++)
  17.             {
  18.                 matrix[row].Add(currentRowTokens[col]);
  19.             }
  20.  
  21.             currentRowContents = Console.ReadLine();
  22.         }
  23.  
  24.         int currentLength = 1;
  25.         int bestLength = int.MinValue;
  26.         string bestValue = String.Empty;
  27.         for (int row = 0; row < matrix.Count; row++)
  28.         {
  29.             for (int col = 0; col < matrix[row].Count; col++)
  30.             {
  31.                 currentLength = CheckVertical(matrix, row, col, matrix[row][col]);
  32.                 if (currentLength > bestLength)
  33.                 {
  34.                     bestLength = currentLength;
  35.                     bestValue = matrix[row][col];
  36.                 }
  37.                 currentLength = CheckHorizontal(matrix, row, col, matrix[row][col]);
  38.                 if (currentLength > bestLength)
  39.                 {
  40.                     bestLength = currentLength;
  41.                     bestValue = matrix[row][col];
  42.                 }
  43.                 currentLength = CheckDiagonal(matrix, row, col, matrix[row][col]);
  44.                 if (currentLength > bestLength)
  45.                 {
  46.                     bestLength = currentLength;
  47.                     bestValue = matrix[row][col];
  48.                 }
  49.             }
  50.         }
  51.  
  52.         Console.Write(bestValue);
  53.         for (int i = 1; i < bestLength; i++)
  54.         {
  55.             Console.Write(", {0}", bestValue);
  56.         }
  57.         Console.WriteLine();
  58.     }
  59.  
  60.     private static int CheckDiagonal(List<List<string>> matrix, int row, int col, string currentValue)
  61.     {
  62.         int currentLength = 1;
  63.         for (int i = row, j = col; i < matrix.Count && j < matrix[row].Count; i++, j++)
  64.         {
  65.             if (i + 1 < matrix.Count && j + 1 < matrix[i + 1].Count && matrix[i + 1][j + 1].Equals(currentValue))
  66.             {
  67.                 currentLength++;
  68.             }
  69.             else
  70.             {
  71.                 break;
  72.             }
  73.         }
  74.         return currentLength;
  75.     }
  76.  
  77.     private static int CheckHorizontal(List<List<string>> matrix, int row, int col, string currentValue)
  78.     {
  79.         int currentLength = 1;
  80.         for (int i = col; i < matrix[row].Count; i++)
  81.         {
  82.             if (i + 1 < matrix[row].Count && matrix[row][i + 1].Equals(currentValue))
  83.             {
  84.                 currentLength++;
  85.             }
  86.             else
  87.             {
  88.                 break;
  89.             }
  90.         }
  91.         return currentLength;
  92.     }
  93.  
  94.     private static int CheckVertical(List<List<string>> matrix, int row, int col, string currentValue)
  95.     {
  96.         int currentLength = 1;
  97.         for (int i = row; i < matrix.Count; i++)
  98.         {
  99.             if (i + 1 < matrix.Count && matrix[i + 1][col].Equals(currentValue))
  100.             {
  101.                 currentLength++;
  102.             }
  103.             else
  104.             {
  105.                 break;
  106.             }        
  107.         }
  108.         return currentLength;
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement