Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- /*
- * Write a program to find the largest connected area of adjacent empty cells in a matrix.
- */
- namespace FinderLargestArea
- {
- class FinderLargestArea
- {
- static char[,] lab =
- {
- {' ', ' ', '*', '*', ' ', ' ', ' '},
- {'*', '*', '*', '*', '*', '*', '*'},
- {' ', '*', ' ', ' ', ' ', ' ', ' '},
- {' ', '*', '*', '*', '*', '*', '*'},
- {' ', '*', ' ', '*', ' ', '*', ' '},
- };
- static int maxCount = 0;
- static int tempCount = 0;
- static int passableAreasCount = 0;
- static void CountEmptyCells(int row, int col)
- {
- if (CheckBoundaries(row, col))
- {
- return;
- }
- if (lab[row, col] != ' ')
- {
- return;
- }
- // Temporary mark the current cell as visited
- lab[row, col] = 's';
- tempCount++;
- // Recursively explore all possible directions
- CountEmptyCells(row, col - 1); // left
- CountEmptyCells(row - 1, col); // up
- CountEmptyCells(row, col + 1); // right
- CountEmptyCells(row + 1, col); // down
- // Mark back the current cell as free
- //lab[row, col] = ' ';
- }
- static bool CheckBoundaries(int row, int col)
- {
- if ((col < 0) || (row < 0) || (col >= lab.GetLength(1)) || (row >= lab.GetLength(0)))
- {
- // We are out of the labyrinth -> can't find a path
- return true;
- }
- else
- {
- return false;
- }
- }
- static void FindLargestArea()
- {
- int temp1 = 0;
- for (int i = 0; i < lab.GetLength(0); i++)
- {
- for (int j = 0; j < lab.GetLength(1); j++)
- {
- if (lab[i, j] != ' ')
- {
- continue;
- }
- else
- {
- CountEmptyCells(i,j);
- temp1 = tempCount;
- passableAreasCount++;
- if(tempCount > maxCount)
- {
- maxCount = tempCount;
- }
- tempCount = 0;
- }
- }
- }
- }
- static void Main(string[] args)
- {
- FindLargestArea();
- Console.WriteLine("The count is {0}", maxCount);
- Console.WriteLine("The count of passable areas is {0}", passableAreasCount);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement