Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- /* Write a program that finds the largest possible sequence
- of equal neighboring elements in a rectangular matrix and prints its size.*/
- namespace _10._LargestSurface
- {
- class Program
- {
- static HashSet<Tuple<int, int>> isPassed = new HashSet<Tuple<int, int>>();
- static Tuple<int, int> rowColTuple = new Tuple<int, int>(0, 0);
- static int counter = 0;
- static int maxCounter = 0;
- static void Main(string[] args)
- {
- int output = LoadingTheMatrix();
- Console.WriteLine(output);
- }
- static int LoadingTheMatrix()
- {
- var parameters = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- var row = parameters[0];
- var col = parameters[1];
- int[,] matrix = new int[row, col];
- for (int i = 0; i < row; i++) // to be added in Matrix loading method
- {
- var input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- for (int j = 0; j < col; j++)
- {
- matrix[i, j] = input[j];
- rowColTuple = new Tuple<int, int>(i, j);
- isPassed.Add(rowColTuple);
- }
- }
- for (int i = row - 1; i >= 0; i--)
- {
- for (int j = col - 1; j >= 0; j--)
- {
- rowColTuple = new Tuple<int, int>(i, j);
- if (isPassed.Contains(rowColTuple))
- {
- FindLargestSurface(i, j, matrix, rowColTuple, matrix[i, j]);
- if (counter > maxCounter)
- {
- maxCounter = counter;
- }
- counter = 0;
- }
- }
- }
- return maxCounter;
- }
- static void FindLargestSurface(int row, int col, int[,] matrix, Tuple<int, int> rowColTuple, int currValue)
- {
- if ((row < 0) || (row >= matrix.GetLength(0)) ||
- (col < 0) || (col >= matrix.GetLength(1)))
- {
- return;
- }
- if (!isPassed.Contains(rowColTuple))
- {
- return;
- }
- if (currValue != matrix[row, col])
- {
- return;
- }
- isPassed.Remove(rowColTuple);
- counter++;
- FindLargestSurface(row, col - 1, matrix, new Tuple<int, int>(row, col - 1), currValue); //left
- FindLargestSurface(row - 1, col, matrix, new Tuple<int, int>(row - 1, col), currValue); //up
- FindLargestSurface(row, col + 1, matrix, new Tuple<int, int>(row, col + 1), currValue); //rigth
- FindLargestSurface(row + 1, col, matrix, new Tuple<int, int>(row + 1, col), currValue); //down
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment