HDOJ 1016 Prime Ring Problem (DFS)
来源:互联网 发布:淘宝怎么投诉卖家客服 编辑:程序博客网 时间:2024/04/29 03:02
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 36982 Accepted Submission(s): 16305
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
#include#includeint prime[40] = {0,1,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0}; //素数表int visit[21]; //访问标记int a[21]; // 存放序列结果int n;void dfs(int num){ int i; if (num == n && prime[a[num - 1] + 1]){ //结束条件 for (i = 0; i < num - 1; i++) printf("%d ", a[i]); printf("%d\n", a[num - 1]); } else{ for (i = 2; i <= n; i++){ if (!visit[i] && prime[i + a[num - 1]]){ //如果i没有被用过并且能与前面的数和成素数 visit[i] = 1; //标记i已被访问 a[num++] = i; //将i加入结果序列 dfs(num); //递归访问 visit[i] = 0; //回溯法标准框架去掉标记 num--; } } }}int main(){ int num = 0; while(scanf("%d", &n) != EOF){ num++; printf("Case %d:\n", num); memset(visit, 0, sizeof(visit)); a[0] = 1; dfs(1); printf("\n"); } return 0;}
0 0
- HDOJ 1016 Prime Ring Problem (DFS)
- HDOJ 1016 Prime Ring Problem(DFS)
- HDOJ 1016 Prime Ring Problem (DFS)
- hdoj 1016 Prime Ring Problem 【DFS】
- HDOJ 1016 Prime Ring Problem DFS
- HDoj-1016-Prime Ring Problem-DFS
- hdoj 1016 Prime Ring Problem【DFS】
- HDOJ 1016 Prime Ring Problem(dfs)
- hdoj 1016 Prime Ring Problem【DFS】
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
- HDOJ-1016Prime Ring Problem(DFS)
- HDOJ 题目1016 Prime Ring Problem(搜素,dfs)
- HDOJ 1016 Prime Ring Problem(dfs+回溯)
- HDOJ-1016-Prime Ring Problem(素数环)【DFS】
- HDOJ 1016 Prime Ring Problem(DFS + STL容器)
- hdu 1016 Prime Ring Problem(DFS)
- hdu 1016 Prime Ring Problem(DFS)
- HDU 1016 Prime Ring Problem (DFS)
- 杭电acm2700
- HDOJ 1241 Oil Deposits (DFS)
- win7下QT5.2.0+VS2010环境配置
- javascript深入理解js闭包
- 我的C++学习历程(old)
- HDOJ 1016 Prime Ring Problem (DFS)
- hdu 3591 多重背包+完全背包练习题
- arm交叉编译器gnueabi、none-eabi、arm-eabi、gnueabihf、gnueabi区别
- HDOJ 1010 Tempter of the Bone (DFS)
- 常用正则表达式
- UnsatisfiedLinkError: No implementation found for , AndroidStudio使用*.so
- sqlite变更字段类型
- 最小二乘法求解的两种表示方法
- 图像处理、分析、识别、应用的课程笔记,PPT/PDF课件,计算机视觉中的机器学习、目标识别、分割、文本识别、fMRI 分析、运动和追踪等在线视频教程