Python program:
mod = 1e9 + 7
MAX = 301
MAXP = 3
MAXK = 600
MAXPREV = 4
dp = [ [ [ [ - 1 for x in range ( MAXPREV + 1 ) ] for y in range ( MAXK ) ] for z in range ( MAXP + 1 ) ] for k in range ( MAX ) ]
vis = [ False ] * MAX
def helper ( col , prevCol , painted , prev , N , P , K ) :
    if ( painted >= K ) :
        continuousCol = 0
        maxContinuousCol = 0
        for i in range ( N ) :
            if ( vis [ i ] == False ) :
                continuousCol += 1
            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 ]
    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 )
    elif ( 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 )
    elif ( 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 )
    elif ( 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 )
    dp [ col ] [ prevCol ] [ painted ] [ prev ] = res % mod
    return dp [ col ] [ prevCol ] [ painted ] [ prev ]
def solve ( n , p , k ) :
    global dp
    global vis
    return helper ( 0 , 0 , 0 , 0 , n , p , k )
if __name__ == " _ _ main _ _ " :
    N = 2
    K = 2
    P = 2
    print ( int ( solve ( N , P , K ) ) )

Index/key variables: col, i, painted, prev, prevCol
Accumulator variables: continuousCol
Iterator variables: i