url: https://www.luogu.com.cn/problem/P2476
tag:
SCOI2008,动态规划,搜索,记忆化搜索
思路:
开一个6维的数组来表示状态(因为每种情况有多种,所以不能用压位bit),分别表示足够涂1, 2, 3, 4, 5块木头的颜色有几种,最后一维表示上一种颜色的种类。然后用记忆化搜索来做。参考: https://www.luogu.com.cn/article/fhusu9wq
代码:
#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
ll MOD = 1e9 + 7;
int n, t[6];
ll dp[16][16][16][16][16][6];
ll dfs(int a, int b, int c, int d, int e, int last)
{
if (dp[a][b][c][d][e][last] != -1) return dp[a][b][c][d][e][last];
if (a + b + c + d + e == 0) return 1;
ll res = 0;
if (a) res += (a - (last == 2)) * dfs(a - 1, b, c, d, e, 1);
if (b) res += (b - (last == 3)) * dfs(a + 1, b - 1, c, d, e, 2);
if (c) res += (c - (last == 4)) * dfs(a, b + 1, c - 1, d, e, 3);
if (d) res += (d - (last == 5)) * dfs(a, b, c + 1, d - 1, e, 4);
if (e) res += e * dfs(a, b, c, d + 1, e - 1, 5);
dp[a][b][c][d][e][last] = res % MOD;
return dp[a][b][c][d][e][last];
}
int main()
{
cin >> n;
for (int i = 0; i < n; i ++)
{
int a;
cin >> a;
t[a] ++;
}
memset(dp, -1, sizeof dp);
ll ans = dfs(t[1], t[2], t[3], t[4], t[5], 0);
cout << ans << endl;
return 0;
}