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
using System ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static readonly int MAX = 301 ; static readonly int MAXP = 3 ; static readonly int MAXK = 600 ; static readonly int MAXPREV = 4 ; static int [ , , , ] dp = new int [ MAX , MAXP + 1 , MAXK , MAXPREV + 1 ] ; static bool [ ] vis = new bool [ MAX ] ; static 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 = Math . Max ( maxContinuousCol , continuousCol ) ; continuousCol = 0 ; } } maxContinuousCol = Math . 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 ; } static int solve ( int n , int p , int K ) { for ( int i = 0 ; i < MAX ; i ++ ) for ( int j = 0 ; j < MAXP + 1 ; j ++ ) for ( int k = 0 ; k < MAXK ; k ++ ) for ( int l = 0 ; l < MAXPREV + 1 ; l ++ ) dp [ i , j , k , l ] = - 1 ; for ( int i = 0 ; i < vis . Length ; i ++ ) vis [ i ] = false ; return helper ( 0 , 0 , 0 , 0 , n , p , K ) ; } public static void Main ( String [ ] args ) { int N = 2 , K = 2 , P = 2 ; Console . Write ( solve ( N , P , K ) + " STRNEWLINE " ) ; } }