给定一个整数N ,任务是平铺尺寸为N * 1的板,任务是计算使用尺寸为1 * 1和2 * 1的砖平铺板的方法的数量。
例子:
Input: N = 2
Output: 5
Explanation:
Tile the board of size 2 x 1 by placing 1 x 1 tile in a Horizontal way and another 1 x 1 tile in a vertical way.
Tile the board of size 2 x 1 by placing 1 x 1 tile in a vertical way and another 1 x 1 tile in a Horizontal way.
Tile the board of size 2 x 1 by placing 1 x 1 tile in a Horizontal way and another 1 x 1 tile in a Horizontal way.
Tile the board of size 2 x 1 by placing 1 x 1 tile in a vertical way and another 1 x 1 tile in a vertical way.
Tile the board of size 2 x 1 by placing 2 x 1 tile in a horizontal way.
Therefore, the required output is 5.
Input: N = 1
Output: 2
方法:可以使用动态编程解决问题。想法是将N x 1板划分为较小的板,然后计算平铺较小的板的方法。最后,打印铺装N x 1板的方法总数。请按照以下步骤解决问题:
- 以下是放置第一个图块的三种方法:
- 垂直放置1 x 1瓷砖。
- 将1 x 1瓷砖水平放置。
- 将2 x 1瓷砖水平放置。
- 因此,解决该问题的递归关系如下:
cntWays(N) = 2 * cntWays(N – 1) + cntWays(N – 2)
- 初始化一个数组dp [] ,其中dp [i]存储平铺ix 1板的方式的数量。
- 迭代范围[2,N],并使用制表法填充数组dp [] 。
- 最后,打印dp [N]的值。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to count the ways to tile N * 1
// board using 1 * 1 and 2 * 1 tiles
long countWaysToTileBoard(long N)
{
// dp[i]: Stores count of ways to tile
// i * 1 board using given tiles
long dp[N + 1];
// Base Case
dp[0] = 1;
dp[1] = 2;
// Iterate over the range [2, N]
for (int i = 2; i <= N; i++) {
// Fill dp[i] using
// the recurrence relation
dp[i] = (2 * dp[i - 1]
+ dp[i - 2] );
}
cout << dp[N];
}
// Driver Code
int main()
{
// Given N
long N = 2;
// Function Call
countWaysToTileBoard(N);
return 0;
}
Java
// Java program to implement
// the above approach
class GFG{
// Function to count the ways to tile N * 1
// board using 1 * 1 and 2 * 1 tiles
static void countWaysToTileBoard(int N)
{
// dp[i]: Stores count of ways to tile
// i * 1 board using given tiles
int dp[] = new int[N + 1];
// Base Case
dp[0] = 1;
dp[1] = 2;
// Iterate over the range [2, N]
for(int i = 2; i <= N; i++)
{
// Fill dp[i] using
// the recurrence relation
dp[i] = (2 * dp[i - 1] +
dp[i - 2]);
}
System.out.print(dp[N]);
}
// Driver Code
public static void main (String[] args)
{
// Given N
int N = 2;
// Function Call
countWaysToTileBoard(N);
}
}
// This code is contributed by AnkThon
Python3
# Python3 program to implement
# the above approach
# Function to count the ways to tile N * 1
# board using 1 * 1 and 2 * 1 tiles
def countWaysToTileBoard( N) :
# dp[i]: Stores count of ways to tile
# i * 1 board using given tiles
dp = [0]*(N + 1)
# Base Case
dp[0] = 1
dp[1] = 2
# Iterate over the range [2, N]
for i in range(2, N + 1) :
# Fill dp[i] using
# the recurrence relation
dp[i] = (2 * dp[i - 1] + dp[i - 2] )
print(dp[N])
# Driver Code
if __name__ == "__main__" :
# Given N
N = 2
# Function Call
countWaysToTileBoard(N)
# This code is contributed by jana_sayantan
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to count the ways to tile N * 1
// board using 1 * 1 and 2 * 1 tiles
static void countWaysToTileBoard(int N)
{
// dp[i]: Stores count of ways to tile
// i * 1 board using given tiles
int []dp = new int[N + 1];
// Base Case
dp[0] = 1;
dp[1] = 2;
// Iterate over the range [2, N]
for(int i = 2; i <= N; i++)
{
// Fill dp[i] using
// the recurrence relation
dp[i] = (2 * dp[i - 1] +
dp[i - 2]);
}
Console.Write(dp[N]);
}
// Driver Code
public static void Main(String[] args)
{
// Given N
int N = 2;
// Function Call
countWaysToTileBoard(N);
}
}
// This code is contributed by shikhasingrajput
5
时间复杂度: O(N)
辅助空间: O(N)