Java program:
import java . util . * ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static final int MAX = 301 ; static final int MAXP = 3 ; static final int MAXK = 600 ; static final int MAXPREV = 4 ; static int [ ] [ ] [ ] [ ] dp = new int [ MAX ] [ MAXP + 1 ] [ MAXK ] [ MAXPREV + 1 ] ; static boolean [ ] vis = new boolean [ 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 ; Arrays . fill ( vis , false ) ; return helper ( 0 , 0 , 0 , 0 , n , p , K ) ; } public static void main ( String [ ] args ) { int N = 2 , K = 2 , P = 2 ; System . out . print ( solve ( N , P , K ) + "NEW_LINE"); } }

Index/key variables: MAX, MAXK, col, i, j, k, l, painted, prev, prevCol
Accumulator variables: continuousCol, i, j, k, l, res
Iterator variables: i, j, k, l
Class/struct names: GFG