C# program:
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 " ) ; } }

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