Advertisement
soxa

TheLargestArea

Dec 26th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | None | 0 0
  1. using System;
  2. /** Write a program that finds the largest area of equal neighbor elements in a rectangular matrix and prints its size. */
  3.  
  4. class TheLargestArea
  5. {
  6.     static int colCount;
  7.     static int rolCount;
  8.     static int lonestSequenceCounter = 0;
  9.     static int lonestSequenceNum = 0;
  10.     static int[,] array;
  11.     static bool[,] arrayCheck;
  12.     static int count = 1;
  13.     // Get max sequence of element
  14.     static void GetLongestSequence(int counter, int rol, int col)
  15.     {
  16.         if (counter > lonestSequenceCounter)
  17.         {
  18.             lonestSequenceCounter = counter;
  19.             lonestSequenceNum = array[rol, col]; // if we want to see who is the element
  20.         }
  21.     }
  22.     // move to right side in sequence
  23.     static void MoveRight(int rol, int col)
  24.     {
  25.         if (col >= colCount || arrayCheck[rol, col]) return;
  26.         if (array[rol, col] == array[rol, col - 1])
  27.         {
  28.             ++count;
  29.             arrayCheck[rol, col] = true; // make an element of the matrix inaccessible
  30.             MoveInMatricBasic(rol, col);
  31.             GetLongestSequence(count, rol, col);
  32.         }
  33.         else
  34.         {
  35.             return;
  36.         }
  37.     }
  38.     // move to left side in sequence
  39.     static void MoveLeft(int rol, int col)
  40.     {
  41.         if (col < 0 || arrayCheck[rol, col]) return;
  42.         if (array[rol, col] == array[rol, col + 1])
  43.         {
  44.             ++count;
  45.             arrayCheck[rol, col] = true; //make an element of the matrix inaccessible
  46.             MoveInMatricBasic(rol, col);
  47.             GetLongestSequence(count, rol, col);
  48.         }
  49.         else
  50.         {
  51.             return;
  52.         }
  53.     }
  54.     // move to down side in sequence
  55.     static void MoveDown(int rol, int col)
  56.     {
  57.         if (rol >= rolCount || arrayCheck[rol, col]) return;
  58.  
  59.         if (array[rol, col] == array[rol - 1, col])
  60.         {
  61.             ++count;
  62.             arrayCheck[rol, col] = true; //make an element of the matrix inaccessible
  63.             MoveInMatricBasic(rol, col);
  64.             GetLongestSequence(count, rol, col);
  65.         }
  66.         else
  67.         {
  68.             return;
  69.         }
  70.     }
  71.     // move to up side in sequence
  72.     static void MoveUp(int rol, int col)
  73.     {
  74.         if (rol < 0 || arrayCheck[rol, col]) return;
  75.  
  76.         if (array[rol, col] == array[rol + 1, col])
  77.         {
  78.             ++count;
  79.             arrayCheck[rol, col] = true; //make an element of the matrix inaccessible
  80.             MoveInMatricBasic(rol, col);
  81.             GetLongestSequence(count, rol, col);
  82.         }
  83.         else
  84.         {
  85.             return;
  86.         }
  87.     }
  88.     //check the four corners of the element
  89.     static void MoveInMatricBasic(int rol,int col)
  90.     {
  91.         MoveUp(rol - 1, col);
  92.         MoveRight(rol, col + 1);
  93.         MoveLeft(rol, col - 1);
  94.         MoveDown(rol + 1, col);
  95.         return;
  96.     }
  97.  
  98.     static void Main()
  99.     {
  100.         // Input data in array
  101.         Console.WriteLine("Enter rols ");
  102.         rolCount = int.Parse(Console.ReadLine());
  103.         Console.WriteLine("Enter cols");
  104.         colCount = int.Parse(Console.ReadLine());
  105.         Console.WriteLine("Enter array with {0} rols and {1} cols", rolCount, colCount);
  106.         array = new int[rolCount, colCount];
  107.         arrayCheck = new bool[rolCount, colCount];
  108.         // Input value in array
  109.         for (int rol = 0; rol < rolCount; rol++)
  110.         {
  111.             string element = Console.ReadLine();
  112.             string[] dontUse = new string[] { " " };
  113.             string[] elementArray = element.Split(dontUse, StringSplitOptions.RemoveEmptyEntries);
  114.  
  115.             for (int col = 0; col < elementArray.Length; col++)
  116.             {
  117.                 array[rol, col] = int.Parse(elementArray[col]);
  118.                 arrayCheck[rol, col] = false;
  119.             }
  120.         }
  121.  
  122.         // Solution
  123.         for (int rol = 0; rol < rolCount; rol++)
  124.         {
  125.             for (int col = 0; col < colCount; col++)
  126.             {
  127.                 arrayCheck[rol, col] = true; //make an element of the matrix inaccessible
  128.                 MoveInMatricBasic(rol, col);
  129.                 count = 1;
  130.             }
  131.         }
  132.          Console.WriteLine(lonestSequenceCounter);
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement