[UVA][dp] 12030 - Help the Winners@Morris' Blog|PChome Online 人新台
2013-07-05 12:07:40| 人815| 回0 | 上一篇 | 下一篇

[UVA][dp] 12030 - Help the Winners

0 收藏 0 0 站台


  Help the Winners 

The Sell N' Profit shop has recently had a raffle contest with a prize of a dress and matching shoes to some of their regular customers. They have set aside an equal number of dresses and pairs of shoes of different designs for the purpose.

The plan was going quite well until one customer pointed out that not all the dresses and all the pairs of shoes match each other (i.e. you won't look good if you wore that dress and that pair of shoes at the same time - but the same dress or shoes might look good with something else). So, simply giving someone a dress and a pair of shoes at random, risks angering their customers. They have to give each winner a dress and shoes that match. But some pairs of (dress, shoes) are so adorable to girls such that if they are given to any girl, she would be happier than ever and she may become a great customer of the shop. These pairs are called super matching pairs. But, choosing such pair may lead to a situation that some other girls get non-matching (dress, shoes) pairs.

Now you are given n dresses and n pairs of shoes, a list of what dress matches which shoes and some super matching (dress, shoes) pairs. As the company have lack of fashion sense, they ask you to find how many ways there are to make up n sets of dresses and matching shoes, such that either all the girls are happy (got matching dress and shoes), or at least one of the girls got a super matching pair.

Input 

Input starts with an integer T ($ le$100), denoting the number of test cases.

Each case starts with an integer n ( 1$ le$n$ le$15) by itself on a line, denoting the number of dresses and pair of shoes. Each of the next n lines contains n space separated integers (one of 0, 1 or 2). The j-th integer in i-th line represents the status of the i-th dress and j-th pair of shoes. 0 means that i-th dress doesn't match j-th pair of shoes. 1 means they match. 2 means they are super matching.

Output 

For each case, print the case number and number of ways you can form N sets of dresses and shoes.

Sample Input 

2 3 0 1 1 1 1 0 1 0 1 3 1 1 2 2 1 0 1 1 2 

Sample Output 

Case 1: 2 Case 2: 4 




目描述:


要找到策略都有得匹配,或者其中一得到 super 匹配(而其他有匹配也)。
匹配的方法。

目解法:


全部匹配最多答案是 "15!"

由於 n <= 15, 可以使用(bitmask)。
依序匹配,且根剩可使用的,的方法。
匹配到重覆的,便可以直接回。

#include <stdio.h>
#include <string.h>
long long dp[1<<15][2][2];
int n, g[16][16];
long long dfs(int idx, int state, int super, int m) {
    if(idx == n) {
        if(super || m)  return 1;
        return 0;
    }
    long long &val = dp[state][super][m];
    if(val != -1) return val;
    val = 0;
    int i, tm, ts;
    for(i = 0; i < n; i++) {
        if(((state>>i)&1) == 0) {
            ts = g[idx][i] == 2 || super;
            tm = g[idx][i]&m;
            val += dfs(idx+1, state|(1<<i), ts, tm);
        }
    }
    return val;
}
int main() {
    int testcase, cases = 0;
    int i, j;
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d", &n);
        for(i = 0; i < n; i++)
            for(j = 0; j < n; j++)
                scanf("%d", &g[i][j]);
        memset(dp, -1, sizeof(dp));
        printf("Case %d: %lld\n", ++cases, dfs(0, 0, 0, 1));
    }
    return 0;
}

台: Morris
人(815) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA] 12356 - Army Buddies
此分上一篇:[UVA] 12085 - Mobile Casanova

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86