Advertisement
Hristo_B

BFS

Jul 19th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. //* Write a program that finds the largest area of equal neighbor elements in a rectangular matrix and prints its size.
  5. //Hint: you can use the algorithm "Depth-first search" or "Breadth-first search" (find them in Wikipedia).
  6.  
  7. class LargestAreaOfNeighbourElem
  8. {
  9.     static int counter = 0;
  10.  
  11.     static int[,] directions =
  12.     {
  13.         {-1 , 0 },
  14.         { 1 , 0 },
  15.         { 0 , -1},
  16.         { 0 , 1 }
  17.     };
  18.  
  19.     static void TraverseBFS(int[,] arr, bool[,] boolArr, int row, int col)
  20.     {
  21.         Queue<int> queue = new Queue<int>();
  22.         if (!boolArr[row, col])
  23.         {
  24.             queue.Enqueue(arr[row, col]);
  25.             while (queue.Count > 0)
  26.             {
  27.                 int currentNode = queue.Dequeue();
  28.                 counter++;
  29.                 boolArr[row, col] = true;
  30.                 bool downward = false;
  31.                 bool upward = false;
  32.                 bool left = false;
  33.                 bool right = false;
  34.                 for (int i = 0; i < 4; i++)
  35.                 {
  36.                     if (((row - 1) >= 0) && (!boolArr[row - 1, col]) && (arr[row - 1, col] == arr[row, col]) && !upward)
  37.                     {
  38.                         queue.Enqueue(arr[row - 1, col]);
  39.                         upward = true;
  40.                     }
  41.                     else if (((col - 1) >= 0) && (!boolArr[row, col - 1]) && (arr[row, col - 1] == arr[row, col]) && !left)
  42.                     {
  43.                         queue.Enqueue(arr[row, col - 1]);
  44.                         left = true;
  45.                     }
  46.                     else if (((col + 1) <= arr.GetLength(0)) && (!boolArr[row, col + 1]) && (arr[row, col + 1] == arr[row, col]) && !right)
  47.                     {
  48.                         queue.Enqueue(arr[row, col + 1]);
  49.                         right = true;
  50.                     }
  51.                     else if (((row + 1) <= arr.GetLength(1)) && (!boolArr[row + 1, col]) && (arr[row + 1, col] == arr[row, col]) && !downward)
  52.                     {
  53.                         queue.Enqueue(arr[row + 1, col]);
  54.                         downward = true;
  55.                     }
  56.                    
  57.                 }
  58.                 left = false;
  59.                 right = false;
  60.                 downward = false;
  61.                 upward = false;
  62.             }
  63.         }
  64.     }
  65.  
  66.     static void Main()
  67.     {
  68.         int[,] arr =
  69.         {
  70.             { 1, 3, 2, 2, 2, 4},
  71.             { 3, 3, 3, 2, 4, 4},
  72.             { 4, 3, 1, 2, 3, 3},
  73.             { 4, 3, 1, 3, 3, 1},
  74.             { 4, 3, 3, 3, 1, 1},
  75.         };
  76.         bool[,] boolArr = new bool[arr.GetLength(0), arr.GetLength(1)];
  77.         for (int row = 0; row < arr.GetLength(1); row++)
  78.         {
  79.             for (int col = 0; col < arr.GetLength(0); col++)
  80.             {
  81.                 TraverseBFS(arr, boolArr, row, col);
  82.                 Console.WriteLine(counter);
  83.                 counter = 0;
  84.             }
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement