Advertisement
Guest User

LargestAreaNeighborElements

a guest
Jan 9th, 2013
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 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. namespace LargestAreaNeighborElements
  8. {
  9.     class LargestAreaNeighborElements
  10.     {
  11.         class Position
  12.         {
  13.             private int x;
  14.             private int y;
  15.  
  16.             public Position()
  17.             {
  18.                 x = 0;
  19.                 y = 0;
  20.             }
  21.  
  22.             public Position(int initialX, int initialY)
  23.             {
  24.                 x = initialX;
  25.                 y = initialY;
  26.             }
  27.  
  28.             public int X
  29.             {
  30.                 get
  31.                 {
  32.                     return x;
  33.                 }
  34.                 set
  35.                 {
  36.                     x = value;
  37.                 }
  38.             }
  39.  
  40.             public int Y
  41.             {
  42.                 get
  43.                 {
  44.                     return y;
  45.                 }
  46.                 set
  47.                 {
  48.                     y = value;
  49.                 }
  50.             }
  51.         }
  52.  
  53.         static bool ValidCell(Position position, int n, int m)
  54.         {
  55.             return (position.X >= 0 && position.X < n && position.Y >= 0 && position.Y < m);
  56.         }
  57.  
  58.         static void Main(string[] args)
  59.         {
  60.             int n = int.Parse(Console.ReadLine());
  61.             int m = int.Parse(Console.ReadLine());
  62.  
  63.             int[,] matrix = new int[n, m];
  64.             for (int i = 0; i < n; ++i)
  65.             {
  66.                 for (int j = 0; j < m; ++j)
  67.                 {
  68.                     matrix[i, j] = int.Parse(Console.ReadLine());
  69.                 }
  70.             }
  71.  
  72.             int[] deltaX = new int[] { -1, 1, 0, 0 };
  73.             int[] deltaY = new int[] { 0, 0, -1, 1 };
  74.  
  75.             bool[,] visited = new bool[n, m];
  76.  
  77.             int largestArea = 0;
  78.             int tmpArea = 0;
  79.             for (int i = 0; i < n; ++i)
  80.             {
  81.                 for (int j = 0; j < m; ++j)
  82.                 {
  83.                     if (visited[i, j] == false)
  84.                     {
  85.                         visited[i, j] = true;
  86.                         tmpArea = 0;
  87.                         Queue<Position> queue = new Queue<Position>();
  88.                         queue.Enqueue(new Position(i, j));
  89.  
  90.                         while (queue.Count > 0)
  91.                         {
  92.                             tmpArea++;
  93.                             Position currentPosition = queue.Dequeue();
  94.  
  95.                             for (int k = 0; k < 4; ++k)
  96.                             {
  97.                                 Position nextPosition = new Position(currentPosition.X + deltaX[k], currentPosition.Y + deltaY[k]);
  98.                                 if (ValidCell(nextPosition, n, m) && visited[nextPosition.X, nextPosition.Y] == false
  99.                                     && matrix[currentPosition.X, currentPosition.Y] == matrix[nextPosition.X, nextPosition.Y])
  100.                                 {
  101.                                     visited[nextPosition.X, nextPosition.Y] = true;
  102.                                     queue.Enqueue(nextPosition);
  103.                                 }
  104.                             }
  105.                         }
  106.  
  107.                         if (tmpArea > largestArea)
  108.                         {
  109.                             largestArea = tmpArea;
  110.                         }
  111.                     }
  112.                 }
  113.             }
  114.  
  115.             Console.WriteLine("The largeast area size is: " + largestArea);
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement