Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. /*
  6.  * Write a program to find the largest connected area of adjacent empty cells in a matrix.
  7.  */
  8. namespace FinderLargestArea
  9. {
  10.     class FinderLargestArea
  11.     {
  12.         static char[,] lab =
  13.     {
  14.         {' ', ' ', '*', '*', ' ', ' ', ' '},
  15.         {'*', '*', '*', '*', '*', '*', '*'},
  16.         {' ', '*', ' ', ' ', ' ', ' ', ' '},
  17.         {' ', '*', '*', '*', '*', '*', '*'},
  18.         {' ', '*', ' ', '*', ' ', '*', ' '},
  19.     };
  20.         static int maxCount = 0;
  21.         static int tempCount = 0;
  22.         static int passableAreasCount = 0;
  23.         static void CountEmptyCells(int row, int col)
  24.         {
  25.             if (CheckBoundaries(row, col))
  26.             {
  27.                 return;
  28.             }
  29.            
  30.             if (lab[row, col] != ' ')
  31.             {
  32.                 return;
  33.             }
  34.  
  35.             // Temporary mark the current cell as visited
  36.             lab[row, col] = 's';
  37.             tempCount++;
  38.             // Recursively explore all possible directions
  39.             CountEmptyCells(row, col - 1); // left
  40.             CountEmptyCells(row - 1, col); // up
  41.             CountEmptyCells(row, col + 1); // right
  42.             CountEmptyCells(row + 1, col); // down
  43.             // Mark back the current cell as free
  44.             //lab[row, col] = ' ';
  45.         }
  46.  
  47.         static bool CheckBoundaries(int row, int col)
  48.         {
  49.             if ((col < 0) || (row < 0) || (col >= lab.GetLength(1)) || (row >= lab.GetLength(0)))
  50.             {
  51.                 // We are out of the labyrinth -> can't find a path
  52.                 return true;
  53.             }
  54.             else
  55.             {
  56.                 return false;
  57.             }
  58.         }
  59.  
  60.         static void FindLargestArea()
  61.         {
  62.             int temp1 = 0;
  63.             for (int i = 0; i < lab.GetLength(0); i++)
  64.             {
  65.                 for (int j = 0; j < lab.GetLength(1); j++)
  66.                 {
  67.                     if (lab[i, j] != ' ')
  68.                     {
  69.                         continue;
  70.                     }
  71.                     else
  72.                     {
  73.                         CountEmptyCells(i,j);
  74.                         temp1 = tempCount;
  75.                         passableAreasCount++;
  76.                         if(tempCount > maxCount)
  77.                         {
  78.                             maxCount = tempCount;                            
  79.                         }
  80.                         tempCount = 0;
  81.                     }
  82.                 }
  83.             }
  84.         }
  85.  
  86.         static void Main(string[] args)
  87.         {
  88.             FindLargestArea();
  89.             Console.WriteLine("The count is {0}", maxCount);
  90.             Console.WriteLine("The count of passable areas is {0}", passableAreasCount);
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement