Given a floor of size n x m and tiles of size 1 x m. The problem is to count the number of ways to tile the given floor using 1 x m tiles. A tile can either be placed horizontally or vertically.
Both n and m are positive integers and 2 < = m.
Examples:
Input : n = 2, m = 3
Output : 1
Only one combination to place
two tiles of size 1 x 3 horizontally
on the floor of size 2 x 3.
Input : n = 4, m = 4
Output : 2
1st combination:
All tiles are placed horizontally
2nd combination:
All tiles are placed vertically.
This problem is mainly a more generalized approach to the Tiling Problem.
Approach: For a given value of n and m, the number of ways to tile the floor can be obtained from the following relation.
| 1, 1 < = n < m
count(n) = | 2, n = m
| count(n-1) + count(n-m), m < n
C++
#include <bits/stdc++.h>
using namespace std;
int countWays( int n, int m)
{
int count[n + 1];
count[0] = 0;
for ( int i = 1; i <= n; i++) {
if (i > m)
count[i] = count[i - 1] + count[i - m];
else if (i < m || i == 1)
count[i] = 1;
else
count[i] = 2;
}
return count[n];
}
int main()
{
int n = 7, m = 4;
cout << "Number of ways = "
<< countWays(n, m);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int countWays( int n, int m)
{
int count[] = new int [n + 1 ];
count[ 0 ] = 0 ;
int i;
for (i = 1 ; i <= n; i++) {
if (i > m)
count[i] = count[i - 1 ] + count[i - m];
else if (i < m || i == 1 )
count[i] = 1 ;
else
count[i] = 2 ;
}
return count[n];
}
public static void main(String[] args)
{
int n = 7 ;
int m = 4 ;
System.out.println( "Number of ways = "
+ countWays(n, m));
}
}
|
Python3
def countWays(n, m):
count = []
for i in range (n + 2 ):
count.append( 0 )
count[ 0 ] = 0
for i in range ( 1 , n + 1 ):
if (i > m):
count[i] = count[i - 1 ] + count[i - m]
elif (i < m or i = = 1 ):
count[i] = 1
else :
count[i] = 2
return count[n]
n = 7
m = 4
print ( "Number of ways = " , countWays(n, m))
|
C#
using System;
class GFG {
static int countWays( int n, int m)
{
int [] count = new int [n + 1];
count[0] = 0;
int i;
for (i = 1; i <= n; i++) {
if (i > m)
count[i] = count[i - 1]
+ count[i - m];
else if (i < m || i == 1)
count[i] = 1;
else
count[i] = 2;
}
return count[n];
}
public static void Main()
{
int n = 7;
int m = 4;
Console.Write( "Number of ways = "
+ countWays(n, m));
}
}
|
PHP
<?php
function countWays( $n , $m )
{
$count [0] = 0;
for ( $i = 1; $i <= $n ; $i ++)
{
if ( $i > $m )
$count [ $i ] = $count [ $i - 1] +
$count [ $i - $m ];
else if ( $i < $m or $i == 1)
$count [ $i ] = 1;
else
$count [ $i ] = 2;
}
return $count [ $n ];
}
$n = 7;
$m = 4;
echo "Number of ways = " , countWays( $n , $m );
?>
|
Javascript
<script>
function countWays(n, m)
{
let count = new Array(n + 1);
count[0] = 0;
let i;
for (i = 1; i <= n; i++) {
if (i > m)
count[i] = count[i - 1] + count[i - m];
else if (i < m || i == 1)
count[i] = 1;
else
count[i] = 2;
}
return count[n];
}
let n = 7;
let m = 4;
document.write( "Number of ways = " + countWays(n, m));
</script>
|
Output:
Number of ways = 5
Time Complexity: O(n)
Auxiliary Space: O(n)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
22 Apr, 2022
Like Article
Save Article