- Related Questions & Answers
- Find the number of rectangles of size 2x1 which can be placed inside a rectangle of size n x m in Python
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n in C++
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N in C++
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors in C++
- Calculate the value of (m)1/n in JavaScript
- Program to count number of ways we can fill 3 x n box with 2 x 1 dominos in Python
- Program to find sum of 1 + x/2! + x^2/3! +…+x^n/(n+1)! in C++
- Program to find latest group of size M using Python
- Count of values of x <= n for which (n XOR x) = (n – x) in C++
- Construct a frequency array of digits of the values obtained from x^1, x^2, ....., x^n in C++
- Max sum of M non-overlapping subarrays of size K in C++
- Count number of paths whose weight is exactly X and has at-least one edge of weight M in C++
- Find the minimum number of steps to reach M from N in C++
- Sum of even numbers from n to m regardless if n<m or n>m JavaScript
- Convert a number m to n using minimum number of given operations in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Given two numbers n and m representing the length and breadth of the floor of a room. The goal is to count the number of ways in which this floor can be tiled using the tiles of size 1Xm.

**For Example**

n=3 m=2

Count the number of ways to tile the floor of size n x m using 1 x m size tiles are: 3

The ways will be three 1x2 tiles arranged as shown below −

n=3 m=3

Count the number of ways to tile the floor of size n x m using 1 x m size tiles are: 2

The ways will be three 1x3 tiles arranged vertically and horizontally. Only two ways.

**Approach used in the below program is as follows** −

In this approach we check the value of n. If n is less than m and has value 1 then there will be only one tile to be placed all over the floor of size 1Xm.

If n is equal to m then there will be 2 ways, placing n 1xm tiles all vertically and all horizontally. If n is greater than m then we will calculate ways using previous ways − ways[n−1]+ways[m−1].

Take integers m and n for the dimensions of the floor and tiles.

Function ways_tile_floor(int N, int M) takes the dimensions and returns the number of ways to tile the floor of size n x m using 1 x m size tiles.

Take an array arr[ ] of length N+1 to store count of tiles for index i=current value of N.

For arr[0] tiles will be 0. So arr[0]=0.

Traverse arr[] using for loop from i=1 to i=N. For each arr[i], if i>M then calculate count using previous values. arr[i]=arr[i − 1] + arr[i − M].

In case i=1 and or i<M, set arr[i] with 1.

In case i=M, set arr[i]=2.

At the end of the for loop, arr[N] will have a count of ways of arranging tiles.

Return arr[N] as result.

#include <bits/stdc++.h> using namespace std; int ways_tile_floor(int N, int M){ int arr[N + 1]; arr[0] = 0; for (int i = 1; i <= N; i++){ if (i > M){ arr[i] = arr[i − 1] + arr[i − M]; } else if (i < M || i == 1){ arr[i] = 1; } else { arr[i] = 2; } } return arr[N]; } int main(){ int n = 3, m = 2; cout<<"Count the number of ways to tile the floor of size n x m using 1 x m size tiles are: "<<ways_tile_floor(n, m); return 0; }

If we run the above code it will generate the following output −

Count the number of ways to tile the floor of size n x m using 1 x m size tiles are: 3

Advertisements