Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. ```public class Solution {
  2. public int MaxAreaOfIsland(int[,] grid) {
  3. int maxArea = 0;
  4.  
  5. for(int ii = 0; ii < grid.GetLength(0); ii++)
  6. {
  7. Console.WriteLine("Row " + ii);
  8. int bestSeenArea = 0;
  9. for(int jj = 0; jj < grid.GetLength(1); jj++)
  10. {
  11. Console.WriteLine("Seen zero, no recursion!");
  12. if(grid[ii,jj] == 1)
  13. {
  14. grid[ii,jj] = 0;
  15. Console.WriteLine("Seen 1, recursion!");
  16. bestSeenArea += 1 + recursiveFourDirectionSearch(grid, ii, jj);
  17. Console.WriteLine("The size of the island is! " + bestSeenArea);
  18. }
  19. }
  20. maxArea = Math.Max(maxArea, bestSeenArea);
  21. }
  22. return maxArea;
  23. }
  24. public int recursiveFourDirectionSearch(int[,] grid, int row, int column)
  25. {
  26. int size = 0;
  27. int maxRows = grid.GetLength(0);
  28. int maxColumns = grid.GetLength(1);
  29. if(grid[row,column] == 1)
  30. {
  31. if((column < maxColumns) && (grid[row, column + 1] == 1))
  32. {
  33. grid[row, column + 1] = 0;
  34. size += 1 + recursiveFourDirectionSearch(grid, row, column +1);
  35. }
  36. if((column > 0) && (grid[row, column - 1] == 1))
  37. {
  38. grid[row, column - 1] = 0;
  39. size += 1 + recursiveFourDirectionSearch(grid, row, column - 1);
  40. }
  41. if((row < maxRows) && (grid[row + 1, column] == 1))
  42. {
  43. grid[row + 1, column] = 0;
  44. size += 1 + recursiveFourDirectionSearch(grid, row + 1, column);
  45. }
  46. if((row > 0) && (grid[row - 1, column] == 1))
  47. {
  48. grid[row - 1, column] = 0;
  49. size += 1 + recursiveFourDirectionSearch(grid, row - 1, column);
  50. }
  51. }
  52. return size;
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement