Advertisement
Guest User

Untitled

a guest
Jul 11th, 2015
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SequenceInMatrix
  8. {
  9.     class SequenceInMatrix
  10.     {
  11.        
  12.         static List<string> final = new List<string>();
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             int n = int.Parse(Console.ReadLine());
  17.             int m = int.Parse(Console.ReadLine());
  18.             string[,] matrix = new string[n, m];
  19.  
  20.             FillMatrix(matrix);
  21.             MatrixHorizontal(matrix);
  22.             MatrixVertical(matrix);
  23.             MatrixDiagonal(matrix);
  24.             Console.WriteLine(String.Join(", ", final));
  25.         }
  26.  
  27.         private static void MatrixDiagonal(string[,] matrix)
  28.         {
  29.             for (int i = 0; i < matrix.GetLength(1) - 1; i++)
  30.             {
  31.                 List<string> tempList = new List<string>();
  32.                 tempList.Add(matrix[0, i]);
  33.                 for (int j1 = i + 1, j2 = i + 1; j1 < matrix.GetLength(0); j1++, j2++)
  34.                 {
  35.                     if (matrix[j1, j2] == matrix[j1 - 1, j2 - 1])
  36.                     {
  37.                         tempList.Add(matrix[j1, j2]);
  38.                         if (j1 == matrix.GetLength(0) - 1)
  39.                         {
  40.                             if (tempList.Count > final.Count)
  41.                             {
  42.                                 final.Clear();
  43.                                 foreach (var item in tempList)
  44.                                 {
  45.                                     final.Add(item);
  46.                                 }
  47.                                 tempList.Clear();
  48.                                 tempList.Add(matrix[j1, j2]);
  49.                             }
  50.                             else
  51.                             {
  52.                                 tempList.Clear();
  53.                                 tempList.Add(matrix[j1, j2]);
  54.                             }
  55.                         }
  56.                     }
  57.                     else
  58.                     {
  59.                         if (tempList.Count > final.Count)
  60.                         {
  61.                             final.Clear();
  62.                             foreach (var item in tempList)
  63.                             {
  64.                                 final.Add(item);
  65.                             }
  66.                             tempList.Clear();
  67.                             tempList.Add(matrix[j1, j2]);
  68.                         }
  69.                         else
  70.                         {
  71.                             tempList.Clear();
  72.                             tempList.Add(matrix[j1, j2]);
  73.                         }
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.  
  79.         private static void MatrixVertical(string[,] matrix)
  80.         {
  81.             for (int i = 0; i < matrix.GetLength(1); i++)
  82.             {
  83.                 List<string> tempList = new List<string>();
  84.                 tempList.Add(matrix[0, i]);
  85.                 for (int j = 1; j < matrix.GetLength(0); j++)
  86.                 {
  87.                     if (matrix[j, i] == matrix[j - 1, i])
  88.                     {
  89.                         tempList.Add(matrix[j, i]);
  90.                         if (j == matrix.GetLength(0) - 1)
  91.                         {
  92.                             if (tempList.Count > final.Count)
  93.                             {
  94.                                 final.Clear();
  95.                                 foreach (var item in tempList)
  96.                                 {
  97.                                     final.Add(item);
  98.                                 }
  99.                                 tempList.Clear();
  100.                                 tempList.Add(matrix[j, i]);
  101.                             }
  102.                             else
  103.                             {
  104.                                 tempList.Clear();
  105.                                 tempList.Add(matrix[j, i]);
  106.                             }
  107.                         }
  108.                     }
  109.                     else
  110.                     {
  111.                         if (tempList.Count > final.Count)
  112.                         {
  113.                             final.Clear();
  114.                             foreach (var item in tempList)
  115.                             {
  116.                                 final.Add(item);
  117.                             }
  118.                             tempList.Clear();
  119.                             tempList.Add(matrix[j, i]);
  120.                         }
  121.                         else
  122.                         {
  123.                             tempList.Clear();
  124.                             tempList.Add(matrix[j, i]);
  125.                         }
  126.                     }
  127.                 }
  128.             }
  129.         }
  130.  
  131.         private static void MatrixHorizontal(string[,] matrix)
  132.         {
  133.             for (int i = 0; i < matrix.GetLength(0); i++)
  134.             {
  135.                 List<string> tempList = new List<string>();
  136.                 tempList.Add(matrix[i, 0]);
  137.                 for (int j = 1; j < matrix.GetLength(1); j++)
  138.                 {
  139.                     if (matrix[i, j] == matrix[i, j - 1])
  140.                     {
  141.                         tempList.Add(matrix[i, j]);
  142.                         if (j == matrix.GetLength(1) - 1)
  143.                         {
  144.                             if (tempList.Count > final.Count)
  145.                             {
  146.                                 final.Clear();
  147.                                 foreach (var item in tempList)
  148.                                 {
  149.                                     final.Add(item);
  150.                                 }
  151.                                 tempList.Clear();
  152.                                 tempList.Add(matrix[i, j]);
  153.                             }
  154.                             else
  155.                             {
  156.                                 tempList.Clear();
  157.                                 tempList.Add(matrix[i, j]);
  158.                             }
  159.                         }
  160.                     }
  161.                     else
  162.                     {
  163.                         if (tempList.Count > final.Count)
  164.                         {
  165.                             final.Clear();
  166.                             foreach (var item in tempList)
  167.                             {
  168.                                 final.Add(item);
  169.                             }
  170.                             tempList.Clear();
  171.                             tempList.Add(matrix[i, j]);
  172.                         }
  173.                         else
  174.                         {
  175.                             tempList.Clear();
  176.                             tempList.Add(matrix[i, j]);
  177.                         }
  178.                     }
  179.                 }
  180.             }
  181.         }
  182.  
  183.         private static void FillMatrix(string[,] matrix)
  184.         {
  185.             for (int i = 0; i < matrix.GetLength(0); i++)
  186.             {
  187.                 for (int j = 0; j < matrix.GetLength(1); j++)
  188.                 {
  189.                     matrix[i, j] = Console.ReadLine();
  190.                 }
  191.             }
  192.         }  
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement