Advertisement
Guest User

Untitled

a guest
Dec 29th, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  1. /*07. * Write a program that finds the largest area of equal neighbor elements
  2. in a rectangular matrix and prints its size. Hint: you can use the algorithm
  3. "Depth-first search" or "Breadth-first search" (find them in Wikipedia).*/
  4.  
  5. using System;
  6.  
  7. class FindsLargestAreaOfEqualElements
  8. {
  9.     static void Main()
  10.     {
  11.         string[,] field = {{"c", "b", "d", "j", "a", "a"},
  12.                            {"b", "a", "j", "c", "z", "a"},
  13.                            {"l", "a", "a", "j", "a", "a"},
  14.                            {"j", "a", "j", "a", "a", "l"},
  15.                            {"b", "a", "a", "a", "z", "a"}};
  16.  
  17.         int tempLength = 0;
  18.         bool[,] temp = new bool[field.GetLength(0), field.GetLength(1)];
  19.         int maxLength = 1;
  20.         bool[,] largestArea = new bool[field.GetLength(0), field.GetLength(1)];
  21.        
  22.         for (int row = 0; row < field.GetLength(0); row++)
  23.         {
  24.             for ( int col = 0; col < field.GetLength(1); col++)
  25.             {
  26.                 DepthFirstSearch(field, row, col, ref temp, ref tempLength);
  27.  
  28.                  if (tempLength > maxLength)
  29.                 {
  30.                     maxLength = tempLength;
  31.  
  32.                     for (int i = 0; i < temp.GetLength(0); i++)
  33.                     {
  34.                         for (int j = 0; j < temp.GetLength(1); j++)
  35.                         {
  36.                             largestArea[i, j] = temp[i, j];
  37.                         }
  38.                     }
  39.                 }
  40.                
  41.                 tempLength = 0;
  42.                 temp = new bool[field.GetLength(0), field.GetLength(1)];
  43.             }
  44.         }
  45.  
  46.         for (int row = 0; row < field.GetLength(0); row++)
  47.         {
  48.             for (int col = 0; col < field.GetLength(1); col++)
  49.             {
  50.                 if (largestArea[row, col])
  51.                 {
  52.                     Console.ForegroundColor = ConsoleColor.Green;
  53.                     Console.Write(field[row, col] + " ");
  54.                     Console.ForegroundColor = ConsoleColor.Gray;
  55.                 }
  56.                 else
  57.                 {
  58.                     Console.Write(field[row, col] + " ");
  59.                 }
  60.             }
  61.             Console.WriteLine();
  62.         }        
  63.     }
  64.  
  65.     static void DepthFirstSearch(string[,] array, int row, int col, ref bool[,] temp, ref int tempLength)
  66.     {
  67.         temp[row, col] = true;
  68.         tempLength += 1;
  69.         for ( int r = row - 1; r <= row + 1; r++)
  70.         {
  71.             for (int c = col - 1; c <= col + 1; c++)
  72.             {
  73.                 if (c < 0)
  74.                 {
  75.                     c++;
  76.                 }
  77.                 if (c > array.GetLength(1) - 1)
  78.                 {
  79.                     continue;
  80.                 }
  81.                 if (r < 0)
  82.                 {
  83.                     r++;
  84.                 }
  85.                 if (r > array.GetLength(0) - 1)
  86.                 {
  87.                     continue;
  88.                 }
  89.  
  90.                 if (array[row, col] == array[r, c] && !temp[r, c])
  91.                 {
  92.                     temp[r, c] = true;
  93.  
  94.                     DepthFirstSearch(array, r ,c, ref temp, ref tempLength);                            
  95.                 }
  96.             }
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement