Skip to content

Instantly share code, notes, and snippets.

@jsam
Last active September 10, 2015 10:29
Show Gist options
  • Save jsam/fed4b05ee5721d3bb3d9 to your computer and use it in GitHub Desktop.
Save jsam/fed4b05ee5721d3bb3d9 to your computer and use it in GitHub Desktop.
Group
from array import array
import numpy as np
import math
import sys
sys.setrecursionlimit(15000)
GRID = None
def valid(i, j, row, column):
return i >= 0 and i < row and j >= 0 and j < column and GRID[i][j] == 'Y'
def burndown(i, j, row, column):
if valid(i, j, row, column):
GRID[i][j] = 'N'
burndown(i - 1, j, row, column)
burndown(i, j - 1, row, column)
burndown(i + 1, j, row, column)
burndown(i, j + 1, row, column)
def Group( grid):
global GRID
GRID = np.array([np.array(list(grid[row])) for row in xrange(0, len(grid))], dtype=str)
islands = 0
for row in xrange(0, len(grid)):
for col in xrange(0, len(grid[0])):
if GRID[row][col] == 'Y':
islands += 1
burndown(row, col, len(grid), len(grid[0]))
modulo_arrangements = 0
for k in xrange(0, islands + 1):
if k % 2 == 0:
modulo_arrangements += math.factorial(islands) / (math.factorial(k) * math.factorial(islands - k))
return modulo_arrangements % 1000000007
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment