- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count the number of ways to tile the floor of size n x m using 1 x m size tiles in C++
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
Input
n=3 m=2
Output
Count the number of ways to tile the floor of size n x m using 1 x m size tiles are: 3
Explanation
The ways will be three 1x2 tiles arranged as shown below −
Input
n=3 m=3
Output
Count the number of ways to tile the floor of size n x m using 1 x m size tiles are: 2
Explanation
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.
Example
#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; }
Output
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
- Related Articles
- A room measures ( 4.8 mathrm{m} ) and ( 5.04 mathrm{m} ). Find the size of the longest square tile that can be used to tile the floor without cutting any tile.
- Find the number of rectangles of size 2x1 which can be placed inside a rectangle of size n x m in Python
- A Mason lays square tiles on a wall 8 m by 12 m. Find the largest size of square tile, that he can fit in the wall without cutting a whole tile.
- If ( a=x^{m+n} y^{l}, b=x^{n+l} y^{m} ) and ( c=x^{l+m} y^{n} ), prove that ( a^{m-n} b^{n-1} c^{l-m}=1 . )
- Find the numerical value of ( P: Q ) where ( mathrm{P}=left(frac{x^{m}}{x^{n}}right)^{m+n-l} timesleft(frac{x^{n}}{x^{l}}right)^{n+l-m} timesleft(frac{x^{l}}{x^{m}}right)^{l+m-n} ) and( mathrm{Q}=left(x^{1 /(a-b)}right)^{1 /(a-c)} timesleft(x^{1 /(b-c)}right)^{1 /(b-a)} timesleft(x^{1 /(c-a)}right)^{1 /(c-b)} )where ( a, b, c ) being all different.A. ( 1: 2 )B. ( 2: 1 )C. ( 1: 1 )D. None of these
- If ( x=a^{m+n}, y=a^{n+1} ) and ( z=a^{l+m} ), prove that ( x^{m} y^{n} z^{l}=x^{n} y^{l} z^{m} )
- Program to count number of ways we can fill 3 x n box with 2 x 1 dominos in Python
- Find the Number of Solutions of n = x + n x using C++
- Simplify:( sqrt[lm]{frac{x^{l}}{x^{m}}} times sqrt[m n]{frac{x^{m}}{x^{n}}} times sqrt[n l]{frac{x^{n}}{x^{l}}} )
- If the magnification of a body of size 1 m is 2, what is the size of the image?
- Program to find latest group of size M using Python
- A mason has to fit a bathroom with square marble tiles of the largest possible size. The size of the bathroom is 10ft. by 8ft. What would be the size in inches of the tile required that has to be cut and how many such tiles are required?
- Solve the following quadratic equation by factorization: $frac{m}{n}x^2+frac{n}{m}=1-2x$
- Add the following monomials:(i) ( 8 x y, 2 x y, 9 x y )( left(iv{}right)-40 m n,-30 m n, 18 m n )
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N in C++
