dfs去重(剪枝)
来源:互联网 发布:无线信号探测软件 编辑:程序博客网 时间:2024/04/30 10:24
关于dfs的剪枝问题我之前一直是云里雾里的,最后通过做了几道典型的例题,掌握了一些方法,现在将这些题分享给大家。
zoj—Exchange Cards
Here comes the problem, given the card value he plans to get and the cards he has, Mike wants to fix how many ways he can get it. So it's you task to write a program to figure it out.
Input
The problem consists of multiple test cases, terminated byEOF. There's a blank line between two inputs.
The first line of each test case gives n, the value of the card Mike plans to get andm, the number of different kinds of cards Mike has. n will be an integer number between 1 and 1000.m will be an integer number between 1 and 10.
The next m lines give the information of different kinds of cards Mike have. Each line contains two integers,val and num, representing the value of this kind of card, and the number of this kind of card Mike have.
Note: different kinds of cards will have different value, eachval and num will be an integer greater than zero.
Output
For each test case, output in one line the number of different ways Mike could exchange for the card he wants. You can be sure that the output will fall into an integer value.
Output a blank line between two test cases.
Sample Input
5 22 13 110 510 27 25 32 21 5
Sample Output
17
#include<stdio.h>#include<string.h>int n,m,k;int a[100010];int count;void dfs(int index,int toal){int i,t;if(toal==n){count++;return;}for(i=index;i<k;i++){if(toal+a[i]>n)continue;t=toal+a[i];dfs(i+1,t);while(a[i]==a[i+1]&&i+1<k)i++;//剪枝的操作}}int main(){int x1,y1,i,h;h=0;while(scanf("%d%d",&n,&m)!=EOF){k=0;for(i=0;i<m;i++){scanf("%d%d",&x1,&y1);while(y1--){a[k++]=x1; }}count=0;dfs(0,0);if(h)printf("\n");printf("%d\n",count);h++;}return 0;}
方法二:
#include<stdio.h>int ans,n,k;int a[15][2];void dfs(int card,int toal){int val,i;if(toal==n){ans++;return;}if(toal>n||card>k)return;for(i=card;i<=k;i++){if(a[i][1]>0){val=a[i][0]+toal;a[i][1]--;dfs(i,val);a[i][1]++; }}}int main(){int i,m;m=0;while(scanf("%d%d",&n,&k)!=EOF){ans=0;for(i=1;i<=k;i++){scanf("%d%d",&a[i][0],&a[i][1]);}dfs(1,0);if(m)printf("\n");printf("%d\n",ans);m++;}}
Description
Input
Output
Sample Input
4 6 4 3 2 2 1 1
5 3 2 1 1
400 12 50 50 50 50 50 50 25 25 25 25 25 25
0 0
Sample Output
Sums of 4:
4
3+1
2+2
2+1+1
Sums of 5:
NONE
Sums of 400:
50+50+50+50+50+50+25+25+25+25
50+50+50+50+50+25+25+25+25+25+25
代码如下:/*回溯加剪枝(去重)*/#include<stdio.h>#include<string.h>int n,m;int a[15],vin[15];int z[15];int k;int flag;void dfs(int index,int toal){int i,t,j;if(toal==n){flag=1;if(k==1)printf("%d\n",z[0]);else {for(j=0;j<k-1;j++)printf("%d+",z[j]);printf("%d\n",z[j]); }}for(i=index;i<=m;i++){if(toal+a[i]<=n&&vin[i]==0){//vin[i]=1;t=toal+a[i];z[k]=a[i];k++;dfs(i+1,t);//vin[i]=0;k--;while(i+1<=m&&a[i]==a[i+1])i++;// 例某一个状态 扩展完成后,//若下一个a[i+1]==a[i],则不必再进行dfs(i+1,t)这个状态了,//因为这个状态所扩展出来的结果跟上一个是一样的。因此我们只需要 //找到下一个a[k]!=a[i] 的状态}}}int main(){int i;while(scanf("%d%d",&n,&m),n|m){for(i=1;i<=m;i++)scanf("%d",&a[i]);memset(vin,0,sizeof(vin));k=0;flag=0;printf("Sums of %d:\n",n);dfs(1,0);if(!flag)printf("NONE\n");}return 0;}
- dfs去重(剪枝)
- Sum It Up(DFS+剪枝+去重)
- ZOJ 1711--DFS--组合--剪枝/去重
- USACO ORZ(DFS+set去重)
- DFS(剪枝)_hdu_1010
- Friends(DFS+剪枝)
- hdu5113(dfs+剪枝)
- hdu1010(dfs+剪枝)
- dfs+剪枝(hdu1010)
- 等边三角形(dfs剪枝)
- HDU5937(DFS+剪枝)
- Cubes(DFS+剪枝)
- hdu1010(dfs+剪枝)
- hdu 2610 dfs+判重剪枝
- POJ 1564(DFS+去重)
- zoj1711 dfs搜索+去重
- ZOJ(1711)Sum It Up (DFS+剪枝+去重复)
- POJ 题目1564 Sum It Up(DFS,去重)
- (4.1.1.2)菜单创建函数:onPrepareOptionsMenu 和onCreateOptionsMenu 的区别
- A1044 Shopping in Mars (25)
- Oracle CASE WHEN 用法介绍
- Word Break
- 学习设计模式——单例模式
- dfs去重(剪枝)
- Codeforces Round #284 (Div. 1) C. Array and Operations(网络流‘最大流)
- 大讲台谈hive(后篇二)
- Executor的理解与使用
- zoj 1002 Fire Net【DFS】
- 上传文件
- 【机房收费系统】机房收费系统之动态下机功能
- desc关键字引发的问题
- CPU伪共享