Advertisement
nina75

Multidimensional Arrays/Task7

Dec 30th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.05 KB | None | 0 0
  1. //* Write a program that finds the largest area of equal neighbor elements in a rectangular matrix and prints its size. Example:
  2. //  1  3  2  2  2  4
  3. //  3  3  3  2  4  4
  4. //  4  3  1  2  3  3   --> 13
  5. //  4  3  1  3  3  1
  6. //  4  3  3  3  1  1
  7. // Hint: you can use the algorithm "Depth-first search" or "Breadth-first search" (find them in Wikipedia).
  8.  
  9. using System;
  10. class LargestArea
  11. {
  12.     static bool[,] isVisited;
  13.     static int count;
  14.     static void Main()
  15.     {
  16.         //Input
  17.         int[,] testMatrix = {
  18.                             { 1, 3, 2, 2, 2, 4 },
  19.                             { 3, 3, 3, 2, 4, 4 },
  20.                             { 4, 3, 1, 2, 3, 3 },
  21.                             { 4, 3, 1, 3, 3, 1 },
  22.                             { 4, 3, 3, 3, 1, 1 }
  23.                         };
  24.  
  25.         //Declare a bool matrix, where if the position is checked the cell becomes true
  26.         isVisited = new bool[testMatrix.GetLength(0), testMatrix.GetLength(1)];
  27.         for (int row = 0; row < isVisited.GetLength(0); row++)
  28.         {
  29.             for (int col = 0; col < isVisited.GetLength(1); col++)
  30.             {
  31.                 isVisited[row, col] = false;
  32.             }
  33.         }
  34.        
  35.         //Call SearchEquals method for each element of the matrix
  36.         int bestCount = 1;
  37.         int mostFrequent = testMatrix[0, 0];
  38.         for (int row = 0; row < testMatrix.GetLength(0); row++)
  39.         {
  40.             for (int col = 0; col < testMatrix.GetLength(1); col++)
  41.             {
  42.                 count = 0;
  43.                 SearchEquals(testMatrix, row, col);
  44.                 if (count > bestCount)
  45.                 {
  46.                     bestCount = count;
  47.                     mostFrequent = testMatrix[row, col];
  48.                 }
  49.             }
  50.         }
  51.      
  52.         //Print the result
  53.         Console.WriteLine("{0} --> {1} times ", mostFrequent, bestCount);
  54.  
  55.     }
  56.     //Check is cell in the array
  57.     static bool IsCellInMatrix(int[,] matrix, int i, int j)
  58.     {
  59.         return i >= 0 && i < matrix.GetLength(0) && j >= 0 && j < matrix.GetLength(1);
  60.     }
  61.  
  62.     //Searching equals elements in position(row, col)
  63.     static void SearchEquals(int[,] matrix, int row, int col)
  64.     {
  65.         int value = matrix[row, col];
  66.         isVisited[row, col] = true;
  67.         count++;
  68.  
  69.         if (IsCellInMatrix(matrix, row, col + 1) && matrix[row, col + 1] == value && isVisited[row, col + 1] == false)
  70.         {
  71.             SearchEquals(matrix, row, col + 1);
  72.         }
  73.  
  74.         if (IsCellInMatrix(matrix, row, col - 1) && matrix[row, col - 1] == value && isVisited[row, col - 1] == false)
  75.         {
  76.             SearchEquals(matrix, row, col - 1);
  77.         }
  78.  
  79.         if (IsCellInMatrix(matrix, row - 1, col) && matrix[row - 1, col] == value && isVisited[row - 1, col] == false)
  80.         {
  81.             SearchEquals(matrix, row - 1, col);
  82.         }
  83.  
  84.         if (IsCellInMatrix(matrix, row + 1, col) && matrix[row + 1, col] == value && isVisited[row + 1, col] == false)
  85.         {
  86.             SearchEquals(matrix, row +1 , col);
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement