为了花儿能够茁壮成长,每一朵花的上下左右四个方向不能有其他的花。问有多少种种花的方案。
为防止答案过大,答案对1e9+7取模。
# (750)# 牛牛爱花 # @param n int整型 列数 (2215)# @return int整型 # class Solution: def solve(self , n ): (2216)# write code here if n < 1: return n MOD = int(1e9+7) dp = [[0]*8 for _ in range(2)] dp[0] = [1,1,1,0,1,1,0,0] cur,pre = 0,1 for i in range(n-1): cur,pre = pre,cur dp[cur] = [0]*8 for j in range(8): if j==3&nbs***bsp;j == 6&nbs***bsp;j == 7: continue # 枚举上一列的所有状态 for k in range(8): (2217)# k&j判断这两列的插花是否相邻 if k == 3&nbs***bsp;k == 6&nbs***bsp;k == 7&nbs***bsp;k&j : continue dp[cur][j] = (dp[cur][j]+dp[pre][k])%MOD return sum(dp[cur])%MOD-1
class Solution { public: /** * 牛牛爱花 * @param n int整型 列数 * @return int整型 */ // 000 001 010 100 101 int solve(int n) { int M = 1e9+7; long dp[5]={1, 1, 1, 1, 1}, a[5]; for(int i=2;i<=n;i++){ a[0] = (dp[0]+dp[1]+dp[2]+dp[3]+dp[4])%M; // 000: a[1] = (dp[0]+dp[2]+dp[3])%M; //001 a[2] = (dp[0]+dp[1]+dp[3]+dp[4])%M; // 010 a[3] = (dp[0]+dp[1]+dp[2])%M; // 100 a[4] = (dp[0]+dp[2])%M; // 101 for(int j=0;j<5;j++) dp[j] = a[j]; } return (dp[0]+dp[1]+dp[2]+dp[3]+dp[4]-1)%M; } };
public int solve(int n) { long[][] dp = new long[n][1<<3]; int mod = (int) (1e9 + 7); int[] vaild = new int[]{0, 1, 2, 4, 5}; for (int v : vaild) dp[0][v] = 1; for (int i = 1; i < n; i++) for (int v : vaild) for (int k : vaild) if ((v & k) == 0) dp[i][v] = (dp[i][v] + dp[i - 1][k]) % mod; long sum = 0; for (int i = 0; i < 8; i++) sum = (sum + dp[n - 1][i]) % mod; return (int) (sum - 1); }
/**每一列有五种状态,000,100,010,001,101,每一种状态前面的状态都是固定的,比如说100只能由000、010、001 *三种状态获得,这实际上就是一个状态机,因此递推可得答案。注意要减去所有列都为0的状态 */ int solve(int n) { int mod = 1e9+7; vector<vector<long long> > dp(5,vector<long long>(n+1,1)); for(int i=2;i<=n;i++){ dp[0][i] = (dp[0][i-1]+dp[1][i-1]+dp[2][i-1]+dp[3][i-1]+dp[4][i-1])%mod; dp[1][i] = (dp[0][i-1]+dp[2][i-1]+dp[3][i-1])%mod; dp[2][i] = (dp[0][i-1]+dp[1][i-1]+dp[3][i-1]+dp[4][i-1])%mod; dp[3][i] = (dp[0][i-1]+dp[1][i-1]+dp[2][i-1])%mod; dp[4][i] = (dp[0][i-1]+dp[2][i-1])%mod; } return (dp[0][n]+dp[1][n]+dp[2][n]+dp[3][n]+dp[4][n]-1)%mod; }
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
class Solution { public: int solve(int n) { int MOD = 1e9+7; vector<vector<long> > dp(5, vector<long>(n,0)); for(int i = 0; i < 5; i++){ dp[i][0] = 1; } for(int i = 1; i < n; i++){ dp[0][i] = (dp[0][i - 1] + dp[1][i - 1] + dp[2][i - 1] + dp[3][i - 1] + dp[4][i - 1]) % MOD; //000 dp[1][i] = (dp[0][i - 1] + dp[2][i - 1] + dp[3][i - 1]) % MOD; //001 dp[2][i] = (dp[0][i - 1] + dp[1][i - 1] + dp[3][i - 1] + dp[4][i - 1]) % MOD; //010 dp[3][i] = (dp[0][i - 1] + dp[1][i - 1] + dp[2][i - 1]) % MOD; //100 dp[4][i] = (dp[0][i - 1] + dp[2][i - 1]) % MOD; //101 } return (dp[0][n - 1] + dp[1][n - 1] + dp[2][n - 1] + dp[3][n - 1] + dp[4][n - 1] - 1) % MOD; } };
class Solution { public: int res=0; long long m=1e9+7; vector<vector<long long>>dp(n,vector<long long>(5,0)); dp[0][0]=dp[0][1]=dp[0][2]=dp[0][3]=dp[0][4]=1; for(int i=1;i<n;i++) { dp[i][0]=(dp[i-1][0]+dp[i-1][1]+dp[i-1][2]+dp[i-1][3]+dp[i-1][4])%m; dp[i][1]=(dp[i-1][0]+dp[i-1][2]+dp[i-1][3])%m; dp[i][2]=(dp[i-1][0]+dp[i-1][1]+dp[i-1][1]+dp[i-1][4])%m; dp[i][3]=(dp[i-1][0]+dp[i-1][1]+dp[i-1][2])%m; dp[i][4]=(dp[i-1][0]+dp[i-1][2])%m; } res=(dp[n-1][0]+dp[n-1][1]+dp[n-1][2]+dp[n-1][3]+dp[n-1][4])%m; return res-1; } };
public int solve (int n) { // write code here int[][] dp=new int[n][8]; for (int i = 0; i < 7; i++) { if(i!=3&&i!=6&&i!=7){ dp[0][i]=1; } } for (int i = 1; i < n; i++) { for (int j = 0; j < 7; j++) { if(j==3||j==6||j==7){ continue; } for (int k = 0; k < 7; k++) { int x=j&k; if(k==3||k==6||k==7||x!=0){ continue; } dp[i][j]=(dp[i][j]+dp[i-1][k])%((int)Math.pow(10,9)+7); } } } int res=-1; for (int i = 0; i < 7; i++) { if(i!=3&&i!=6&&i!=7){ res=(res+dp[n-1][i])%((int)Math.pow(10,9)+7); } } return res; }