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 List<Tuple<int, int>> isPassed = new List<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)
- {
- var parameters = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
- var row = parameters[0];
- var col = parameters[1];
- //var rowColTuple = new Tuple<int, int>(0, 0);
- 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;
- }
- }
- }
- Console.WriteLine(maxCounter);
- }
- //static int[] MatrixLoading()
- //{
- //TODO: implement matrix fulling here
- //}
- 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, rowColTuple, currValue); //left
- FindLargestSurface(row - 1, col, matrix, rowColTuple, currValue); //up
- FindLargestSurface(row, col + 1, matrix, rowColTuple, currValue); //rigth
- FindLargestSurface(row + 1, col, matrix, rowColTuple, currValue); //down
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment