Number of ways to paint K cells in 3 x N grid such that no P continuous columns are left unpainted | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int mod = 1e9 + 7 ; #define MAX  301 NEW_LINE #define MAXP  3 NEW_LINE #define MAXK  600 NEW_LINE #define MAXPREV  4 NEW_LINE int dp [ MAX ] [ MAXP + 1 ] [ MAXK ] [ MAXPREV + 1 ] ; bool vis [ MAX ] ; int helper ( int col , int prevCol , int painted , int prev , int N , int P , int K ) { if ( painted >= K ) { int continuousCol = 0 ; int maxContinuousCol = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( vis [ i ] == false ) continuousCol ++ ; else { maxContinuousCol = max ( maxContinuousCol , continuousCol ) ; continuousCol = 0 ; } } maxContinuousCol = max ( maxContinuousCol , continuousCol ) ; if ( maxContinuousCol < P ) return 1 ; return 0 ; } if ( col >= N ) return 0 ; if ( dp [ col ] [ prevCol ] [ painted ] [ prev ] != -1 ) return dp [ col ] [ prevCol ] [ painted ] [ prev ] ; int res = 0 ; if ( prev == 0 ) { vis [ col ] = true ; res += ( helper ( col + 1 , 0 , painted + 1 , 1 , N , P , K ) ) % mod ; res += ( helper ( col + 1 , 0 , painted + 1 , 2 , N , P , K ) ) % mod ; res += ( helper ( col + 1 , 0 , painted + 1 , 3 , N , P , K ) ) % mod ; if ( painted + 2 <= K ) { res += ( helper ( col + 1 , 0 , painted + 2 , 4 , N , P , K ) ) % mod ; } vis [ col ] = false ; if ( prevCol + 1 < P ) { res += ( helper ( col + 1 , prevCol + 1 , painted , 0 , N , P , K ) ) % mod ; } } else if ( prev == 1 ) { vis [ col ] = true ; res += ( helper ( col + 1 , 0 , painted + 1 , 2 , N , P , K ) ) % mod ; res += ( helper ( col + 1 , 0 , painted + 1 , 3 , N , P , K ) ) % mod ; vis [ col ] = false ; if ( prevCol + 1 < P ) { res += ( helper ( col + 1 , prevCol + 1 , painted , 0 , N , P , K ) ) % mod ; } } else if ( prev == 2 ) { vis [ col ] = true ; res += ( helper ( col + 1 , 0 , painted + 1 , 1 , N , P , K ) ) % mod ; res += ( helper ( col + 1 , 0 , painted + 1 , 3 , N , P , K ) ) % mod ; if ( painted + 2 <= K ) { res += ( helper ( col + 1 , 0 , painted + 2 , 4 , N , P , K ) ) % mod ; } vis [ col ] = false ; if ( prevCol + 1 < P ) { res += ( helper ( col + 1 , prevCol + 1 , painted , 0 , N , P , K ) ) % mod ; } } else if ( prev == 3 ) { vis [ col ] = true ; res += ( helper ( col + 1 , 0 , painted + 1 , 1 , N , P , K ) ) % mod ; res += ( helper ( col + 1 , 0 , painted + 1 , 2 , N , P , K ) ) % mod ; vis [ col ] = false ; if ( prevCol + 1 < P ) { res += ( helper ( col + 1 , prevCol + 1 , painted , 0 , N , P , K ) ) % mod ; } } else { vis [ col ] = true ; res += ( helper ( col + 1 , 0 , painted + 1 , 2 , N , P , K ) ) % mod ; vis [ col ] = false ; if ( prevCol + 1 < P ) { res += ( helper ( col + 1 , prevCol + 1 , painted , 0 , N , P , K ) ) % mod ; } } return dp [ col ] [ prevCol ] [ painted ] [ prev ] = res % mod ; } int solve ( int n , int p , int k ) { memset ( dp , -1 , sizeof ( dp ) ) ; memset ( vis , false , sizeof ( vis ) ) ; return helper ( 0 , 0 , 0 , 0 , n , p , k ) ; } int main ( ) { int N = 2 , K = 2 , P = 2 ; cout << solve ( N , P , K ) << endl ; return 0 ; }