Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- /** Write a program that finds the largest area of equal neighbor elements in a rectangular matrix and prints its size. */
- class TheLargestArea
- {
- static int colCount;
- static int rolCount;
- static int lonestSequenceCounter = 0;
- static int lonestSequenceNum = 0;
- static int[,] array;
- static bool[,] arrayCheck;
- static int count = 1;
- // Get max sequence of element
- static void GetLongestSequence(int counter, int rol, int col)
- {
- if (counter > lonestSequenceCounter)
- {
- lonestSequenceCounter = counter;
- lonestSequenceNum = array[rol, col]; // if we want to see who is the element
- }
- }
- // move to right side in sequence
- static void MoveRight(int rol, int col)
- {
- if (col >= colCount || arrayCheck[rol, col]) return;
- if (array[rol, col] == array[rol, col - 1])
- {
- ++count;
- arrayCheck[rol, col] = true; // make an element of the matrix inaccessible
- MoveInMatricBasic(rol, col);
- GetLongestSequence(count, rol, col);
- }
- else
- {
- return;
- }
- }
- // move to left side in sequence
- static void MoveLeft(int rol, int col)
- {
- if (col < 0 || arrayCheck[rol, col]) return;
- if (array[rol, col] == array[rol, col + 1])
- {
- ++count;
- arrayCheck[rol, col] = true; //make an element of the matrix inaccessible
- MoveInMatricBasic(rol, col);
- GetLongestSequence(count, rol, col);
- }
- else
- {
- return;
- }
- }
- // move to down side in sequence
- static void MoveDown(int rol, int col)
- {
- if (rol >= rolCount || arrayCheck[rol, col]) return;
- if (array[rol, col] == array[rol - 1, col])
- {
- ++count;
- arrayCheck[rol, col] = true; //make an element of the matrix inaccessible
- MoveInMatricBasic(rol, col);
- GetLongestSequence(count, rol, col);
- }
- else
- {
- return;
- }
- }
- // move to up side in sequence
- static void MoveUp(int rol, int col)
- {
- if (rol < 0 || arrayCheck[rol, col]) return;
- if (array[rol, col] == array[rol + 1, col])
- {
- ++count;
- arrayCheck[rol, col] = true; //make an element of the matrix inaccessible
- MoveInMatricBasic(rol, col);
- GetLongestSequence(count, rol, col);
- }
- else
- {
- return;
- }
- }
- //check the four corners of the element
- static void MoveInMatricBasic(int rol,int col)
- {
- MoveUp(rol - 1, col);
- MoveRight(rol, col + 1);
- MoveLeft(rol, col - 1);
- MoveDown(rol + 1, col);
- return;
- }
- static void Main()
- {
- // Input data in array
- Console.WriteLine("Enter rols ");
- rolCount = int.Parse(Console.ReadLine());
- Console.WriteLine("Enter cols");
- colCount = int.Parse(Console.ReadLine());
- Console.WriteLine("Enter array with {0} rols and {1} cols", rolCount, colCount);
- array = new int[rolCount, colCount];
- arrayCheck = new bool[rolCount, colCount];
- // Input value in array
- for (int rol = 0; rol < rolCount; rol++)
- {
- string element = Console.ReadLine();
- string[] dontUse = new string[] { " " };
- string[] elementArray = element.Split(dontUse, StringSplitOptions.RemoveEmptyEntries);
- for (int col = 0; col < elementArray.Length; col++)
- {
- array[rol, col] = int.Parse(elementArray[col]);
- arrayCheck[rol, col] = false;
- }
- }
- // Solution
- for (int rol = 0; rol < rolCount; rol++)
- {
- for (int col = 0; col < colCount; col++)
- {
- arrayCheck[rol, col] = true; //make an element of the matrix inaccessible
- MoveInMatricBasic(rol, col);
- count = 1;
- }
- }
- Console.WriteLine(lonestSequenceCounter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement