HDU 1016 Prime Ring Problem
来源:互联网 发布:网络诈骗形式 编辑:程序博客网 时间:2024/06/07 03:11
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25182 Accepted Submission(s): 11243
Total Submission(s): 25182 Accepted Submission(s): 11243
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 <iostream>using namespace std;#define M 25int n,tot,su[M],vis[M],A[M];int S(int a){ int i; for(i=2;i<=a/2;i++) { if(a%i==0) return 0; } return 1;}//素数打表void DFS(int cur){ int i,j; if(cur==n&&su[A[0]+A[n-1]]) //状态:cur(已经排列数的个数);范围:1<=x<=n; 解空间的边:n条,选择第i条边x=i; { for(i=0;i<n;i++) { if(i!=n-1) printf("%d ",A[i]); else printf("%d",A[i]); } printf("\n"); } else { for(i=2;i<=n;i++) { if(su[A[cur-1]+i]&&!vis[i]) //如果两数相加不是素数或者这个数用过就直接跳过不做处理(处死)(限界函数:su[]和vis[]) { //如果符合就表示可能存在解,进入子树,再将子树做新节点。 A[cur]=i; vis[i]=1; DFS(cur+1); vis[i]=0; } } }}int main(){ int i; for(i=0;i<M;i++) { su[i]=S(i); A[i]=i+1; } i=1; while(scanf("%d",&n)!=EOF) { printf("Case %d:\n",i); DFS(1); i++; printf("\n"); } return 0;}/*解的构成:从根节点到叶节点的路径。限界函数: (1)在从根节点开始到本节点的路径已经选过的不是合理选择。 (2)与父节点的和是素数。 (3)用过本节点的深度是n,与根节点的和为素数。如何处死:不处理。
0 0
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- HDU 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- HDU-1016 prime ring problem
- hdu 1016Prime Ring Problem
- hdu 1016 Prime Ring Problem
- HDU-1016 Prime Ring Problem
- HDU-1016-Prime Ring Problem
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- Hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- Hdu 1016 - Prime Ring Problem
- HDU 1016 Prime Ring Problem
- Apache服务的工作原理
- 用dfs实现1到n的全排列
- linux内核2.6.3x--Executable file formats / Emulations、 Networking support
- Oracle服务器安装 第一次使用 安装命令
- Byte[]转换为字符串System.Text.Encoding.Default.GetString()尾部空‘\0’信息处理
- HDU 1016 Prime Ring Problem
- vm三种连接方式bridge, nat, host-only
- Android Studio科普篇——2.误区
- redis的java客户端Jedis简单封装
- 在Nginx下针对IP和目录限速
- Android和C#实现实时视频传输Demo
- C# 调用 C++ dll 的一些问题
- 屏蔽CListCtrl选择功能
- Apache 防止未授权域名指向服务器