着色方案——dp

博客探讨了使用动态规划解决着色方案的问题,重点在于计算不同次数的颜色使用方案,而非关注颜色种类。
摘要由CSDN通过智能技术生成

考虑不同次数有多少个
不要考虑不同颜色有多少个

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int cnt[6];// 表示 可以使用i次的 颜色有多少个
ll vis[16][16][16][16][16][16];
int k;
const int mod = 1e9+7;

ll dfs(int t1, int t2, int t3, int t4, int t5, int last)
{
    ll ans = 0;
    if(t1+t2+t3+t4+t5 == 0)return 1;

    if(vis[t1][t2][t3][t4][t5][last]!= 0)return vis[t1][t2][t3][t4][t5][last];

    if(t1)//如果使用的1次的还存在
    {
        ans+=(t1- (last == 2))*dfs(t1-1, t2, t3, t4, t5, 1);/******last == 2 的 作用: 如果上次是使用2次的,这次就变成了使用1次的了, 因为不能连续图相同的颜色,所以那个1次要减去******************/
        ans%=mod;
    }

    if(t2)
    {
        ans+=(t2 - (last == 3))*dfs(t1+1, t2-1, t3 ,t4, t5, 2);// 为甚么 要+1 -1, 这次使用颜色是2 次的 , 所以 那个颜色在下一个状态 只能使用一次了, 所以要+1, -1. 
        ans%=mod;
    }

    if(t3)
    {
        ans+=(t3- (last == 4))*dfs(t1,  t2+1, t3-1, t4, t5, 3);
        ans%=mod;
    }

    if(t4)
    {
        ans+=(t4- (last == 5))*dfs(t1, t2, t3+1, t4-1, t5, 4);
        ans%=mod;
    }

    if(t5)
    {
        ans+=t5*dfs(t1, t2, t3, t4+1, t5-1, 5);
        ans%=mod;
    }


    return vis[t1][t2][t3][t4][t5][last] =ans;
}
int main()
{

    memset(cnt, 0, sizeof(cnt));
    memset(vis, 0, sizeof(vis));
    scanf("%d", &k);

    for(int i = 0;i < k;i++)
    {
        int c;
        scanf("%d", &c);
        cnt[c]++;
    }

    ll ans = dfs(cnt[1], cnt[2], cnt[3], cnt[4], cnt[5], 0);

    printf("%lld", ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值