Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- //* Write a program that finds the largest area of equal neighbor elements
- //in a rectangular matrix and prints its size. Example:
- class LargestMatrizArea
- {
- static void Main()
- {
- int[,] matrix = {
- {1,3,3,4,2,2,3,3,5},
- {3,2,2,3,4,2,3,5,5},
- {3,3,3,4,2,2,2,2,3},
- {2,3,2,3,3,1,1,5,5},
- {3,2,1,1,1,1,1,3,3}
- };
- //int midResult = 0;
- int maxiSize = 0;
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- int midResult = 0;
- if (matrix[row,col] != -1)
- {
- midResult = CheckCellResult(row,col,matrix);
- }
- if (midResult > maxiSize) maxiSize = midResult;
- }
- }
- Console.WriteLine(maxiSize);
- }
- static int CheckCellResult(int row, int col, int[,] matrix)
- {
- bool isAvailable = true;
- int midSize = 1;
- int simbol = matrix[row,col];
- matrix[row, col] = -1;
- while(isAvailable)
- {
- isAvailable = false;
- if (row > 0 && matrix[row - 1, col] == simbol)
- {
- midSize += CheckCellResult(row - 1, col, matrix);
- isAvailable = true;
- }
- else if (row > 0 && col> 0 && matrix[row - 1, col-1] == simbol)
- {
- midSize += CheckCellResult(row - 1, col-1, matrix);
- isAvailable = true;
- }
- else if (col > 0 && matrix[row, col - 1] == simbol)
- {
- midSize += CheckCellResult(row, col - 1, matrix);
- isAvailable = true;
- }
- else if (col > 0 && row < matrix.GetLength(0)-1 && matrix[row+1, col - 1] == simbol)
- {
- midSize += CheckCellResult(row+1, col - 1, matrix);
- isAvailable = true;
- }
- else if (row < matrix.GetLength(0) - 1 && matrix[row + 1, col] == simbol)
- {
- midSize += CheckCellResult(row + 1, col, matrix);
- isAvailable = true;
- }
- else if (col < matrix.GetLength(1)-1 && row > 0 && matrix[row - 1, col + 1] == simbol)
- {
- midSize += CheckCellResult(row - 1, col + 1, matrix);
- isAvailable = true;
- }
- else if (col < matrix.GetLength(1) - 1 && row < matrix.GetLength(0) - 1 && matrix[row + 1, col + 1] == simbol)
- {
- midSize += CheckCellResult(row + 1, col + 1, matrix);
- isAvailable = true;
- }
- else if (col < matrix.GetLength(1) - 1 && matrix[row, col + 1] == simbol)
- {
- midSize += CheckCellResult(row, col + 1, matrix);
- isAvailable = true;
- }
- }
- return midSize;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement