hdu1016 Prime Ring Problem(DFS)
来源:互联网 发布:易语言七夕表白源码 编辑:程序博客网 时间:2024/05/29 18:27
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29755 Accepted Submission(s): 13268
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)
解题报告:因为蓝桥杯快到了,所以把以前做过的搜索题再重新做一遍。素数环,用过的数字标记就可以了。
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;int arr[21], vis[21], n;bool isPrime(int n) {//n > 2if(n % 2 == 0) return 0;for (int i = 2; i <= sqrt(n); i ++) {if(n % i == 0) {return 0;}}return 1;}void dfs(int k) {if(n == k) {if(!isPrime(1 + arr[n-1])) return ;for (int i = 0; i < n-1; i ++) {printf("%d ", arr[i]);}printf("%d\n", arr[n-1]);return ;}for (int i = 2; i <= n; i ++) {if(!vis[i] && isPrime(arr[k-1] + i)) {arr[k] = i;vis[i] = 1;dfs(k+1);vis[i] = 0;}}}int main() {int cnt = 1;;while (scanf("%d", &n)!= EOF) {memset(vis, 0, sizeof(vis));vis[1] = 1;arr[0] = 1;printf("Case %d:\n", cnt ++);dfs(1);printf("\n");}return 0;}
0 0
- HDU1016:Prime Ring Problem(DFS)
- hdu1016 Prime Ring Problem(DFS)
- Hdu1016 Prime Ring Problem(DFS)
- HDU1016:Prime Ring Problem(DFS)
- HDU1016:Prime Ring Problem(DFS)
- HDU1016 Prime Ring Problem 解题报告--dfs
- HDU1016-Prime Ring Problem(DFS)
- hdu1016(Prime Ring Problem)- dfs+枚举
- hdu1016 Prime Ring Problem(dfs)
- hdu1016 Prime Ring Problem(dfs)
- hdu1016-Prime Ring Problem(简单dfs)
- hdu1016 Prime Ring Problem(回溯dfs)
- hdu1016 Prime Ring Problem(dfs)
- hdu hdu1016 Prime Ring Problem(dfs)
- HDU1016 Prime Ring Problem (DFS)
- HDU1016 Prime Ring Problem(dfs)
- HDU1016:Prime Ring Problem(dfs)
- HDU1016 Prime Ring Problem
- Unity3D高通ar-----增强现实
- boost库之aiso通信
- poj 2503 Babelfish Trie
- java语言之单例设计模式
- uva 165 Stamps (回溯)
- hdu1016 Prime Ring Problem(DFS)
- 数组、结构体、指针1
- Android LayoutInflater 详解
- Ordering Tasks
- Linux内核分析MOOC课程汇编分析
- 有getter setter的属性在Inspector视图中设置值
- session与cookie的区别和联系?
- 数组、结构体、指针2
- 数学之美番外篇:进化论中的概率论