You are given an infinite supply of bricks. Each brick has one of two possible lengths: 2 units or 3 units.
Your task is to build a wall of a specified width and height.
The wall must be made up of horizontal rows of bricks. However, the bricks must be arranged so that the right edge of a brick in one row does not align vertically with the right edge of a brick in the row directly above or below it. This is to ensure the wall’s stability.
Write a Python function that takes two arguments: the width and the height of the wall. The function should return the count of all possible ways to build the wall that meet the criteria.
For example, if the width is 5 and the height is 1, there are 2 possible walls: (2,3) and (3,2). A wall with identical row patterns stacked on top of each other is not allowed.
If it is not possible to build the wall with the given width and height, return 0.
这道题本质上是“按行生成 + 行间兼容性约束”的计数问题。先枚举所有能拼出指定宽度的单行方案,每一行只由长度为 2 或 3 的砖组成,并记录这一行内部所有砖缝的位置。接着用动态规划统计高度为 H 时的所有堆叠方式:如果两行在同一列出现砖缝,就不满足稳定性要求,因此只能让相邻两行的砖缝集合没有交集。通常会先把每一种行形态抽象成状态,再用状态之间是否冲突来转移;如果宽度本身无法被 2 和 3 的组合凑出来,则答案为 0。这个思路适合用 DFS/ 回溯生成状态,再配合 DP 做层间计数。