hdoj - 1258 Sum It Up && hdoj - 1016 Prime Ring Problem (简单dfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1258

关键点就是一次递归里面一样的数字只能选一次。

 1 #include <cstdio>
 2 #include <cstring>
 3
 4 int n,t;
 5 int b[15],c[15];
 6 bool flag;
 7 void dfs(int k,int sum,int l)
 8 {
 9     if(sum==t)
10     {
11         for(int i=0;i<l-1;i++)
12             printf("%d+",c[i]);
13         printf("%d\n",c[l-1]);
14         flag=1;
15         return;
16     }
17     int last=-1;
18     for(int i=k;i<n;i++)
19     {
20         if(sum+b[i]>t) continue;
21         if(b[i]!=last) //注意这个就好了
22         {
23             last=c[l]=b[i];
24             dfs(i+1,sum+b[i],l+1);
25         }
26     }
27 }
28
29 int main()
30 {
31    // freopen("a.txt","r",stdin);
32     while(~scanf("%d%d",&t,&n))
33     {
34         if(n==0) break;
35         memset(c,0,sizeof(c));
36         flag=0;
37         for(int i=0;i<n;i++)
38             scanf("%d",&b[i]);
39         printf("Sums of %d:\n",t);
40         dfs(0,0,0);
41         if(!flag) printf("NONE\n");
42     }
43     return 0;
44 }

http://acm.hdu.edu.cn/showproblem.php?pid=1016

这题注意回溯就好。

 1 #include <cstdio>
 2 #include <cstring>
 3 int n,b[25];
 4 bool used[25];
 5 bool is_prime(int x)
 6 {
 7     if(x==1) return false;
 8     else if(x==2||x==3) return true;
 9     for(int i=2;i*i<=x;i++)
10         if(x%i==0) return false;
11     return true;
12 }
13
14 void dfs(int k,int num)
15 {
16     if(num==n)
17     {
18         //printf("%d\n",num);
19         if(is_prime(b[n]+b[1]))
20         {
21             //printf("%d\n",k);
22             for(int i=1;i<n;i++)
23                 printf("%d ",b[i]);
24             printf("%d\n",b[n]);
25         }
26         return;
27     }
28     for(int i=1;i<=n;i++)
29     {
30         if(!used[i]&&is_prime(b[num]+i))
31         {
32             used[i]=true;
33             b[num+1]=i;
34             //printf("%d %d\n",i,num);
35             dfs(i,num+1);
36             used[i]=false;
37         }
38     }
39 }
40
41 int main()
42 {
43    // freopen("a.txt","r",stdin);
44     int j=1;
45     while(~scanf("%d",&n))
46     {
47         memset(b,0,sizeof(b));
48         memset(used,0,sizeof(used));
49         printf("Case %d:\n",j++);
50         b[1]=1;
51         used[1]=true;
52         dfs(1,1);
53         printf("\n");
54     }
55     return 0;
56 }
时间: 2024-09-29 09:59:16

hdoj - 1258 Sum It Up && hdoj - 1016 Prime Ring Problem (简单dfs)的相关文章

hdu 1016 Prime Ring Problem (简单DFS)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25700    Accepted Submission(s): 11453 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

hdoj 1016 Prime Ring Problem 【DFS】

策略如题 链接 http://acm.hdu.edu.cn/showproblem.php?pid=1016 代码: #include<stdio.h> #include<string.h> int prime[25] = {1, 1}, n, vis[25]; //vis作用:标记是否用过 int a[25]; void f() //找出来前20的素数 判定为0 { for(int i = 2; i <= 24; i ++){ if(prime[i] == 0) for(i

HDU 1016 Prime Ring Problem --- 经典DFS

思路:第一个数填1,以后每个数判断该数和前一个数想加是否为素数,是则填,然后标记,近一步递归求解. 然后记得回溯,继续判断下一个和前一个数之和为素数的数. /* HDU 1016 Prime Ring Problem --- 经典DFS */ #include <cstdio> #include <cstring> int n; bool primer[45], visit[25]; //primer打素数表,visit标记是否访问 int a[25]; //数组a存储放置的数 /

hdu 1016 Prime Ring Problem (dfs)

一切见注释. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; bool vis[22]; int n; int ans[22]; int top; bool isprime(int x)//判断素数 { for(int i=2;i<x;i++) if(x%i==0)return false; return

HDU 1016 Prime Ring Problem 题解

Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1

[ACM] 1016 Prime Ring Problem (深度优先搜索)

Prime Ring Problem Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle

[HDU 1016]--Prime Ring Problem(回溯)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2

HDU 1016 Prime Ring Problem(素数环问题)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 63806    Accepted Submission(s): 27457 Problem Description A ring is compos

【dfs】hdu 1016 Prime Ring Problem

[dfs]hdu 1016 Prime Ring Problem 题目链接 刚开始接触搜索,先来一道基本题目练练手. 注意对树的深度进行dfs dfs过程中注意回退!!! 素数提前打表判断快一些 参考代码 /*Author:Hacker_vision*/ #include<bits/stdc++.h> #define clr(k,v) memset(k,v,sizeof(k)) using namespace std; const int _max=1e3+10;//素数打表 int n,pr