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.
Input: N = 1, P = 3, K = 1
There are 3 ways to paint 1 cell in a 3 x 1 grid.
Input: N = 2, P = 2, K = 2
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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Count of cells in a matrix whose adjacent cells's sum is prime Number
- Color a grid such that all same color cells are connected either horizontally or vertically
- Minimum Numbers of cells that are connected with the smallest path between 3 given cells
- Shortest distance between two cells in a matrix or grid
- Check if cells numbered 1 to K in a grid can be connected after removal of atmost one blocked cell
- Number of cells in the right and left diagonals passing through (x, y) in a matrix
- Ways of filling matrix such that product of all rows and all columns are equal to unity
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Flood fill Algorithm - how to implement fill() in paint?
- Count of ways to select K consecutive empty cells from a given Matrix
- Count number of ways to fill a "n x 4" grid using "1 x 4" tiles
- Print a matrix in alternate manner (left to right then right to left)
- Enlarge a Matrix such that each element occurs in R rows and C columns
- Number of cells a queen can move with obstacles on the chessborad
- Find the number of cells in the table contains X
- Find Number of Even cells in a Zero Matrix after Q queries
- Number of cells in a matrix that satisfy the given condition
- Number of cells in matrix which are equidistant from given two points
- 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 firstname.lastname@example.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.