Program to count number of ways we can fill 3 x n box with 2 x 1 dominos in Python

Suppose we have a number n, we have to find the number of ways we can fill a (3 x n) block with 2 x 1 dominos. We can rotate the dominos when required. If the answer is very large then return this mod 10^9 + 7.

For example, if the input is n = 4, then the output will be 11.

Algorithm

To solve this problem, we need to use dynamic programming. The key insight is that we can only fill a 3×n grid if n is even, because each domino covers 2 cells.

We maintain two states ?

  • cs (complete state) ? number of ways to completely fill a 3×i grid
  • os (odd state) ? number of ways to fill a 3×i grid with some cells protruding

Step-by-Step Approach

  • m = 10^9 + 7 (for modulo operations)
  • if n is odd, then return 0 (impossible to fill)
  • Initialize cs := 1, os := 0
  • For i in range 2 to n (increment by 2) ?
    • cs := 3 * cs + os
    • os := 2 * cs + os
  • return cs mod m

Example

Let us see the implementation to get better understanding ?

class Solution:
    def solve(self, n):
        m = (10 ** 9 + 7)
        if n % 2 == 1:
            return 0
        
        cs = 1  # complete state
        os = 0  # odd state
        
        for i in range(2, n + 1, 2):
            new_cs = (3 * cs + os) % m
            new_os = (2 * cs + os) % m
            cs, os = new_cs, new_os
            
        return cs

# Test the solution
ob = Solution()
n = 4
result = ob.solve(n)
print(f"Number of ways to fill 3x{n} grid: {result}")

The output of the above code is ?

Number of ways to fill 3x4 grid: 11

Testing with Different Values

Let's test with multiple values to verify our solution ?

class Solution:
    def solve(self, n):
        m = (10 ** 9 + 7)
        if n % 2 == 1:
            return 0
        
        cs = 1
        os = 0
        
        for i in range(2, n + 1, 2):
            new_cs = (3 * cs + os) % m
            new_os = (2 * cs + os) % m
            cs, os = new_cs, new_os
            
        return cs

# Test with multiple values
ob = Solution()
test_cases = [2, 3, 4, 6, 8]

for n in test_cases:
    result = ob.solve(n)
    print(f"n = {n}: {result} ways")

The output shows different results for various grid sizes ?

n = 2: 3 ways
n = 3: 0 ways
n = 4: 11 ways
n = 6: 41 ways
n = 8: 153 ways

How It Works

The algorithm uses the following recurrence relation ?

  • Complete state (cs) ? represents ways to completely tile a 3×i rectangle
  • Odd state (os) ? represents ways to tile with some protruding pieces
  • The transitions are: cs = 3×cs + os and os = 2×cs + os
  • We only process even values of i since odd widths are impossible to fill

Conclusion

This dynamic programming solution efficiently counts the ways to fill a 3×n grid with 2×1 dominos. The key insight is maintaining two states and using the fact that only even widths can be completely filled.

---
Updated on: 2026-03-25T13:30:04+05:30

312 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements