Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace Largest_Area_in_Matrix
- {
- class Program
- {
- static int[,] matrix;
- static bool[,] visited;
- static void Main()
- {
- var sizes = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- matrix = new int[sizes[0], sizes[1]];
- visited = new bool[sizes[0], sizes[1]];
- for (int row = 0; row < sizes[0]; row++)
- {
- var input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- for (int col = 0; col < sizes[1]; col++)
- {
- matrix[row, col] = input[col];
- }
- }
- Console.WriteLine(FindMaxArea());
- }
- static int FindMaxArea()
- {
- int maxArea = 0;
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- maxArea = Math.Max(maxArea, FindMaxArea(row, col, matrix[row, col]));
- }
- }
- return maxArea;
- }
- static int FindMaxArea(int row, int col, int value)
- {
- if (row < 0 || col < 0 || row >= matrix.GetLength(0) || col >= matrix.GetLength(1))
- {
- return 0;
- }
- if (visited[row, col])
- {
- return 0;
- }
- if (matrix[row, col] != value)
- {
- return 0;
- }
- visited[row, col] = true;
- return FindMaxArea(row, col + 1, value) + FindMaxArea(row, col - 1, value) +
- FindMaxArea(row + 1, col, value) + FindMaxArea(row - 1, col, value) + 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement