杭电——1016 Prime Ring Problem 深搜
来源:互联网 发布:word for mac如何使用 编辑:程序博客网 时间:2024/05/16 14:59
本题链接:点击打开链接
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 48854 Accepted Submission(s): 21541
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.
Note: the number of first circle should always be 1.
Input
n (0 < n < 20).
Output
The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.
You are to write a program that completes above process.
Print a blank line after each case.
You are to write a program that completes above process.
Print a blank line after each case.
Sample Input
68
Sample Output
Case 1:1 4 3 2 5 61 6 5 2 3 4Case 2:1 2 3 8 5 6 7 41 2 5 8 3 4 7 61 4 7 6 5 8 3 21 6 7 4 3 8 5 2
Source
Asia 1996, Shanghai (Mainland China)
Recommend
JGShining | We have carefully selected several similar problems for you: 1072 1242 1253 1026 1015
PS;本人写的第一道深搜题目,觉得我写的不好的大神可以选择不看。
题意:输入一个偶数n,然后找出1~n的组合形成一个环,使得相邻两个数之和为素数,即形成素数环;输出所有符合条件的组合素数环。输入数据有多组,其中1<n<20.
解题思路:
1.由于所求数据在20以内,相加之后不会超过40,所有可以提前预处理一下做个40以内的素数判断数组,以节约用时。
2.接着就是做深搜循环判断了。
代码如下:
#include <string.h>#include <stdio.h>#define M 40int vis[M]; //标记数组int dis[M]; //素数环数组int a[M];//素数判断数组int n;void sushu() //素数预处理,若数组a的下标为素数,那么a[i]=1,否则a[i]=0,(本人还是习惯用1,0表示,不习惯用bool类型,但推荐用bool型,我也会慢慢改变的){ int i,j; memset(a,1,sizeof(a)); for(i=2;i<M;i++) { if(a[i]) for(j=i+i;j<M;j+=i) //是i的倍数都不是素数 a[j]=0; //不是素数的标记为0 }}void dfs(int x) //x表示已判断个数{ int i; if(x==n&&a[dis[n-1]+dis[0]]) //当判断了n个数并且dis[n-1]+dis[0]也是素数的的时候输出该素数环 { for(i=0;i<n-1;i++) printf("%d ",dis[i]); printf("%d\n",dis[n-1]); } else { for(i=2;i<=n;i++) if(vis[i]&&a[dis[x-1]+i]) { vis[i]=0; //符合条件,标记为0,表示该数已被使用,不可再用 dis[x]=i; dfs(x+1); vis[i]=1;//回溯 } }}int main(){ int t=1; sushu(); while(~scanf("%d",&n)) { memset(vis,1,sizeof(vis)); //记得每次初始化数组 printf("Case %d:\n",t++); vis[1]=0; dis[0]=1; dfs(1); printf("\n"); } return 0;}
2 0
- 杭电——1016 Prime Ring Problem 深搜
- 杭电OJ——1016 Prime Ring Problem
- 杭电 HDU 1016 Prime Ring Problem
- 杭电1016-Prime Ring Problem
- 杭电 Prime Ring Problem 1016 DFS
- 杭电 1016 Prime Ring Problem
- 杭电OJ 1016:Prime Ring Problem
- 杭电 1016 Prime Ring Problem
- 杭电1016Prime Ring Problem
- 杭电 1016 Prime Ring Problem
- Prime Ring Problem HDU 杭电1016
- 杭电 1016 Prime Ring Problem【DFS】
- 杭电1016 Prime Ring Problem
- hdu 杭电 1016 Prime Ring Problem
- 杭电OJ 1016 Prime Ring Problem
- 杭电 oj 1016 Prime Ring Problem
- 杭电1016 Prime Ring Problem
- 杭电1016 Prime Ring Problem DFS深搜
- spring mybatis
- Spring-MVC 整合JSON 实例
- tensorflow +mnist
- 深入理解 Yii2.0
- Junit4小技巧-测试基类
- 杭电——1016 Prime Ring Problem 深搜
- R语言---数据类型
- android:Button备忘
- 杂记
- 流程、模板 v.s. 敏捷、 精益 v.s. 产品级敏捷、微服务产品级敏捷
- [Qt-creator] The .pro file could not be parsed
- AndroidStudio导入项目过慢
- MySQL临时表的简单用法 在大数据量时有时会加快查询速度
- symbol lookup error: /home/caffe/anaconda/lib/libreadline.so.6: undefined symbol: PC