Number of ways to paint K cells in 3 x N grid such that no P continuous columns are left unpainted | Python 3 implementation to find the number of ways to paint K cells of 3 x N grid such that No two adjacent cells are painted ; Visited array to keep track of which columns were painted ; Recursive Function to compute the number of ways to paint the K cells of the 3 x N grid ; Condition to check if total cells painted are K ; Check if any P continuous columns were left unpainted ; Condition to check if no P continuous columns were left unpainted ; return 0 if there are P continuous columns are left unpainted ; Condition to check if No further cells can be painted , so return 0 ; if already calculated the value return the val instead of calculating again ; Previous column was not painted ; Column is painted so , make vis [ col ] = true ; Condition to check if the number of cells to be painted is equal to or more than 2 , then we can paint first and third row ; Condition to check if number of previous continuous columns left unpainted is less than P ; Condition to check if first row was painted in previous column ; Condition to check if second row was painted in previous column ; Condition to check if the number of cells to be painted is equal to or more than 2 , then we can paint first and third row ; Condition to check if third row was painted in previous column ; Condition to check if first and third row were painted in previous column ; Memoize the data and return the Computed value ; Function to find the number of ways to paint 3 x N grid ; Set all values of dp to - 1 ; ; Set all values of Visited array to false ; Driver Code
mod = 1e9 + 7 NEW_LINE MAX = 301 NEW_LINE MAXP = 3 NEW_LINE MAXK = 600 NEW_LINE MAXPREV = 4 NEW_LINE dp = [ [ [ [ - 1 for x in range ( MAXPREV + 1 ) ] for y in range ( MAXK ) ] for z in range ( MAXP + 1 ) ] for k in range ( MAX ) ] NEW_LINE vis = [ False ] * MAX NEW_LINE def helper ( col , prevCol , painted , prev , N , P , K ) : NEW_LINE INDENT if ( painted >= K ) : NEW_LINE INDENT continuousCol = 0 NEW_LINE maxContinuousCol = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( vis [ i ] == False ) : NEW_LINE INDENT continuousCol += 1 NEW_LINE DEDENT else : NEW_LINE INDENT maxContinuousCol = max ( maxContinuousCol , continuousCol ) NEW_LINE continuousCol = 0 NEW_LINE DEDENT DEDENT maxContinuousCol = max ( maxContinuousCol , continuousCol ) NEW_LINE if ( maxContinuousCol < P ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT if ( col >= N ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ col ] [ prevCol ] [ painted ] [ prev ] != - 1 ) : NEW_LINE INDENT return dp [ col ] [ prevCol ] [ painted ] [ prev ] NEW_LINE DEDENT res = 0 NEW_LINE if ( prev == 0 ) : NEW_LINE INDENT vis [ col ] = True NEW_LINE res += ( ( helper ( col + 1 , 0 , painted + 1 , 1 , N , P , K ) ) % mod ) NEW_LINE res += ( ( helper ( col + 1 , 0 , painted + 1 , 2 , N , P , K ) ) % mod ) NEW_LINE res += ( ( helper ( col + 1 , 0 , painted + 1 , 3 , N , P , K ) ) % mod ) NEW_LINE if ( painted + 2 <= K ) : NEW_LINE INDENT res += ( ( helper ( col + 1 , 0 , painted + 2 , 4 , N , P , K ) ) % mod ) NEW_LINE DEDENT vis [ col ] = False NEW_LINE if ( prevCol + 1 < P ) : NEW_LINE INDENT res += ( ( helper ( col + 1 , prevCol + 1 , painted , 0 , N , P , K ) ) % mod ) NEW_LINE DEDENT DEDENT elif ( prev == 1 ) : NEW_LINE INDENT vis [ col ] = True NEW_LINE res += ( ( helper ( col + 1 , 0 , painted + 1 , 2 , N , P , K ) ) % mod ) NEW_LINE res += ( ( helper ( col + 1 , 0 , painted + 1 , 3 , N , P , K ) ) % mod ) NEW_LINE vis [ col ] = False NEW_LINE if ( prevCol + 1 < P ) : NEW_LINE INDENT res += ( ( helper ( col + 1 , prevCol + 1 , painted , 0 , N , P , K ) ) % mod ) NEW_LINE DEDENT DEDENT elif ( prev == 2 ) : NEW_LINE INDENT vis [ col ] = True NEW_LINE res += ( ( helper ( col + 1 , 0 , painted + 1 , 1 , N , P , K ) ) % mod ) NEW_LINE res += ( ( helper ( col + 1 , 0 , painted + 1 , 3 , N , P , K ) ) % mod ) NEW_LINE if ( painted + 2 <= K ) : NEW_LINE INDENT res += ( ( helper ( col + 1 , 0 , painted + 2 , 4 , N , P , K ) ) % mod ) NEW_LINE DEDENT vis [ col ] = False NEW_LINE if ( prevCol + 1 < P ) : NEW_LINE INDENT res += ( ( helper ( col + 1 , prevCol + 1 , painted , 0 , N , P , K ) ) % mod ) NEW_LINE DEDENT DEDENT elif ( prev == 3 ) : NEW_LINE INDENT vis [ col ] = True NEW_LINE res += ( ( helper ( col + 1 , 0 , painted + 1 , 1 , N , P , K ) ) % mod ) NEW_LINE res += ( ( helper ( col + 1 , 0 , painted + 1 , 2 , N , P , K ) ) % mod ) NEW_LINE vis [ col ] = False NEW_LINE if ( prevCol + 1 < P ) : NEW_LINE INDENT res += ( ( helper ( col + 1 , prevCol + 1 , painted , 0 , N , P , K ) ) % mod ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT vis [ col ] = True NEW_LINE res += ( ( helper ( col + 1 , 0 , painted + 1 , 2 , N , P , K ) ) % mod ) NEW_LINE vis [ col ] = False NEW_LINE if ( prevCol + 1 < P ) : NEW_LINE INDENT res += ( ( helper ( col + 1 , prevCol + 1 , painted , 0 , N , P , K ) ) % mod ) NEW_LINE DEDENT DEDENT dp [ col ] [ prevCol ] [ painted ] [ prev ] = res % mod NEW_LINE return dp [ col ] [ prevCol ] [ painted ] [ prev ] NEW_LINE DEDENT def solve ( n , p , k ) : NEW_LINE INDENT global dp NEW_LINE global vis NEW_LINE return helper ( 0 , 0 , 0 , 0 , n , p , k ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 NEW_LINE K = 2 NEW_LINE P = 2 NEW_LINE print ( int ( solve ( N , P , K ) ) ) NEW_LINE DEDENT