CUGOJ 1437 Take Photo

本文探讨了一种利用动态规划解决排列组合问题的方法,特别关注在给定限制条件下计算合法排列的数量。通过实例分析,展示了如何通过状态转移方程解决实际问题,为读者提供了深入理解动态规划在解决此类问题上的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

Description

There are n people standing in a line to take photo. But for some reasons ,there are some limits on their positions. For example, i must stand on j’s right, marked for (i, j). Now, giving you the n people and those limits, please tell me the number of the legal permutation.

Input

There are multiple test cases. For each test:
The first line contains one positive integer n (1<=n<=16), indicating the number of people. Then following there are n lines, each line containing n integers. The element at ith row and jth column is either 1 or 0; if the element is 1, it represents the limit (i, j); You can assume that any pair of (i, j) is bound to be 0 if i is equal to j.
Processed to the end of file.

Output

 For each test case, output the number of legal permutation on one line.

Sample Input

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

Sample Output

0
6
1


状态dp,类似于棋盘问题,加上一些限制,状态i中1的个数表示已经安排了几个人,并且放在了哪些行,


// http://acm.cug.edu.cn/JudgeOnline/problem.php?id=1437

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
ll dp[1<<16];
int n;
int map[17][17];
int num1[17],num2[17],num3[17],num4[17];
inline int getone(int i)
{
    int cnt=0;
    while(i) cnt+=i%2,i>>=1;
    return cnt;
}
int main()
{
    while(~scanf("%d",&n))
    {
        memset(num1,0,sizeof(num1));
        memset(num3,0,sizeof(num3));
        memset(num4,0,sizeof(num4));
        memset(num2,0,sizeof(num2));
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
            {
                scanf("%d",&map[i][j]);
            }
        int tag=1;
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                if(i!=j)
                {
                    if(map[i][j]&&map[j][i]) tag=0;break;
                }
        if(!tag)
        {
            puts("0");
            continue;
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<i;j++)
            {
                if(map[j][i]) num1[i]++;//在0~i人中有多少个人必须在i的右边
                if(map[i][j]) num2[i]++;//在0~i人中有多少个人必须在i的左边
            }
        }
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                if(map[j][i]) num3[i]++;//在i+1~n-1人中。。。。。
                if(map[i][j]) num4[i]++;//。。。。
            }
        }
        int all=(1<<n)-1;
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        for(int i=1;i<=all;i++)
        {
            int rr=getone(i)-1;
            ll ans=0;
            for(int j=0;j<n;j++)
            {
                if(i&(1<<j))
                {
                    int l=0,r=0,ll=0,rrr=0;
                    for(int k=0;k<j;k++)
                        (i&(1<<k))?l++:ll++;
                    for(int k=j+1;k<n;k++)
                        (i&(1<<k))?r++:rrr++;
                    if(r<num1[rr]) continue;
                    if(l<num2[rr]) continue;
                    if(rrr<num3[rr]) continue;
                    if(ll<num4[rr]) continue;
                    ans+=dp[i&(~(1<<j))];
                }
            }
            dp[i]=ans;
        }
        printf("%lld\n",dp[all]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值