为了花儿能够茁壮成长,每一朵花的上下左右四个方向不能有其他的花。问有多少种种花的方案。
为防止答案过大,答案对1e9+7取模。
class Solution: def solve(self , n ): # write code here mod = 10 ** 9 + 7 dp = [[0 for i in range(1 << 3)] for i in range(n)] valid = {0,1,2,4,5} # 0表示不种,1表示种 for i in range(n): for state in range(1 << 3): # 判断当前是否是合法状态 if state in valid: if i == 0: dp[i][state] = 1 continue for last_state in range(1 << 3): if last_state in valid and last_state & state == 0: # 枚举是否合法 dp[i][state] += dp[i - 1][last_state] % mod return sum(dp[-1]) % mod - 1