Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class LargestAreaOfEqualNeighborElements
- {
- //Keeps the current element and its count
- static int[] curr = new int[2];
- //Marks steps made by the code TRUE-been here, False-not been here
- static bool[,] map;
- static int maxCount = 0;
- static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- int m = int.Parse(Console.ReadLine());
- int[,] matrix = new int[n, m];
- map = new bool[n, m];
- for (int row = 0; row < n; row++)
- {
- for (int col = 0; col < m; col++)
- {
- matrix[row, col] = int.Parse(Console.ReadLine());
- }
- }
- for (int row = 0; row < n; row++)
- {
- for (int col = 0; col < m; col++)
- {
- int[] cordinates = new int[2] { row, col };
- findingEqualNeighors(matrix, cordinates);
- if (maxCount < curr[1])
- {
- maxCount = curr[1];
- }
- }
- }
- Console.WriteLine(maxCount);
- }
- static void findingEqualNeighors(int[,] matrix, int[] cordinates)
- {
- int currElement = matrix[cordinates[0], cordinates[1]];
- //curr[0] is the current element
- curr[0] = currElement;
- //curr[1] is the counter for the current element
- curr[1] = 1;
- pathfinding(matrix, cordinates);
- }
- static void pathfinding(int[,] matrix, int[] cordinates)
- {
- int row = cordinates[0];
- int col = cordinates[1];
- map[row, col] = true;
- //Check if step up is possible
- if (row - 1 >= 0)
- {
- if (matrix[row - 1, col] == matrix[row, col] && map[row - 1, col] == false)
- {
- curr[1]++;
- cordinates[0]--;
- pathfinding(matrix, cordinates);
- cordinates[0]++;
- }
- }
- //Check if step down is possible
- if (row + 1 < matrix.GetLength(0))
- {
- if (matrix[row + 1, col] == matrix[row, col] && map[row + 1, col] == false)
- {
- curr[1]++;
- cordinates[0]++;
- pathfinding(matrix, cordinates);
- cordinates[0]--;
- }
- }
- //Check if step left is possible
- if (col - 1 >= 0)
- {
- if (matrix[row, col - 1] == matrix[row, col] && map[row, col - 1] == false)
- {
- curr[1]++;
- cordinates[1]--;
- pathfinding(matrix, cordinates);
- cordinates[1]++;
- }
- }
- //Check if step right is possible
- if (col + 1 < matrix.GetLength(1))
- {
- if (matrix[row, col + 1] == matrix[row, col] && map[row, col + 1] == false)
- {
- curr[1]++;
- cordinates[1]++;
- pathfinding(matrix, cordinates);
- cordinates[1]--;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement