nassss

Untitled

Mar 12th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 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. if (row < 3 || col > 1024)
  27. {
  28. throw new ArgumentOutOfRangeException("Rows and cols should be between following ranges >=3 && <= 1024!");
  29. }
  30. int[,] matrix = new int[row, col];
  31. for (int i = 0; i < row; i++) // to be added in Matrix loading method
  32. {
  33. var input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  34.  
  35. for (int j = 0; j < col; j++)
  36. {
  37. matrix[i, j] = input[j];
  38. rowColTuple = new Tuple<int, int>(i, j);
  39. isPassed.Add(rowColTuple);
  40. }
  41. }
  42. for (int i = row - 1; i >= 0; i--)
  43. {
  44. for (int j = col - 1; j >= 0; j--)
  45. {
  46. rowColTuple = new Tuple<int, int>(i, j);
  47. if (isPassed.Contains(rowColTuple))
  48. {
  49. FindLargestSurface(i, j, matrix, rowColTuple, matrix[i, j]);
  50. if (counter > maxCounter)
  51. {
  52. maxCounter = counter;
  53. }
  54. counter = 0;
  55. }
  56. }
  57. }
  58. return maxCounter;
  59. }
  60. static void FindLargestSurface(int row, int col, int[,] matrix, Tuple<int, int> rowColTuple, int currValue)
  61. {
  62. if ((row < 0) || (row >= matrix.GetLength(0)) ||
  63. (col < 0) || (col >= matrix.GetLength(1)))
  64. {
  65. return;
  66. }
  67. if (!isPassed.Contains(rowColTuple))
  68. {
  69. return;
  70. }
  71. if (currValue != matrix[row, col])
  72. {
  73. return;
  74. }
  75. isPassed.Remove(rowColTuple);
  76. counter++;
  77.  
  78. FindLargestSurface(row, col - 1, matrix, new Tuple<int, int>(row, col - 1), currValue); //left
  79. FindLargestSurface(row - 1, col, matrix, new Tuple<int, int>(row - 1, col), currValue); //up
  80. FindLargestSurface(row, col + 1, matrix, new Tuple<int, int>(row, col + 1), currValue); //rigth
  81. FindLargestSurface(row + 1, col, matrix, new Tuple<int, int>(row + 1, col), currValue); //down
  82. }
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment