def num_distinct_islands(grid):
if not grid:
return 0
distinct_islands = set()
def dfs(row, col, path, direction):
if row < 0 or row >= len(grid) or col < 0 or col >= len(grid[0]) or grid[row][col] != 1:
return
grid[row][col] = 0
path.append(direction)
dfs(row - 1, col, path, 'U')
dfs(row + 1, col, path, 'D')
dfs(row, col - 1, path, 'L')
dfs(row, col + 1, path, 'R')
path.append('B')
for row in range(len(grid)):
for col in range(len(grid[0])):
if grid[row][col] == 1:
path = []
dfs(row, col, path, 'O')
distinct_islands.add(''.join(path))
return len(distinct_islands)
# Example usage
grid = [
[1, 1, 0, 0, 0],
[1, 1, 0, 0, 0],
[0, 0, 0, 1, 1],
[0, 0, 0, 1, 1
]
]
print(num_distinct_islands(grid)) # 1
To embed this program on your website, copy the following code and paste it into your website's HTML: