Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace P27.LargestAreaOfEqualNumbers
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[,] matrix =
- {
- { 1, 0, 1, 1, 1, 1 },
- { 0, 3, 3, 1, 4, 1 },
- { 1, 3, 1, 1, 3, 1 },
- { 1, 3, 1, 3, 3, 1 },
- { 1, 3, 3, 3, 3, 1 }
- };
- Console.WriteLine(FindLargestArreaOfEqualElements(matrix));
- }
- private static int FindLargestArreaOfEqualElements(int[,] pMatrix)
- {
- int result = 0;
- int bestResult = 0;
- for (int row = 0; row < pMatrix.GetLength(0); row++)
- {
- for (int col = 0; col < pMatrix.GetLength(1); col++)
- {
- if (pMatrix[row, col] != int.MinValue)
- {
- result = FindArreaOfEqualElements(pMatrix, row, col, pMatrix[row, col]);
- if (result > bestResult)
- {
- bestResult = result;
- }
- }
- }
- }
- return bestResult;
- }
- private static int FindArreaOfEqualElements(int[,] pMatrix ,int row,int col, int value)
- {
- int result = 0;
- Queue<Point2D> visitedPointsQueue = new Queue<Point2D>();
- visitedPointsQueue.Enqueue(new Point2D(row,col));
- while (visitedPointsQueue.Count > 0)
- {
- Point2D currentPoint = visitedPointsQueue.Dequeue();
- if(pMatrix[currentPoint.X,currentPoint.Y] == value)
- {
- result++;
- }
- pMatrix[currentPoint.X, currentPoint.Y] = int.MinValue;
- if (currentPoint.X > 0 && pMatrix[currentPoint.X-1,currentPoint.Y] == value)
- {
- visitedPointsQueue.Enqueue(new Point2D(currentPoint.X-1, currentPoint.Y));
- }
- if (currentPoint.X < pMatrix.GetLength(0) - 1 && pMatrix[currentPoint.X + 1, currentPoint.Y] == value)
- {
- visitedPointsQueue.Enqueue(new Point2D(currentPoint.X + 1, currentPoint.Y));
- }
- if (currentPoint.Y > 0 && pMatrix[currentPoint.X, currentPoint.Y - 1] == value)
- {
- visitedPointsQueue.Enqueue(new Point2D(currentPoint.X, currentPoint.Y - 1));
- }
- if (currentPoint.Y < pMatrix.GetLength(1) - 1 && pMatrix[currentPoint.X, currentPoint.Y + 1] == value)
- {
- visitedPointsQueue.Enqueue(new Point2D(currentPoint.X, currentPoint.Y + 1));
- }
- }
- return result;
- }
- struct Point2D
- {
- private int x;
- private int y;
- public Point2D(int x = 0, int y = 0)
- {
- this.x = x;
- this.y = y;
- }
- public int X
- {
- get
- {
- return this.x;
- }
- set
- {
- this.x = value;
- }
- }
- public int Y
- {
- get
- {
- return this.y;
- }
- set
- {
- this.y = value;
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment