nassss

Untitled

Mar 12th, 2020
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 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 List<Tuple<int, int>> isPassed = new List<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. var parameters = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  19. var row = parameters[0];
  20. var col = parameters[1];
  21. //var rowColTuple = new Tuple<int, int>(0, 0);
  22. int[,] matrix = new int[row, col];
  23. for (int i = 0; i < row; i++) // to be added in Matrix loading method
  24. {
  25. var input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  26.  
  27. for (int j = 0; j < col; j++)
  28. {
  29. matrix[i, j] = input[j];
  30. rowColTuple = new Tuple<int, int>(i, j);
  31. isPassed.Add(rowColTuple);
  32. }
  33. }
  34.  
  35. for (int i = row - 1; i >= 0; i--)
  36. {
  37. for (int j = col - 1; j >= 0; j--)
  38. {
  39. rowColTuple = new Tuple<int, int>(i, j);
  40. if (isPassed.Contains(rowColTuple))
  41. {
  42. FindLargestSurface(i, j, matrix,rowColTuple, matrix[i, j]);
  43. if (counter > maxCounter)
  44. {
  45. maxCounter = counter;
  46. }
  47. counter = 0;
  48. }
  49. }
  50. }
  51. Console.WriteLine(maxCounter);
  52. }
  53. //static int[] MatrixLoading()
  54. //{
  55. //TODO: implement matrix fulling here
  56. //}
  57. static void FindLargestSurface(int row, int col, int[,] matrix, Tuple<int, int> rowColTuple, int currValue)
  58. {
  59. if ((row < 0) || (row >= matrix.GetLength(0)) ||
  60. (col < 0) || (col >= matrix.GetLength(1)))
  61. {
  62. return;
  63. }
  64. if (!isPassed.Contains(rowColTuple))
  65. {
  66. return;
  67. }
  68. if (currValue != matrix[row, col])
  69. {
  70. return;
  71. }
  72. isPassed.Remove(rowColTuple);
  73. counter++;
  74.  
  75. FindLargestSurface(row, col - 1, matrix, rowColTuple, currValue); //left
  76. FindLargestSurface(row - 1, col, matrix, rowColTuple, currValue); //up
  77. FindLargestSurface(row, col + 1, matrix, rowColTuple, currValue); //rigth
  78. FindLargestSurface(row + 1, col, matrix, rowColTuple, currValue); //down
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment