Prime Ring Problem (DFS练习题)

K - Prime Ring Problem

=================================================================================================================================

题目大意是给出 1~n 个数 第一个数必定是 1 ,使得无论那两个相邻的数相加,都是质数(即大于1的自然数中,除了1和它本身以外不再有其他因数);

打印出所有可能,即直接用dfs 遍历所有可能性;

我的代码思路:

1. 数组范围很小 最大的和不超过40  则可以直接预处理这个范围内的数是否为质数。

2.如何快速判断是否为质数 ,快速的方法 :先判断2之后  3~sqrt(n) 之间所有的奇数 是否存在其约数。

3.用tail数组储存列表。

4.一般dfs的套路 使用book 标记是否使用过,开始遍历。

=================================================================================================================================

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 using namespace std;
 6 int n;
 7 bool prime[40];
 8 void Prime()  //预处理1~40之间的数是否为质数
 9 {
10     for(int i = 3;i <= 40;++i)
11     {
12         bool flag = 0;
13         for(int j = 2;j<=sqrt(i);j==2?++j:j+=2)
14           if(i%j==0) {flag = 1;break;}
15         flag==0?prime[i]=1:prime[i]=0; //1即为质数0则否
16     }
17 }
18 int tail[40]; //列表
19 void print()  //打印列表
20 {
21     for(int i =1;i<=n;++i)
22         printf(i==n?"%d\n":"%d ",tail[i]);
23 }
24 bool book[40]; //标记
25 void dfs(int x)
26 {
27     if(x==n)
28        {   //最后再判断最后一个数与第一个数相加是否为质数
29            if(prime[tail[n]+tail[1]]) print();
30            return;
31        }
32     for(int i = 2;i<=n;++i)
33     {
34         if(prime[i+tail[x]]&&book[i]==0)//标准dfs套路↓
35         {
36             tail[x+1] = i;
37             book[i] = 1;
38             dfs(x+1);
39             book[i] = 0;
40         }
41     }
42 }
43 int main()
44 {
45     int cas = 0;
46     Prime();
47     tail[1] = 1; book[1] = 1;
48     while(~scanf("%d",&n))
49     {
50         memset(book,0,sizeof(book));
51         printf("Case %d:\n",++cas);
52         if(n==1) printf("1\n");
53         else if(n%2==1) ; //如果是奇数,铁定实现不了
54         else dfs(1);
55         putchar(‘\n‘);
56     }
57 }

原文地址:https://www.cnblogs.com/darkboy/p/9402297.html

时间: 2024-08-29 20:13:10

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): 29577    Accepted Submission(s): 13188 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

hdu1016 Prime Ring Problem dfs 素数打表

意思是给你一个数n,要构成一个素数环,这个素数由1-n组成,它的特征是选中环上的任意一个数字i,i与它相连的两个数加起来都分别为素数,满足就输出. 这个题的做法和hdu1015做法差不多都是使用dfs 回溯.不同之处在于这个要全部搜索,而hdu1015只需要搜索第一组就可以. 其次在这个题目中使用素数打表的方式简化素数判定,在一定情况下也是对效率有所提高的. Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limi

HDU 1016 Prime Ring Problem(DFS)

题目链接 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

UVA - 524 Prime Ring Problem(dfs回溯法) 解题心得

原题 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  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.

HDU1016 Prime Ring Problem DFS 简单题

题意:输入n,代表有一个n个节点的环,然后在节点分别填入1到n这n个数,规定,第一个填入的必须是1. 0<n<40 要求填入后满足,任意相邻的2个节点的数之和为素数. 将满足条件的填法按照字典序的顺序小到大依次输出. 其实刚开始做的时候我觉得这道题会做很久的,想好后就开始打了,打着打着发现这道题打好了,有点意外,原来这么简单. 注意回溯,在每次DFS后要记得把状态恢复原样,比如这道题是每次DFS后都要把当前DFS的数字i恢复为vis[i]=false; 先考虑好初始化,结束条件,回溯. 先来一

HDOJ-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): 47217    Accepted Submission(s): 20859 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

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存储放置的数 /

【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

HDU 1016 Prime Ring Problem (素数筛+DFS)

题目链接 题意 : 就是把n个数安排在环上,要求每两个相邻的数之和一定是素数,第一个数一定是1.输出所有可能的排列. 思路 : 先打个素数表.然后循环去搜..... 1 //1016 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 6 using namespace std ; 7 8 bool vis[21]; 9 int prime[42] ,cs[21]; 10 int n ; 11