Guest User

Untitled

a guest
Apr 26th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace P27.LargestAreaOfEqualNumbers
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[,] matrix =
  11.             {
  12.                 { 1, 0, 1, 1, 1, 1 },
  13.                 { 0, 3, 3, 1, 4, 1 },
  14.                 { 1, 3, 1, 1, 3, 1 },
  15.                 { 1, 3, 1, 3, 3, 1 },
  16.                 { 1, 3, 3, 3, 3, 1 }
  17.             };
  18.             Console.WriteLine(FindLargestArreaOfEqualElements(matrix));          
  19.         }
  20.        
  21.  
  22.         private static int FindLargestArreaOfEqualElements(int[,] pMatrix)
  23.         {
  24.             int result = 0;
  25.             int bestResult = 0;
  26.             for (int row = 0; row < pMatrix.GetLength(0); row++)
  27.             {
  28.                 for (int col = 0; col < pMatrix.GetLength(1); col++)
  29.                 {
  30.                     if (pMatrix[row, col] != int.MinValue)
  31.                     {
  32.                         result = FindArreaOfEqualElements(pMatrix, row, col, pMatrix[row, col]);
  33.                         if (result > bestResult)
  34.                         {
  35.                             bestResult = result;
  36.                         }
  37.                     }
  38.                 }
  39.             }
  40.             return bestResult;
  41.         }
  42.  
  43.         private static int FindArreaOfEqualElements(int[,] pMatrix ,int row,int col, int value)
  44.         {          
  45.             int result = 0;
  46.             Queue<Point2D> visitedPointsQueue = new Queue<Point2D>();
  47.             visitedPointsQueue.Enqueue(new Point2D(row,col));
  48.             while (visitedPointsQueue.Count > 0)
  49.             {
  50.                 Point2D currentPoint = visitedPointsQueue.Dequeue();
  51.                 if(pMatrix[currentPoint.X,currentPoint.Y] == value)
  52.                 {
  53.                     result++;
  54.                 }
  55.                 pMatrix[currentPoint.X, currentPoint.Y] = int.MinValue;
  56.                 if (currentPoint.X > 0 && pMatrix[currentPoint.X-1,currentPoint.Y] == value)
  57.                 {
  58.                     visitedPointsQueue.Enqueue(new Point2D(currentPoint.X-1, currentPoint.Y));
  59.                 }
  60.                 if (currentPoint.X < pMatrix.GetLength(0) - 1 && pMatrix[currentPoint.X + 1, currentPoint.Y] == value)
  61.                 {
  62.                     visitedPointsQueue.Enqueue(new Point2D(currentPoint.X + 1, currentPoint.Y));
  63.                 }
  64.                 if (currentPoint.Y > 0 && pMatrix[currentPoint.X, currentPoint.Y - 1] == value)
  65.                 {
  66.                     visitedPointsQueue.Enqueue(new Point2D(currentPoint.X, currentPoint.Y - 1));
  67.                 }
  68.                 if (currentPoint.Y < pMatrix.GetLength(1) - 1 && pMatrix[currentPoint.X, currentPoint.Y + 1] == value)
  69.                 {
  70.                     visitedPointsQueue.Enqueue(new Point2D(currentPoint.X, currentPoint.Y + 1));
  71.                 }              
  72.             }
  73.             return result;
  74.         }
  75.  
  76.         struct Point2D
  77.         {
  78.             private int x;
  79.             private int y;
  80.             public Point2D(int x = 0, int y = 0)
  81.             {
  82.                 this.x = x;
  83.                 this.y = y;
  84.             }
  85.             public int X
  86.             {
  87.                 get
  88.                 {
  89.                     return this.x;
  90.                 }
  91.                 set
  92.                 {
  93.                     this.x = value;
  94.                 }
  95.             }
  96.  
  97.             public int Y
  98.             {
  99.                 get
  100.                 {
  101.                     return this.y;
  102.                 }
  103.                 set
  104.                 {
  105.                     this.y = value;
  106.                 }
  107.             }
  108.         }
  109.     }
  110. }
Add Comment
Please, Sign In to add comment