dfs去重(剪枝)

来源:互联网 发布:无线信号探测软件 编辑:程序博客网 时间:2024/04/30 10:24

关于dfs的剪枝问题我之前一直是云里雾里的,最后通过做了几道典型的例题,掌握了一些方法,现在将这些题分享给大家。

zoj—Exchange Cards

Problem Description
As a basketball fan, Mike is also fond of collecting basketball player cards. But as a student, he can not always get the money to buy new cards, so sometimes he will exchange with his friends for cards he likes. Of course, different cards have different value, and Mike must use cards he owns to get the new one. For example, to get a card of value 10$, he can use two 5$ cards or three 3$ cards plus one 1$ card, depending on the kinds of cards he have and the number of each kind of card. And Sometimes he will involve unfortunately in a bad condition that he has not got the exact value of the card he is looking for (fans always exchange cards for equivalent value).

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++;}}
(通过构造不同的图,可以选择不同的方法。)
poj-----Sums it up

Description

Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t = 4, n = 6, and the list is [4, 3, 2, 2, 1, 1], then there are four different sums that equal 4: 4, 3+1, 2+2, and 2+1+1. (A number can be used within a sum as many times as it appears in the list, and a single number counts as a sum.) Your job is to solve this problem in general.

Input

The input will contain one or more test cases, one per line. Each test case contains t, the total, followed by n, the number of integers in the list, followed by n integers x 1 , . . . , x n . If n = 0 it signals the end of the input; otherwise, t will be a positive integer less than 1000, n will be an integer between 1 and 12 (inclusive), and x 1 , . . . , x n will be positive integers less than 100. All numbers will be separated by exactly one space. The numbers in each list appear in nonincreasing order, and there may be repetitions.

Output

For each test case, first output a line containing `Sums of', the total, and a colon. Then output each sum, one per line; if there are no sums, output the line `NONE'. The numbers within each sum must appear in nonincreasing order. A number may be repeated in the sum as many times as it was repeated in the original list. The sums themselves must be sorted in decreasing order based on the numbers appearing in the sum. In other words, the sums must be sorted by their first number; sums with the same first number must be sorted by their second number; sums with the same first two numbers must be sorted by their third number; and so on. Within each test case, all sums must be distinct; the same sum cannot appear twice.

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;}

0 0
原创粉丝点击