Advertisement
stoianpp

Task 7 multiarrays

Dec 19th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. //* Write a program that finds the largest area of equal neighbor elements
  8. //in a rectangular matrix and prints its size. Example:
  9.  
  10. class LargestMatrizArea
  11. {
  12.     static void Main()
  13.     {
  14.         int[,] matrix = {
  15.         {1,3,3,4,2,2,3,3,5},
  16.         {3,2,2,3,4,2,3,5,5},
  17.         {3,3,3,4,2,2,2,2,3},
  18.         {2,3,2,3,3,1,1,5,5},
  19.         {3,2,1,1,1,1,1,3,3}
  20.         };
  21.         //int midResult = 0;
  22.         int maxiSize = 0;
  23.         for (int row = 0; row < matrix.GetLength(0); row++)
  24.         {
  25.             for (int col = 0; col < matrix.GetLength(1); col++)
  26.             {
  27.                 int midResult = 0;
  28.                 if (matrix[row,col] != -1)
  29.                 {
  30.                     midResult = CheckCellResult(row,col,matrix);
  31.                 }
  32.                 if (midResult > maxiSize) maxiSize = midResult;
  33.             }
  34.         }
  35.         Console.WriteLine(maxiSize);
  36.     }
  37.  
  38.     static int CheckCellResult(int row, int col, int[,] matrix)
  39.     {
  40.         bool isAvailable = true;
  41.         int midSize = 1;
  42.         int simbol = matrix[row,col];
  43.         matrix[row, col] = -1;
  44.         while(isAvailable)
  45.         {
  46.             isAvailable = false;
  47.             if (row > 0 && matrix[row - 1, col] == simbol)
  48.             {
  49.                 midSize += CheckCellResult(row - 1, col, matrix);
  50.                 isAvailable = true;
  51.             }
  52.             else if (row > 0 && col> 0 && matrix[row - 1, col-1] == simbol)
  53.             {
  54.                 midSize += CheckCellResult(row - 1, col-1, matrix);
  55.                 isAvailable = true;
  56.             }
  57.             else if (col > 0 && matrix[row, col - 1] == simbol)
  58.             {
  59.                 midSize += CheckCellResult(row, col - 1, matrix);
  60.                 isAvailable = true;
  61.             }
  62.             else if (col > 0 && row < matrix.GetLength(0)-1 && matrix[row+1, col - 1] == simbol)
  63.             {
  64.                 midSize += CheckCellResult(row+1, col - 1, matrix);
  65.                 isAvailable = true;
  66.             }
  67.             else if (row < matrix.GetLength(0) - 1 && matrix[row + 1, col] == simbol)
  68.             {
  69.                 midSize += CheckCellResult(row + 1, col, matrix);
  70.                 isAvailable = true;
  71.             }
  72.             else if (col < matrix.GetLength(1)-1 && row > 0 && matrix[row - 1, col + 1] == simbol)
  73.             {
  74.                 midSize += CheckCellResult(row - 1, col + 1, matrix);
  75.                 isAvailable = true;
  76.             }
  77.             else if (col < matrix.GetLength(1) - 1 && row < matrix.GetLength(0) - 1 && matrix[row + 1, col + 1] == simbol)
  78.             {
  79.                 midSize += CheckCellResult(row + 1, col + 1, matrix);
  80.                 isAvailable = true;
  81.             }
  82.             else if (col < matrix.GetLength(1) - 1 && matrix[row, col + 1] == simbol)
  83.             {
  84.                 midSize += CheckCellResult(row, col + 1, matrix);
  85.                 isAvailable = true;
  86.             }
  87.         }
  88.         return midSize;
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement