73th LeetCode Weekly Contest Domino and Tromino Tiling

We have two types of tiles: a 2x1 domino shape, and an "L" tromino shape. These shapes may be rotated.

XX  <- domino

XX  <- "L" tromino
X

Given N, how many ways are there to tile a 2 x N board? Return your answer modulo 10^9 + 7.

(In a tiling, every square must be covered by a tile. Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings has both squares occupied by a tile.)

Example:
Input: 3
Output: 5
Explanation: 
The five different ways are listed below, different letters indicates different tiles:
XYZ XXZ XYY XXY XYY
XYZ YYZ XZZ XYY XXY

Note:

  • N  will be in range [1, 1000].

一块区域有--,L(上下两种位置),问2*N有几种放置方式。

首先

x 这种我们叫0

x

x这种我们叫1

xx

xx这种我们叫2

x

xxx是怎么得到的呢。一种是x+横着和竖着  xx+竖着 xx+L形(上下两种)

xxx                                      x                     xx          x

于是...dp[i][0]=dp[i-2][0]+dp[i-1][0]+dp[i-1][1]+dp[i-1][2] i表示列数啦

xxxx是怎么得到的呢。xx+L   xx+ --

xxx             xx    xxx

dp[i][1]=dp[i-2][0]+dp[i-1][2]

xxx同理

xxxx

复制代码
 1 class Solution {
 2 public:
 3     const int mod = 1000000007;
 4     int dp[2000][3];
 5     int numTilings(int N) {
 6         dp[1][0]=1,dp[0][0]=1;
 7         for(int i=2;i<=N;i++){
 8             dp[i][0]=(dp[i-2][0]%mod+dp[i-1][0]%mod)%mod+(dp[i-1][1]%mod+dp[i-1][2]%mod)%mod;
 9             dp[i][1]=(dp[i-2][0]%mod+dp[i-1][2]%mod)%mod;
10             dp[i][2]=(dp[i-2][0]%mod+dp[i-1][1]%mod)%mod;
11         }
12         return dp[N][0]%mod;
13     }
14 };
复制代码

 

posted @   樱花落舞  阅读(371)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示