DFS

来源:互联网 发布:五金行业进销存软件 编辑:程序博客网 时间:2024/06/05 08:45

Lotto

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3308    Accepted Submission(s): 1567


Problem Description
In a Lotto I have ever played, one has to select 6 numbers from the set {1,2,...,49}. A popular strategy to play Lotto - although it doesn't increase your chance of winning - is to select a subset S containing k (k>6) of these 49 numbers, and then play several games with choosing numbers only from S. For example, for k=8 and S = {1,2,3,5,8,13,21,34} there are 28 possible games: [1,2,3,5,8,13], [1,2,3,5,8,21], [1,2,3,5,8,34], [1,2,3,5,13,21], ... [3,5,8,13,21,34].

Your job is to write a program that reads in the number k and the set S and then prints all possible games choosing numbers only from S.
 

Input
The input file will contain one or more test cases. Each test case consists of one line containing several integers separated from each other by spaces. The first integer on the line will be the number k (6 < k < 13). Then k integers, specifying the set S, will follow in ascending order. Input will be terminated by a value of zero (0) for k.
 

Output
For each test case, print all possible games, each game on one line. The numbers of each game have to be sorted in ascending order and separated from each other by exactly one space. The games themselves have to be sorted lexicographically, that means sorted by the lowest number first, then by the second lowest and so on, as demonstrated in the sample output below. The test cases have to be separated from each other by exactly one blank line. Do not put a blank line after the last test case.
 

Sample Input
7 1 2 3 4 5 6 78 1 2 3 5 8 13 21 340
 

Sample Output
1 2 3 4 5 61 2 3 4 5 71 2 3 4 6 71 2 3 5 6 71 2 4 5 6 71 3 4 5 6 72 3 4 5 6 71 2 3 5 8 131 2 3 5 8 211 2 3 5 8 341 2 3 5 13 211 2 3 5 13 341 2 3 5 21 341 2 3 8 13 211 2 3 8 13 341 2 3 8 21 341 2 3 13 21 341 2 5 8 13 211 2 5 8 13 341 2 5 8 21 341 2 5 13 21 341 2 8 13 21 341 3 5 8 13 211 3 5 8 13 341 3 5 8 21 341 3 5 13 21 341 3 8 13 21 341 5 8 13 21 342 3 5 8 13 212 3 5 8 13 342 3 5 8 21 342 3 5 13 21 342 3 8 13 21 342 5 8 13 21 343 5 8 13 21 34
写了两个深搜
#include <stdio.h>int a[100];int b[100];int n,i,j;void dfs(int count,int start){if(start>n) return;//越界发生,返回if(count==6){ //6个数字,输出这一组数据,然后回溯到上一级,继续向下搜索for(int i = 0; i < 6; ++i){if(i == 0) printf("%d",b[i]);else printf(" %d",b[i]);}printf("\n"); return ;}     b[count]=a[start];    dfs(count+1,start+1);//向后搜索数字,直到搜索到头以后,返回上一级    dfs(count,start+1);//从上一级,向后跳过已经输出的数字,向后搜索其他的情况,直到越界}int main(){int t=0;while(scanf("%d",&n)!=EOF&&n){if(t!=0)printf("\n");for( i=0;i<n;i++)scanf("%d",&a[i]);dfs(0,0);t++;}} #include <stdio.h>int a[10],b[10];int use[10]={0};int n;void dfs(int cout,int pos){if(cout==6)//输出数据{for(int i = 0; i < 6; ++i)if(i == 0) printf("%d",b[i]);else printf(" %d",b[i]);printf("\n"); return;}for(int i=pos;i<n;i++)//用for循环控制着不断回溯搜索{if(!use[i]){use[i]=1;b[cout]=a[i];dfs(cout+1,i+1);use[i]=0;}}}int main(){while(~scanf("%d",&n)&&n ){for(int i=0;i<n;i++) scanf("%d",&a[i]);  dfs(0,0);}return 0;}

Accepted Necklace

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3820    Accepted Submission(s): 1515Problem DescriptionI have N precious stones, and plan to use K of them to make a necklace for my mother, but she won't accept a necklace which is too heavy. Given the value and the weight of each precious stone, please help me find out the most valuable necklace my mother will accept. InputThe first line of input is the number of cases. For each case, the first line contains two integers N (N <= 20), the total number of stones, and K (K <= N), the exact number of stones to make a necklace. Then N lines follow, each containing two integers: a (a<=1000), representing the value of each precious stone, and b (b<=1000), its weight. The last line of each case contains an integer W, the maximum weight my mother will accept, W <= 1000.  OutputFor each case, output the highest possible value of the necklace. Sample Input1 2 1 1 1 1 1 3  Sample Output1  
#include <iostream>#include <algorithm>#include <cstring>using namespace std;int n,k,bag;int vi[100];int m;struct stone{int value;int weight;}a[100];void dfs(int start,int w,int c,int p){if(c==k) {if(p>m&&w<=bag) m=p;else return;}if(w>bag||start>n) return ;vi[start]=1;dfs(start+1,w+a[start].weight,c+1,p+a[start].value);vi[start]=0;dfs(start+1,w,c,p);}int main(){int test;cin>>test;while(test--){memset(vi,0,sizeof(vi));cin>>n>>k;for(int i=0;i<n;i++)cin>>a[i].value>>a[i].weight;cin>>bag;m=0;dfs(0,0,0,0);cout<<m<<endl;}return 0;} 
-------------------------------------------------------------------------------------------------------------------
#include<stdio.h>struct node{int v,w; }p[22]; int n,k,v,w,a,t,sum;void dfs(int v,int w,int z,int j)//4个参数  依次是 价值 重量 合格宝石数量  j控制时间已经搜索过的不再判断 {if(w > a|| z > k)
return;//如果重量不符合或者合格个数超过限制--结束 if(sum < v)
sum = v;//价值最高 for(int i = j; i < n;i++)dfs(v+p[i].v, w+p[i].w, z+1,i+1);}int main(){scanf("%d",&t);while(t--){sum = 0;scanf("%d%d",&n, &k);// n块宝石,其中 k 块可以做成项链 for(int i = 0; i < n; i++)scanf("%d%d",&p[i].v,&p[i].w);scanf("%d", &a );//项链重量限制 dfs(0,0,0,0);printf("%dn",sum);}return 0;}

How Many Equations Can You Find

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1073    Accepted Submission(s): 703


Problem Description
Now give you an string which only contains 0, 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9.You are asked to add the sign ‘+’ or ’-’ between the characters. Just like give you a string “12345”, you can work out a string “123+4-5”. Now give you an integer N, please tell me how many ways can you find to make the result of the string equal to N .You can only choose at most one sign between two adjacent characters.
 

Input
Each case contains a string s and a number N . You may be sure the length of the string will not exceed 12 and the absolute value of N will not exceed 999999999999.
 

Output
The output contains one line for each data set : the number of ways you can find to make the equation.
 

Sample Input
123456789 321 1
 

Sample Output
181
----------------------------------------------------------------------------------------


#include <iostream>#include <string>using namespace std;char s[1000];int n,c;void dfs(int start,int sum){if(start==strlen(s)){if(sum==n) c+=1;return;}int m=0;for(int i=start;i<strlen(s);i++){m=m*10+s[i]-'0';dfs(i+1,sum+m);if(start!=0)//第一个字符只能是+号dfs(i+1,sum-m);}}int main(){while(cin>>s>>n){c=0;dfs(0,0);cout<<c<<endl;}return 0;}


 
原创粉丝点击