UVA 11125 - Arrange Some Marbles (dp)

Problem H
Arrange Some Marbles
Input: 
Standard Input

Output: Standard Output

 

you are given some marbles of n different color. You have to arrange these marbles in a line. The marbles adjacent with same color form a group. In each group there can be 1 to 3 marble. Adjacent group should have different color and size. The first and last group also should have different color and size. You are given the number of each of these n marbles. You have count the number of ways you can arrange them in a line maintaining the above constraints. For example you have 4 red marbles and 4 green marbles. You can arrange them in the following 8 way  - GGGRRGRR, GGRGGRRR, GGRRRGGR, GRRGGGRR, RGGRRRGG, RRGGGRRG, RRGRRGGG, RRRGGRGG.

 

Input

Input contains multiple number of test cases. The first line contain the number of test cases t (t<3000). Each of the next line contains one test case. Each test case starts with n (1 ≤ n ≤ 4) the number of different color. Next contains n integers. The i'th integer denotes the number of marble of color i. The number of marbles of any color is within the range 0..7 (inclusive). The color of the marbles arenumbered from 1 to n.

 

Output

For each test case output contains one integer in one line denoting the number of ways you can arrange the marbles.

 

Sample Input                          Output for Sample Input

6

2 3 3

2 4 4

2 6 6

3 3 4 5

3 4 5 6

4 2 3 4 5

 

0

8

12

174

1234

1440

 


题意: 给定n种颜色的球,然后每种有一定个数,求这些球摆放成一圈,同种球连续不超过3个且相邻的球的个数不相同的方案数。

思路:dp,由于头尾也不能相同,所以多开2维来存放头尾,然后两维存放当前末尾长度和个数,一维存放剩余球个数,然后进行状态转移记忆化搜索。 注意记忆化的dp数组只要初始化一次,因为状态是通用的。

代码:

#include <stdio.h>
#include <string.h>
const int d8[4] = {1, 8, 64, 512};
int t, n, color[4], dp[4][4][4][4][5000];

int DP(int hc, int hl, int c, int l, int state) {
    if (dp[hc][hl][c][l][state] != -1) 
	return dp[hc][hl][c][l][state];
    int &ans = dp[hc][hl][c][l][state] = 0;
    if (state == 0) {
	if (hc != c && hl != l)
	    return ans = 1;
	else
	    return ans = 0;
    }
    for (int i = 0; i < n; i ++) {
	for (int j = 1; j <= 3 && j <= color[i]; j ++) {
	    if (i != c && j != l) {
		color[i] -= j;
		ans += DP(hc, hl, i, j, state - j * d8[i]);
		color[i] += j;
	    }
	}
    }
    return ans;
}

void init() {
    scanf("%d", &n);
    memset(color, 0, sizeof(color));
    for (int i = 0; i < n; i ++)
	scanf("%d", &color[i]);
}

int solve() {
    int state = 0, ans = 0;
    for (int i = n - 1; i >= 0; i --) {
	state = state * 8 + color[i];
    }
    for (int i = 0; i < n; i ++) {
	for (int j = 1; j <= 3 && j <= color[i]; j ++) {
	    ans += DP(i, j, i, j, state - j * d8[i]); 
	}
    }
    return ans;
}

int main() {
    memset(dp, -1, sizeof(dp));
    scanf("%d", &t);
    while (t --) {
	init(); int sum = 0;
	for (int i = 0; i < 4; i ++)
	    sum += color[i];
	if (!sum) printf("1\n");
	else printf("%d\n", solve());
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值