Advertisement
avisrivastava254084

Untitled

Oct 4th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.19 KB | None | 0 0
  1. islands = []
  2. explored_points = {}
  3.  
  4.  
  5. class Solution:
  6.     def check_valid(self, grid, i, j):
  7.         num_rows = len(grid)
  8.         num_columns = len(grid[0])
  9.         if int(i) < 0 or int(i) >= num_rows:
  10.             return False
  11.         if int(j) < 0 or int(j) >= num_columns:
  12.             return False
  13.         return True
  14.  
  15.     # assumes grid[i][j]==1
  16.     def explore_islands(self, grid, i, j, starting_i, starting_j, current_stack={}):
  17.         global explored_points
  18.         global islands
  19.         if self.check_valid(grid, i, j-1) and not(explored_points.get('{}{}'.format(i, j-1), None)):
  20.             if grid[i][j-1] == '1':
  21.                 current_stack['{}{}'.format(i, j-1)] = 1
  22.                 explored_points['{}{}'.format(i, j-1)] = 1
  23.                 current_stack = self.explore_islands(
  24.                     grid, i, j-1, starting_i, starting_j, current_stack=current_stack
  25.                 )
  26.         if self.check_valid(grid, i, j+1) and not(explored_points.get('{}{}'.format(i, j+1), None)):
  27.             if grid[i][j+1] == '1':
  28.                 current_stack['{}{}'.format(i, j+1)] = 1
  29.                 explored_points['{}{}'.format(i, j+1)] = 1
  30.                 current_stack = self.explore_islands(
  31.                     grid, i, j+1, starting_i, starting_j, current_stack=current_stack
  32.                 )
  33.         if self.check_valid(grid, i-1, j) and not(explored_points.get('{}{}'.format(i-1, j), None)):
  34.             if grid[i-1][j] == '1':
  35.                 current_stack['{}{}'.format(i-1, j)] = 1
  36.                 explored_points['{}{}'.format(i-1, j)] = 1
  37.                 current_stack = self.explore_islands(
  38.                     grid, i-1, j, starting_i, starting_j, current_stack=current_stack
  39.                 )
  40.         if self.check_valid(grid, i+1, j) and not(explored_points.get('{}{}'.format(i+1, j), None)):
  41.             if grid[i+1][j] == '1':
  42.                 current_stack['{}{}'.format(i+1, j)] = 1
  43.                 explored_points['{}{}'.format(i+1, j)] = 1
  44.                 current_stack = self.explore_islands(
  45.                     grid, i+1, j, starting_i, starting_j, current_stack=current_stack
  46.                 )
  47.         # following is the starting point, reached after exploring all the neighbours
  48.         if starting_i == i and starting_j == j:
  49.             islands.append(current_stack)
  50.             return
  51.         return current_stack
  52.  
  53.     def numIslands(self, grid) -> int:
  54.         global islands
  55.         global explored_points
  56.         for row in range(len(grid)):
  57.             for column in range(len(grid[row])):
  58.                 # print('Checking {} row and {} column'.format(row, column))
  59.                 if ((not explored_points.get('{}{}'.format(row, column), None)) and (grid[row][column] == '1')):
  60.                     explored_points['{}{}'.format(row, column)] = 1
  61.                     self.explore_islands(grid, row, column, row, column, current_stack={
  62.                                          '{}{}'.format(row, column): 1})
  63.         return(len(islands))
  64.  
  65.  
  66. solution = Solution()
  67. # print(solution.numIslands(
  68. #     [["1", "1", "1", "1", "0"], ["1", "1", "0", "1", "0"], [
  69. #         "1", "1", "0", "0", "0"], ["0", "0", "0", "0", "0"]]
  70. # ))
  71. print(solution.numIslands([]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement