nassss

Untitled

Mar 12th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. /* Write a program that finds the largest possible sequence
  6. of equal neighboring elements in a rectangular matrix and prints its size.*/
  7.  
  8. namespace _10._LargestSurface
  9. {
  10. class Program
  11. {
  12. static HashSet<Tuple<int, int>> isPassed = new HashSet<Tuple<int, int>>();
  13. static Tuple<int, int> rowColTuple = new Tuple<int, int>(0, 0);
  14. static int counter = 0;
  15. static int maxCounter = 0;
  16. static void Main(string[] args)
  17. {
  18. int output = LoadingTheMatrix();
  19. Console.WriteLine(output);
  20. }
  21. static int LoadingTheMatrix()
  22. {
  23. var parameters = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  24. var row = parameters[0];
  25. var col = parameters[1];
  26. int[,] matrix = new int[row, col];
  27. for (int i = 0; i < row; i++) // to be added in Matrix loading method
  28. {
  29. var input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  30.  
  31. for (int j = 0; j < col; j++)
  32. {
  33. matrix[i, j] = input[j];
  34. rowColTuple = new Tuple<int, int>(i, j);
  35. isPassed.Add(rowColTuple);
  36. }
  37. }
  38. for (int i = row - 1; i >= 0; i--)
  39. {
  40. for (int j = col - 1; j >= 0; j--)
  41. {
  42. rowColTuple = new Tuple<int, int>(i, j);
  43. if (isPassed.Contains(rowColTuple))
  44. {
  45. FindLargestSurface(i, j, matrix, rowColTuple, matrix[i, j]);
  46. if (counter > maxCounter)
  47. {
  48. maxCounter = counter;
  49. }
  50. counter = 0;
  51. }
  52. }
  53. }
  54. return maxCounter;
  55. }
  56. static void FindLargestSurface(int row, int col, int[,] matrix, Tuple<int, int> rowColTuple, int currValue)
  57. {
  58. if ((row < 0) || (row >= matrix.GetLength(0)) ||
  59. (col < 0) || (col >= matrix.GetLength(1)))
  60. {
  61. return;
  62. }
  63. if (!isPassed.Contains(rowColTuple))
  64. {
  65. return;
  66. }
  67. if (currValue != matrix[row, col])
  68. {
  69. return;
  70. }
  71. isPassed.Remove(rowColTuple);
  72. counter++;
  73.  
  74. FindLargestSurface(row, col - 1, matrix, new Tuple<int, int>(row, col - 1), currValue); //left
  75. FindLargestSurface(row - 1, col, matrix, new Tuple<int, int>(row - 1, col), currValue); //up
  76. FindLargestSurface(row, col + 1, matrix, new Tuple<int, int>(row, col + 1), currValue); //rigth
  77. FindLargestSurface(row + 1, col, matrix, new Tuple<int, int>(row + 1, col), currValue); //down
  78. }
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment