幸运三角形&&http://acm.nyist.net/JudgeOnline/problem.php?pid=491
2012-07-30 09:52
615 查看
经典的dfs题,子集的构造方法为难点。。
AC代码:
AC代码:
#include<iostream> #include<cstdio> #include<string.h> #define N 25 using namespace std; bool num ; int res ; int n,sum,half; void dfs(int cur) { if(cur==n+1){sum++;return;} for(int i=0;i<2;++i)//子集生成:位向量法 { int count=0; num[1][cur]=i;//这里0表示'-',1表示'+'. count=res[1][cur-1]+i;//当前+号的个数为新增加的一个和原来的总数和 for(int j=2;j<=cur;++j) { num[j][cur-j+1]=num[j-1][cur-j+1]^num[j-1][cur-j+2]; if(num[j][cur-j+1]) count++; } res[1][cur]=count; if(count<=half&&(cur+1)*cur/2-count<=half) dfs(cur+1);//剪枝 //当搜索到深度为cur时,生成子集的总个数。 } } int main() { while(~scanf("%d",&n)) { sum=0; memset(res,0,sizeof(res)); int m=(n+1)*n/2; if(m&1){printf("0\n");continue;}//当图形的总数为奇数时,剪枝 half=m/2; dfs(1); printf("%d\n",sum); }return 0; }
相关文章推荐
- 最小点覆盖集&&http://acm.nyist.net/JudgeOnline/problem.php?pid=237
- http://acm.nyist.net/JudgeOnline/problem.php?pid=148&&矩阵求fibonacci数列
- http://acm.nyist.net/JudgeOnline/problem.php?pid=401&&简单的博弈论
- 中缀式变后缀式&&http://acm.nyist.net/JudgeOnline/problem.php?pid=467
- http://acm.nyist.net/JudgeOnline/problem.php?pid=128&&前缀式计算
- SPFA+DP&&http://acm.nyist.net/JudgeOnline/problem.php?pid=203
- http://acm.nyist.net/JudgeOnline/problem.php?pid=420&&p次方求和
- http://acm.nyist.net/JudgeOnline/problem.php?pid=120&&强连通分量
- 导弹拦截&&http://acm.nyist.net/JudgeOnline/problem.php?pid=79
- http://acm.nyist.net/JudgeOnline/problem.php?pid=117&&树状数组求逆序数+离散化
- http://acm.nyist.net/JudgeOnline/problem.php?pid=118&&次小生成树
- http://acm.nyist.net/JudgeOnline/problem.php?pid=32&&DFS
- http://acm.nyist.net/JudgeOnline/problem.php?pid=119&&Rmq
- http://acm.nyist.net/JudgeOnline/problem.php?pid=257&&中缀表达式变后缀表达式
- http://acm.nyist.net/JudgeOnline/problem.php?pid=211&&有向图传递闭包问题
- http://acm.nyist.net/JudgeOnline/problem.php?pid=128&前缀式计算
- http://acm.nyist.net/JudgeOnline/problem.php?pid=115&&Dijkstra城市平叛
- http://acm.nyist.net/JudgeOnline/problem.php?pid=38&&最小生成树
- hdu1159 另附:http://acm.nyist.net/JudgeOnline/problem.php?pid=36(最长公共子序列)
- hdu1257 另附:http://acm.nyist.net/JudgeOnline/problem.php?pid=214(最长上升子序列)