Open In App

Tiling Problem

Last Updated : 21 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a “2 x n” board and tiles of size “2 x 1”, count the number of ways to tile the given board using the 2 x 1 tiles. A tile can either be placed horizontally i.e., as a 1 x 2 tile or vertically i.e., as 2 x 1 tile. 

Examples: 

Input: n = 4
Output: 5
Explanation:
For a 2 x 4 board, there are 5 ways

  • All 4 vertical (1 way)
  • All 4 horizontal (1 way)
  • 2 vertical and 2 horizontal (3 ways)

Input: n = 3
Output: 3
Explanation:
We need 3 tiles to tile the board of size  2 x 3.
We can tile the board using following ways

  • Place all 3 tiles vertically.
  • Place 1 tile vertically and remaining 2 tiles horizontally (2 ways)

tilingproblem

 

Implementation – 

Let “count(n)” be the count of ways to place tiles on a “2 x n” grid, we have following two ways to place first tile. 
1) If we place first tile vertically, the problem reduces to “count(n-1)” 
2) If we place first tile horizontally, we have to place second tile also horizontally. So the problem reduces to “count(n-2)” 
Therefore, count(n) can be written as below. 

   count(n) = n if n = 1 or n = 2
   count(n) = count(n-1) + count(n-2)

Here’s the code for the above approach:

C++




// C++ program to count the
// no. of ways to place 2*1 size
// tiles in 2*n size board.
#include <iostream>
using namespace std;
 
int getNoOfWays(int n)
{
    // Base case
    if (n <= 2)
      return n;
 
    return getNoOfWays(n - 1) + getNoOfWays(n - 2);
}
 
// Driver Function
int main()
{
    cout << getNoOfWays(4) << endl;
    cout << getNoOfWays(3);
    return 0;
}


Java




/* Java program to count the
 no of ways to place 2*1 size
 tiles in 2*n size board. */
import java.io.*;
 
class GFG {
  static int getNoOfWays(int n)
  {
 
    // Base case
    if (n <= 2) {
      return n;
    }
    return getNoOfWays(n - 1) + getNoOfWays(n - 2);
  }
 
  // Driver Function
  public static void main(String[] args)
  {
    System.out.println(getNoOfWays(4));
    System.out.println(getNoOfWays(3));
  }
}
 
// This code is contributed by ashwinaditya21.


Python3




# Python3 program to count the
# no. of ways to place 2*1 size
# tiles in 2*n size board.
def getNoOfWays(n):
   
    # Base case
    if n <= 2:
        return n
 
    return getNoOfWays(n - 1) + getNoOfWays(n - 2)
 
# Driver Code
print(getNoOfWays(4))
print(getNoOfWays(3))
 
# This code is contributed by Kevin Joshi


C#




// C# program to implement
// the above approach
using System;
 
class GFG
{
     
static int getNoOfWays(int n)
  {
  
    // Base case
    if (n <= 2) {
      return n;
    }
    return getNoOfWays(n - 1) + getNoOfWays(n - 2);
  }
 
// Driver Code
public static void Main()
{
    Console.WriteLine(getNoOfWays(4));
    Console.WriteLine(getNoOfWays(3));
}
}
 
// This code is contributed by code_hunt.


Javascript




<script>
// JavaScript program to count the
// no. of ways to place 2*1 size
// tiles in 2*n size board.
 
function getNoOfWays(n)
{
    // Base case
    if (n <= 2)
      return n;
       
    return getNoOfWays(n - 1) + getNoOfWays(n - 2);
}
 
// Driver Function
document.write(getNoOfWays(4));
document.write(getNoOfWays(3));
 
// This code is contributed by shinjanpatra
</script>


Output

5
3

Time Complexity: O(2^n)
Auxiliary Space: O(1)

The above recurrence is nothing but Fibonacci Number expression. We can find n’th Fibonacci number in O(Log n) time, see below for all method to find n’th Fibonacci Number. 
https://youtu.be/NyICqRtePVs 
https://youtu.be/U9ylW7NsHlI 
Different methods for n’th Fibonacci Number
Count the number of ways to tile the floor of size n x m using 1 x m size tiles 

 



Similar Reads

Domino and Tromino tiling problem
\\Given a positive integer N, the task is to find the number of ways to fill the board of dimension 2*N with a tile of dimensions 2 × 1, 1 × 2, (also known as domino) and an 'L' shaped tile(also know as tromino) show below that can be rotated by 90 degrees. The L shape tile:XXXAfter rotating L shape tile by 90:XX XorXXXExamples: Input: N = 3Output:
20 min read
Tiling Problem using Divide and Conquer algorithm
Given a n by n board where n is of form 2k where k &gt;= 1 (Basically n is a power of 2 with minimum value as 2). The board has one missing cell (of size 1 x 1). Fill the board using L shaped tiles. A L shaped tile is a 2 x 2 square with one cell of size 1x1 missing. Figure 1: An example inputThis problem can be solved using Divide and Conquer. Bel
16 min read
Tiling with Dominoes
Given a 3 x n board, find the number of ways to fill it with 2 x 1 dominoes.Example 1: Following are all the 3 possible ways to fill up a 3 x 2 board. Example 2: Here is one possible way of filling a 3 x 8 board. You have to find all the possible ways to do so. Examples : Input : 2 Output : 3 Input : 8 Output : 153 Input : 12 Output : 2131 Defining
5 min read
Secretary Problem (A Optimal Stopping Problem)
The Secretary Problem also known as marriage problem, the sultan's dowry problem, and the best choice problem is an example of Optimal Stopping Problem. This problem can be stated in the following form: Imagine an administrator who wants to hire the best secretary out of n rankable applicants for a position. The applicants are interviewed one by on
12 min read
Transportation Problem | Set 7 ( Degeneracy in Transportation Problem )
Please go through this article first. This article will discuss degeneracy in transportation problem through an explained example. Solution: This problem is balanced transportation problem as total supply is equal to total demand. Initial basic feasible solution: Least Cost Cell Method will be used here to find the initial basic feasible solution.
3 min read
Nuts & Bolts Problem (Lock & Key problem) using Quick Sort
Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently. Constraint: Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means a nut can only be compared with a bolt and a bolt can only be compared with a nut to see which
12 min read
Nuts & Bolts Problem (Lock & Key problem) using Hashmap
Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping between nuts and bolts. Match nuts and bolts efficiently. Constraint: Comparison of a nut to another nut or a bolt to another bolt is not allowed. It means nut can only be compared with bolt and bolt can only be compared with a nut to see which one i
5 min read
Difference between 0/1 Knapsack problem and Fractional Knapsack problem
What is Knapsack Problem?Suppose you have been given a knapsack or bag with a limited weight capacity, and each item has some weight and value. The problem here is that "Which item is to be placed in the knapsack such that the weight limit does not exceed and the total value of the items is as large as possible?". Consider the real-life example. Su
13 min read
8 queen problem
The eight queens problem is the problem of placing eight queens on an 8×8 chessboard such that none of them attack one another (no two are in the same row, column, or diagonal). More generally, the n queens problem places n queens on an n×n chessboard. There are different solutions for the problem. Backtracking | Set 3 (N Queen Problem) Branch and
8 min read
The Great Tree-List Recursion Problem.
Asked by Varun Bhatia. Question: Write a recursive function treeToList(Node root) that takes an ordered binary tree and rearranges the internal pointers to make a circular doubly linked list out of the tree nodes. The”previous” pointers should be stored in the “small” field and the “next” pointers should be stored in the “large” field. The list sho
1 min read