程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> UVa11125Arrange Some Marbles

UVa11125Arrange Some Marbles

編輯:C++入門知識

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 are numbered 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




這是一道DP記數題:DP[sta][stasize][n1][n2][n3][n4][last][lastsize]

有一個坑點就是當輸入全都是0的時候輸出1。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
ll dp[5][9][9][9][9][9][5][9];
int cnt[6];
int n,sum;
ll dfs(int sta,int stasize,int n1,int n2,int n3,int n4,int last,int lastsize){
    if(n1==0&&n2==0&&n3==0&&n4==0){
        if(last!=sta&&stasize!=lastsize){
            return 1;
        }
    }
    if(dp[sta][stasize][n1][n2][n3][n4][last][lastsize] != -1)
        return dp[sta][stasize][n1][n2][n3][n4][last][lastsize];
    int ans = 0;
    if(last==1){
        for(int i = 1; i <= 3; i++){
            if(n2>=i&&lastsize!=i) ans += dfs(sta,stasize,n1,n2-i,n3,n4,2,i);
            if(n3>=i&&lastsize!=i) ans += dfs(sta,stasize,n1,n2,n3-i,n4,3,i);
            if(n4>=i&&lastsize!=i) ans += dfs(sta,stasize,n1,n2,n3,n4-i,4,i);
        }
    }
    else if(last==2){
        for(int i = 1; i <= 3; i++){
            if(n1>=i&&lastsize!=i) ans += dfs(sta,stasize,n1-i,n2,n3,n4,1,i);
            if(n3>=i&&lastsize!=i) ans += dfs(sta,stasize,n1,n2,n3-i,n4,3,i);
            if(n4>=i&&lastsize!=i) ans += dfs(sta,stasize,n1,n2,n3,n4-i,4,i);
        }
    }
    else if(last==3){
        for(int i = 1; i <= 3; i++){
            if(n1>=i&&lastsize!=i) ans += dfs(sta,stasize,n1-i,n2,n3,n4,1,i);
            if(n2>=i&&lastsize!=i) ans += dfs(sta,stasize,n1,n2-i,n3,n4,2,i);
            if(n4>=i&&lastsize!=i) ans += dfs(sta,stasize,n1,n2,n3,n4-i,4,i);
        }
    }
    else{
        for(int i = 1; i <= 3; i++){
            if(n1>=i&&lastsize!=i) ans += dfs(sta,stasize,n1-i,n2,n3,n4,1,i);
            if(n2>=i&&lastsize!=i) ans += dfs(sta,stasize,n1,n2-i,n3,n4,2,i);
            if(n3>=i&&lastsize!=i) ans += dfs(sta,stasize,n1,n2,n3-i,n4,3,i);
        }
    }
    return dp[sta][stasize][n1][n2][n3][n4][last][lastsize] = ans;

}
int main(){

    int ncase;
    cin >> ncase;
    memset(dp,-1,sizeof dp);
    while(ncase--){
        scanf("%d",&n);
        sum = 0;
        memset(cnt,0,sizeof cnt);
        for(int i = 1; i <= n; i++)
            scanf("%d",&cnt[i]),sum+=cnt[i];
        if(sum == 0) {cout<<1<=j)ans += dfs(1,j,cnt[1]-j,cnt[2],cnt[3],cnt[4],1,j);
            if(i==2)for(int j = 1; j <= 3; j++)  if(cnt[2]>=j)ans += dfs(2,j,cnt[1],cnt[2]-j,cnt[3],cnt[4],2,j);
            if(i==3)for(int j = 1; j <= 3; j++)  if(cnt[3]>=j)ans += dfs(3,j,cnt[1],cnt[2],cnt[3]-j,cnt[4],3,j);
            if(i==4)for(int j = 1; j <= 3; j++)  if(cnt[4]>=j)ans += dfs(4,j,cnt[1],cnt[2],cnt[3],cnt[4]-j,4,j);
        }
        cout<

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved