Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```public class Solution {
- public int MaxAreaOfIsland(int[,] grid) {
- int maxArea = 0;
- for(int ii = 0; ii < grid.GetLength(0); ii++)
- {
- Console.WriteLine("Row " + ii);
- int bestSeenArea = 0;
- for(int jj = 0; jj < grid.GetLength(1); jj++)
- {
- Console.WriteLine("Seen zero, no recursion!");
- if(grid[ii,jj] == 1)
- {
- grid[ii,jj] = 0;
- Console.WriteLine("Seen 1, recursion!");
- bestSeenArea += 1 + recursiveFourDirectionSearch(grid, ii, jj);
- Console.WriteLine("The size of the island is! " + bestSeenArea);
- }
- }
- maxArea = Math.Max(maxArea, bestSeenArea);
- }
- return maxArea;
- }
- public int recursiveFourDirectionSearch(int[,] grid, int row, int column)
- {
- int size = 0;
- int maxRows = grid.GetLength(0);
- int maxColumns = grid.GetLength(1);
- if(grid[row,column] == 1)
- {
- if((column < maxColumns) && (grid[row, column + 1] == 1))
- {
- grid[row, column + 1] = 0;
- size += 1 + recursiveFourDirectionSearch(grid, row, column +1);
- }
- if((column > 0) && (grid[row, column - 1] == 1))
- {
- grid[row, column - 1] = 0;
- size += 1 + recursiveFourDirectionSearch(grid, row, column - 1);
- }
- if((row < maxRows) && (grid[row + 1, column] == 1))
- {
- grid[row + 1, column] = 0;
- size += 1 + recursiveFourDirectionSearch(grid, row + 1, column);
- }
- if((row > 0) && (grid[row - 1, column] == 1))
- {
- grid[row - 1, column] = 0;
- size += 1 + recursiveFourDirectionSearch(grid, row - 1, column);
- }
- }
- return size;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement