Open In App
Related Articles

Ways to color a 3*N board using 4 colors

Like Article
Save Article
Report issue

Given a 3 X n board, find the number of ways to color it using at most 4 colors such that no two adjacent boxes have the same color. Diagonal neighbors are not treated as adjacent boxes. 
Output the ways%1000000007 as the answer grows quickly.
1<= n < 100000
Examples : 

Input : 1
Output : 36
We can use either a combination of 3 colors
or 2 colors. Now, choosing 3 colors out of 
4 is {4}\choose{3} and arranging them in 3! ways, similarly choosing 2 colors out of 4 is {4}\choose{2} and while arrangingwe can only choose which of them could be at centre, that would be 2 ways. Answer = {4}\choose{3}*3! + {4}\choose{2}*2! = 36Input : 2Output : 588


We are going to solve this using dynamic approach because when a new column is added to the board, the ways in which colors are going to be filled depends just upon the color pattern in the current column. We can only have a combination of two colors and three colors in a column. All possible new columns that can be generated is given in the image. Please consider A, B, C and D as 4 colors. 

Image Containing Color Combination Generation

All possible color combinations that can be generated from current column.

From now, we will refer 3 colors combination for a Nth column of the 3*N board as W(n) and two colors as Y(n). 
We can see that each W can generate 5Y and 11W, and each Y can generate 7Y and 10W. We get two equation from here 
We have two equations now, 

W(n+1) = 10*Y(n)+11*W(n);
Y(n+1) = 7*Y(n)+5*W(n);



// C++ program to find number of ways
// to color a 3 x n grid using 4 colors
// such that no two adjacent have same
// color
#include <iostream>
using namespace std;
int solve(int A)
    // When we to fill single column
    long int color3 = 24;
    long int color2 = 12;
    long int temp = 0;
    for (int i = 2; i <= A; i++)
        temp = color3;
        color3 = (11 * color3 + 10 *
              color2 ) % 1000000007;
        color2 = ( 5 * temp + 7 *
              color2 ) % 1000000007;
    long num = (color3 + color2)
                       % 1000000007;
    return (int)num;
// Driver code
int main()
    int num1 = 1;
    cout << solve(num1) << endl;
    int num2 = 2;
    cout << solve(num2) << endl;
    int num3 = 500;
    cout << solve(num3) << endl;
    int num4 = 10000;
    cout << solve(num4);
    return 0;
// This code is contributed by vt_m.





# Python 3 program to find number of ways
# to color a 3 x n grid using 4 colors
# such that no two adjacent have same
# color
def solve(A):
    # When we to fill single column
    color3 = 24
    color2 = 12
    temp = 0
    for i in range(2, A + 1, 1):
        temp = color3
        color3 = (11 * color3 + 10 * color2 ) % 1000000007
        color2 = ( 5 * temp + 7 * color2 ) % 1000000007
    num = (color3 + color2) % 1000000007
    return num
# Driver code
if __name__ == '__main__':
    num1 = 1
    num2 = 2
    num3 = 500
    num4 = 10000
# This code is contributed by
# Shashank_Sharma





<div id="highlighter_788133" class="syntaxhighlighter nogutter  "><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="plain"><?php</code></div><div class="line number2 index1 alt1"><code class="comments">// PHP program to find number of ways</code></div><div class="line number3 index2 alt2"><code class="comments">// to color a 3 x n grid using 4 colors</code></div><div class="line number4 index3 alt1"><code class="comments">// such that no two adjacent have same</code></div><div class="line number5 index4 alt2"><code class="comments">// color</code></div><div class="line number6 index5 alt1"><code class="keyword">function</code> <code class="plain">solve(</code><code class="variable">$A</code><code class="plain">)</code></div><div class="line number7 index6 alt2"><code class="plain">{</code></div><div class="line number8 index7 alt1"><code class="undefined spaces">    </code> </div><div class="line number9 index8 alt2"><code class="undefined spaces">    </code><code class="comments">// When we to fill single column</code></div><div class="line number10 index9 alt1"><code class="undefined spaces">    </code><code class="variable">$color3</code> <code class="plain">= 24;</code></div><div class="line number11 index10 alt2"><code class="undefined spaces">    </code><code class="variable">$color2</code> <code class="plain">= 12;</code></div><div class="line number12 index11 alt1"><code class="undefined spaces">    </code><code class="variable">$temp</code> <code class="plain">= 0;</code></div><div class="line number13 index12 alt2"><code class="undefined spaces">    </code> </div><div class="line number14 index13 alt1"><code class="undefined spaces">    </code><code class="keyword">for</code> <code class="plain">(</code><code class="variable">$i</code> <code class="plain">= 2; </code><code class="variable">$i</code> <code class="plain"><= </code><code class="variable">$A</code><code class="plain">; </code><code class="variable">$i</code><code class="plain">++) </code></div><div class="line number15 index14 alt2"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number16 index15 alt1"><code class="undefined spaces">        </code><code class="variable">$temp</code> <code class="plain">= </code><code class="variable">$color3</code><code class="plain">;</code></div><div class="line number17 index16 alt2"><code class="undefined spaces">        </code><code class="variable">$color3</code> <code class="plain">= (11 * </code><code class="variable">$color3</code> <code class="plain">+ </code></div><div class="line number18 index17 alt1"><code class="undefined spaces">                   </code><code class="plain">10 * </code><code class="variable">$color2</code> <code class="plain">) % </code></div><div class="line number19 index18 alt2"><code class="undefined spaces">                   </code><code class="plain">1000000007;</code></div><div class="line number20 index19 alt1"><code class="undefined spaces">            </code> </div><div class="line number21 index20 alt2"><code class="undefined spaces">        </code><code class="variable">$color2</code> <code class="plain">= ( 5 * </code><code class="variable">$temp</code> <code class="plain">+ </code></div><div class="line number22 index21 alt1"><code class="undefined spaces">                    </code><code class="plain">7 * </code><code class="variable">$color2</code> <code class="plain">) % </code></div><div class="line number23 index22 alt2"><code class="undefined spaces">                    </code><code class="plain">1000000007;</code></div><div class="line number24 index23 alt1"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number25 index24 alt2"><code class="undefined spaces">    </code> </div><div class="line number26 index25 alt1"><code class="undefined spaces">    </code><code class="variable">$num</code> <code class="plain">= (</code><code class="variable">$color3</code> <code class="plain">+ </code><code class="variable">$color2</code><code class="plain">) % </code></div><div class="line number27 index26 alt2"><code class="undefined spaces">                     </code><code class="plain">1000000007;</code></div><div class="line number28 index27 alt1"><code class="undefined spaces">                        </code> </div><div class="line number29 index28 alt2"><code class="undefined spaces">    </code><code class="keyword">return</code> <code class="plain">(int)</code><code class="variable">$num</code><code class="plain">;</code></div><div class="line number30 index29 alt1"><code class="plain">}</code></div><div class="line number31 index30 alt2"> </div><div class="line number32 index31 alt1"><code class="comments">// Driver code</code></div><div class="line number33 index32 alt2"><code class="variable">$num1</code> <code class="plain">= 1;</code></div><div class="line number34 index33 alt1"><code class="functions">echo</code> <code class="plain">solve(</code><code class="variable">$num1</code><code class="plain">) ,</code><code class="string">"\n"</code><code class="plain">;</code></div><div class="line number35 index34 alt2"> </div><div class="line number36 index35 alt1"><code class="variable">$num2</code> <code class="plain">= 2;</code></div><div class="line number37 index36 alt2"><code class="functions">echo</code> <code class="plain">solve(</code><code class="variable">$num2</code><code class="plain">) ,</code><code class="string">"\n"</code><code class="plain">;</code></div><div class="line number38 index37 alt1"> </div><div class="line number39 index38 alt2"><code class="variable">$num3</code> <code class="plain">= 500;</code></div><div class="line number40 index39 alt1"><code class="functions">echo</code> <code class="plain">solve(</code><code class="variable">$num3</code><code class="plain">),</code><code class="string">"\n"</code><code class="plain">;</code></div><div class="line number41 index40 alt2"> </div><div class="line number42 index41 alt1"><code class="variable">$num4</code> <code class="plain">= 10000;</code></div><div class="line number43 index42 alt2"><code class="functions">echo</code> <code class="plain">solve(</code><code class="variable">$num4</code><code class="plain">);</code></div><div class="line number44 index43 alt1"> </div><div class="line number45 index44 alt2"><code class="comments">// This code is contributed by m_kit.</code></div><div class="line number46 index45 alt1"><code class="plain">?></code></div></div></td></tr></tbody></table></div>




Output : 


Time Complexity: O(N), where number of rows = 3 and number of columns = N of the board.

Auxiliary Space: O(1), no extra space required so it is a constant.


Last Updated : 23 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Similar Reads