# Number of ways to paint K cells in 3 x N grid such that no P continuous columns are left unpainted

Given three integers **N**, **P** and **K**, the task is to find the number of ways of painting K cells of 3 x N grid such that no adjacent cells are painted and also no continuous P columns are left unpainted.

**Note**: Diagonal cells are not considered as adjacent cells.

**Examples:**

Input:N = 1, P = 3, K = 1

Output:3

There are 3 ways to paint 1 cell in a 3 x 1 grid.

Input:N = 2, P = 2, K = 2

Output:8

There are 8 ways to paint 2 cells in a 3×2 grid.

Combinations of cells those are painted is shown below –

1) (0, 0) and (1, 1)

2) (0, 0) and (2, 1)

3) (0, 0) and (2, 0)

4) (1, 0) and (0, 1)

5) (1, 0) and (2, 1)

6) (2, 0) and (0, 1)

7) (2, 0) and (1, 1)

8) (0, 1) and (2, 1)

**Approach:** The idea is to use Dynamic Programming to solve this problem. As we know from the problem that column can be painted only when column is not painted. If column is not painted then we have following five cases –

- Paint the first Row.
- Paint the second row.
- Paint the third row.
- Paint first and third row.
- Leave the current column if atleast one column is painted.

Therefore, using this fact we can solve this problem easily.

Below is the implementation of the above approach:

## C++

`// 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 ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `mod = 1e9 + 7; ` `#define MAX 301 ` `#define MAXP 3 ` `#define MAXK 600 ` `#define MAXPREV 4 ` ` ` `int` `dp[MAX][MAXP + 1][MAXK][MAXPREV + 1]; ` ` ` `// Visited array to keep track ` `// of which columns were painted ` `bool` `vis[MAX]; ` ` ` `// Recursive Function to compute the ` `// number of ways to paint the K cells ` `// of the 3 x N grid ` `int` `helper(` `int` `col, ` `int` `prevCol, ` ` ` `int` `painted, ` `int` `prev, ` ` ` `int` `N, ` `int` `P, ` `int` `K) ` `{ ` ` ` ` ` `// Condition to check if total ` ` ` `// cells painted are K ` ` ` `if` `(painted >= K) { ` ` ` `int` `continuousCol = 0; ` ` ` `int` `maxContinuousCol = 0; ` ` ` ` ` `// Check if any P continuous ` ` ` `// columns were left unpainted ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` ` ` `if` `(vis[i] == ` `false` `) ` ` ` `continuousCol++; ` ` ` `else` `{ ` ` ` `maxContinuousCol ` ` ` `= max(maxContinuousCol, ` ` ` `continuousCol); ` ` ` `continuousCol = 0; ` ` ` `} ` ` ` `} ` ` ` ` ` `maxContinuousCol = max( ` ` ` `maxContinuousCol, ` ` ` `continuousCol); ` ` ` ` ` `// Condition to check if no P ` ` ` `// continuous columns were ` ` ` `// left unpainted ` ` ` `if` `(maxContinuousCol < P) ` ` ` `return` `1; ` ` ` ` ` `// return 0 if there are P ` ` ` `// continuous columns are ` ` ` `// left unpainted ` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `// Condition to check if No ` ` ` `// further cells can be ` ` ` `// painted, so return 0 ` ` ` `if` `(col >= N) ` ` ` `return` `0; ` ` ` ` ` `// if already calculated the value ` ` ` `// return the val instead ` ` ` `// of calculating again ` ` ` `if` `(dp[col][prevCol][painted][prev] != -1) ` ` ` `return` `dp[col][prevCol][painted][prev]; ` ` ` ` ` `int` `res = 0; ` ` ` ` ` `// Previous column was not painted ` ` ` `if` `(prev == 0) { ` ` ` ` ` `// Column is painted so, ` ` ` `// make vis[col]=true ` ` ` `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; ` ` ` ` ` `// 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 ` ` ` `if` `(painted + 2 <= K) { ` ` ` `res ` ` ` `+= (helper( ` ` ` `col + 1, 0, painted + 2, ` ` ` `4, N, P, K)) ` ` ` `% mod; ` ` ` `} ` ` ` `vis[col] = ` `false` `; ` ` ` ` ` `// Condition to check if number of ` ` ` `// previous continuous columns left ` ` ` `// unpainted is less than P ` ` ` `if` `(prevCol + 1 < P) { ` ` ` `res ` ` ` `+= (helper( ` ` ` `col + 1, prevCol + 1, ` ` ` `painted, 0, N, P, K)) ` ` ` `% mod; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Condition to check if first row ` ` ` `// was painted in previous column ` ` ` `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; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Condition to check if second row ` ` ` `// was painted in previous column ` ` ` `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; ` ` ` ` ` `// 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 ` ` ` `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; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Condition to check if third row ` ` ` `// was painted in previous column ` ` ` `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; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Condition to check if first and ` ` ` `// third row were painted ` ` ` `// in previous column ` ` ` `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; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Memoize the data and return the ` ` ` `// Computed value ` ` ` `return` `dp[col][prevCol][painted][prev] ` ` ` `= res % mod; ` `} ` ` ` `// Function to find the number of ` `// ways to paint 3 x N grid ` `int` `solve(` `int` `n, ` `int` `p, ` `int` `k) ` `{ ` ` ` `// Set all values ` ` ` `// of dp to -1; ` ` ` `memset` `(dp, -1, ` `sizeof` `(dp)); ` ` ` ` ` `// Set all values of Visited ` ` ` `// array to false ` ` ` `memset` `(vis, ` `false` `, ` `sizeof` `(vis)); ` ` ` ` ` `return` `helper(0, 0, 0, 0, n, p, k); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `int` `N = 2, K = 2, P = 2; ` ` ` `cout << solve(N, P, K) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

8

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Number of ways to paint a tree of N nodes with K distinct colors with given conditions
- Number of cells in the right and left diagonals passing through (x, y) in a matrix
- Number of ways to go from one point to another in a grid
- Count number of ways to fill a "n x 4" grid using "1 x 4" tiles
- Ways to paint stairs with two colors such that two adjacent are not yellow
- Shortest distance between two cells in a matrix or grid
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Count of cells in a matrix whose adjacent cells's sum is prime Number
- Check if cells numbered 1 to K in a grid can be connected after removal of atmost one blocked cell
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Ways to paint N paintings such that adjacent paintings don't have same colors
- Count of ways to select K consecutive empty cells from a given Matrix
- Color a grid such that all same color cells are connected either horizontally or vertically
- Ways of filling matrix such that product of all rows and all columns are equal to unity
- Minimum Numbers of cells that are connected with the smallest path between 3 given cells
- Find the number of cells in the table contains X
- Number of cells in matrix which are equidistant from given two points
- Number of cells in a matrix that satisfy the given condition
- Find Number of Even cells in a Zero Matrix after Q queries
- Total number of cells covered in a matrix after D days

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.