Dynamic Programming Series
Dynamic Programming Series
Day 14
Ways To Color 3xN Board using 4 colors
(InterviewBit)
Problem Description
Given a 3 x A board, find the number of ways to color it using at most 4 colors such that no 2 adjacent boxes have same color.
Diagonal neighbors are not treated as adjacent boxes.
Return the ways modulo 109 + 7 as the answer grows quickly.
Problem Description
Given a 3 x A board, find the number of ways to color it using at most 4 colors such that no 2 adjacent boxes have same color.
Diagonal neighbors are not treated as adjacent boxes.
Return the ways modulo 109 + 7 as the answer grows quickly.
A = 1
Answer = 36
Problem Description
Given a 3 x A board, find the number of ways to color it using at most 4 colors such that no 2 adjacent boxes have same color.
Diagonal neighbors are not treated as adjacent boxes.
Return the ways modulo 109 + 7 as the answer grows quickly.
A = 1
Answer = 36
| 4 |
| 3 |
| 3 |
X
X
=
36
How To approach this problem?
Suppose there are four color A, B, C, and D
| A |
| B |
| C |
| A |
| B |
| A |
......
| D |
| C |
| D |
36
1st Column
How To approach this problem?
Suppose there are four color A, B, C, and D
| A |
| B |
| C |
| A |
| B |
| A |
......
| D |
| C |
| D |
36
1st Column
2nd Column
Try All 36 possible combination that we can fill in the 2nd column which do not break the constraints
How To approach this problem?
Suppose there are four color A, B, C, and D
| A |
| B |
| C |
| A |
| B |
| A |
......
| D |
| C |
| D |
36
1st Column
2nd Column
Try All 36 possible combination that we can fill in the 2nd column which do not break the constraints
How To approach this problem?
Suppose there are four color A, B, C, and D
| A |
| B |
| C |
| A |
| B |
| A |
......
| D |
| C |
| D |
36
1st Column
2nd Column
Try All 36 possible combination that we can fill in the 2nd column which do not break the constraints
Let's try to form the solution of this approach?
1. Store all 36 combinations.
2. for a ith column if it possible to put three colors x,y,z we will count this as one way of arrangement in that column multiplying total arrangement possible till i-1 th column.
Code
#define mod 1000000007
#define ll long long
struct triplet{
int x, y, z;
};
vector<triplet> trip; //vector of 36 triplets
void fillTriplets(){
//trip vector contain the triplets of color that can be used to paint a certain column
trip.clear();
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
for(int k=0; k<4; k++){
if(i!=j && j!=k) trip.push_back({i,j,k});
}
}
}
}
int dp[4][4][4][100005];
bool isCompatible(const triplet& t1, const triplet& t2){
//check if triplets t1 and t2 are compatible
if(t1.x==t2.x || t1.y==t2.y || t1.z==t2.z ){
return 0;
}
return 1;
}
int Solution::solve(int n){
fillTriplets();
if(n<=0) return -1;
//Bottom-up dp
for(int i=1; i<=n; i++){
for(int j=0; j<36; j++){
if(i==1) dp[trip[j].x][trip[j].y][trip[j].z][i] = 1;
else{
ll temp = 0;
for(int k=0; k<36; k++){
if(isCompatible(trip[j], trip[k])){
temp += dp[trip[k].x][trip[k].y][trip[k].z][i-1];
temp %= mod;
}
}
dp[trip[j].x][trip[j].y][trip[j].z][i] = temp;
}
}
}
ll ans = 0;
for(int i=0; i<trip.size(); i++){
ans = (ans + dp[trip[i].x][trip[i].y][trip[i].z][n])%mod;
}
return ans;
}
Let's understand the code too.
| A | B | C | D |
| B | C | D | A |
| A | B | C | D |
Triplet vector
Contains all possible 36 combinations
...
| 36 | 552 | 9024 |
DP[i][j][k]
Another easier approach to solve this question.
| A |
| B |
| C |
Colors = (A, B, C, D)
| A |
| B |
| A |
4C3*(3*2*1) = 24
4C2*(2*1*1) = 12
+
Another easier approach to solve this question.
Colors = (A, B, C, D)

Image credit: geeksforgeeks
How To approach this problem?
These numbers 5, 11, 7, 10 are constants
Now we have a simple dp relation.
c2(i) = 7*c2(i-1) + 5*c3(i-1)
c3(i) = 10*c2(i-1) + 11*c3(i-1)
Now we have a simple dp relation.
c2(i) = 7*c2(i-1) + 5*c3(i-1)
c3(i) = 10*c2(i-1) + 11*c3(i-1)
Answer at any ith level is c3+c2
Code
int Solution::solve(int A) {
long long mod = 1e9+7;
long long c3 = 24;
long long c2 = 12;
for (int i = 2;i <= A;i++)
{
long long temp = (7*c2 + 5*c3)% mod;
c3 = (10*c2 + 11*c3)%mod;
c2 = temp%mod;
cout << c2+c3 << "\n";
}
return (c2+c3)%mod;
}
So that's It for Day 14
So that's It for Day 14
Concepts that we have learnt
So that's It for Day 14
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. Combinatorics Dynamic Programming.
So that's It for Day 14
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. Combinatorics Dynamic Programming
2. Use of Brute force with DP.
So that's It for Day 14
We have taken a very first step in Dynamic Programming.
Concepts that we have learnt
1. Combinatorics Dynamic Programming.
2. Use of Brute force with DP.
Still, Have any Doubts???
Still, Have any Doubts???
Comment it You will get a reply
OR
Send your doubt to email provided in description.
Copy of Ways to color 3xN board using 4 colors
By Chirayu Jain
Copy of Ways to color 3xN board using 4 colors
- 276