Advertisement
Guest User

04-LargestAreaInMatrix

a guest
Jan 17th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _04_LargestAreaInMatrix
  6. {
  7. class MessagesInBottle
  8. {
  9. static int currentArea;
  10. static int largestArea;
  11. static int[] rows = new int[] { 1, -1, 0, 0 };
  12. static int[] cols = new int[] { 0, 0, 1, -1 };
  13.  
  14. static void Main(string[] args)
  15. {
  16. var rowsCols = Console.ReadLine().Split().Select(int.Parse).ToArray();
  17. var numberOfRows = rowsCols[0];
  18. var numberOfCols = rowsCols[1];
  19.  
  20. var matrix = new int[numberOfRows, numberOfCols];
  21. var visited = new bool[numberOfRows, numberOfCols];
  22.  
  23. for (int row = 0; row < numberOfRows; row++)
  24. {
  25. var currentRow = Console.ReadLine().Split().Select(int.Parse).ToArray();
  26.  
  27. for (int col = 0; col < numberOfCols; col++)
  28. {
  29. matrix[row, col] = currentRow[col];
  30. }
  31. }
  32.  
  33. for (int row = 0; row < numberOfRows; row++)
  34. {
  35. for (int col = 0; col < numberOfCols; col++)
  36. {
  37. if (!visited[row, col])
  38. {
  39. currentArea = 0;
  40. Dfs(row, col, matrix, visited, matrix[row, col]);
  41. // Bfs(row, col, matrix, visited, matrix[row, col]);
  42. }
  43. }
  44. }
  45.  
  46. // largestArea = Math.Max(largestArea, currentArea);
  47. Console.WriteLine(largestArea);
  48. }
  49.  
  50. //private static void Bfs(int row, int col, int[,] matrix, bool[,] visited, int currentElement)
  51. //{
  52. // visited[row, col] = true;
  53. // var queue = new Queue<int>();
  54. // queue.Enqueue(currentElement);
  55.  
  56. // while (queue.Count > 0)
  57. // {
  58. // var element = queue.Dequeue();
  59. // currentArea++;
  60.  
  61. // for (int i = 0; i < rows.Length; i++)
  62. // {
  63. // var neighbourRow = row + rows[i];
  64. // var neighbourCol = col + cols[i];
  65.  
  66. // if ((neighbourRow >= 0 && neighbourCol >= 0 && neighbourRow < matrix.GetLength(0) && neighbourCol < matrix.GetLength(1))
  67. // && currentElement == matrix[neighbourRow, neighbourCol])
  68. // {
  69. // if (!visited[neighbourRow, neighbourCol])
  70. // {
  71. // queue.Enqueue(matrix[neighbourRow, neighbourCol]);
  72. // visited[neighbourRow, neighbourCol] = true;
  73. // }
  74. // }
  75. // }
  76. // }
  77. //}
  78.  
  79. private static void Dfs(int row, int col, int[,] matrix, bool[,] visited, int currentValue)
  80. {
  81. if (row < 0 || col < 0 || row > matrix.GetLength(0) - 1 || col > matrix.GetLength(1) - 1)
  82. {
  83. return;
  84. }
  85. if (matrix[row, col] != currentValue)
  86. {
  87. return;
  88. }
  89. if (visited[row, col])
  90. {
  91. return;
  92. }
  93.  
  94. currentArea++;
  95. visited[row, col] = true;
  96. largestArea = largestArea < currentArea ? currentArea : largestArea;
  97.  
  98. for (int move = 0; move < rows.Length; move++)
  99. {
  100. Dfs(row + rows[move], col + cols[move], matrix, visited, matrix[row, col]);
  101. }
  102. }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement