CUGOJ 1437 Take Photo
2014-01-20 10:30
295 查看
题目
tell me the number of the legal permutation.
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.
0 1
1 0
3
0 0 0
0 0 0
0 0 0
3
0 0 0
1 0 0
0 1 0
6
1
状态dp,类似于棋盘问题,加上一些限制,状态i中1的个数表示已经安排了几个人,并且放在了哪些行,
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, pleasetell 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
20 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
06
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; }
相关文章推荐
- From Galileo to Google: How Big Data Illuminates Human Culture
- Django学习系列(一)--安装Django
- BinarySearchAlgorithm
- 购买可穿戴设备将是人的本能
- Django:关于ForeignKey需要default specified问题的解决方案
- 英雄会(csdn pongo)题解之朋友的礼物
- HDU 4775 Infinite Go
- GoTrace快递查询
- 英雄会(csdn pongo)题解之平衡二叉树
- 单例模式--GOF的23个之一
- Google三大经典论文
- 关于swicth和goto之后的大括号
- POJ 2195 Going Home 最小费用最大流
- zoj 3742 Bellywhite's Algorithm Homework
- 【资料】Ford-Fulkerson Algorithm for Maximum Flow Problem
- compiler,gold parser,DFA,LALR
- 我的django 环境变量简单设置
- #legoo内核# 准则四:使用配置提高杠杆效应
- 理解 django.contrib.staticfiles(collectstatic)
- ORA-01017 invalid username/password;logon denied" (密码丢失解决方案)