jsam
9/10/2015 - 10:28 AM

Group

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