Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 66391    Accepted Submission(s): 28456

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.

 
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.

 
Sample Input
6
8
 
Sample Output
Case 1:
1 4 3 2 5 6
1 6 5 2 3 4
Case 2:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2
 
//HD1016
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
static int j = ;
int n, a[];
bool prime[], vis[]; bool is_prime(int m)//素数判断
{
if (m == )//1不是素数
return false;
for (int i = ; i*i <= m; i++)//素数只能被1和本身整除
if (m % i == )
return false;
return true;
}
void dfs(int cur)
{
if (cur == n && prime[a[] + a[n - ]])//递归出口,当到最后一个数并且首位相加为素数就结束递归
{
for (int i = ; i < n; i++)
{
cout << a[i];
if (i < n - )
cout << ' ';
else
cout << endl;
}
}
else
for (int i = ; i <= n; i++)
if (!vis[i] && prime[i + a[cur - ]])
{
a[cur] = i;
vis[i] = ;
dfs(cur + );
vis[i] = ;
}
} int main()
{
memset(a, , sizeof(a));
memset(prime, false, sizeof(prime));
memset(vis, false, sizeof(vis));
for (int i = ; i < ; i++)
prime[i] = is_prime(i);
while (scanf("%d", &n) != EOF)
{
a[] = ;
printf("Case %d:\n", j++);
dfs();
cout << endl;
}
return ;
}

hdu1016 Prime Ring Problem(DFS)的更多相关文章

  1. Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  3. HDU1016 Prime Ring Problem(DFS回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. hdu1016 Prime Ring Problem【素数环问题(经典dfs)】

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. HDU 1016 Prime Ring Problem (DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. Prime Ring Problem(dfs水)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  7. Prime Ring Problem (DFS练习题)

    K - Prime Ring Problem ============================================================================= ...

  8. Prime Ring Problem dfs

    A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle ...

  9. hdu1016 Prime Ring Problem

    dfs,用全局数组和变量保存并更新当前状态. 答案可以直接在搜索结束时打印,n为奇数时方案数为0. acm.hdu.edu.cn/showproblem.php?pid=1016 #include & ...

随机推荐

  1. 修改linux内核启动logo及显示位置

    转载于:http://blog.chinaunix.net/uid-28458801-id-3484269.html 在此基础上我又添加了我的一些不同的地方,仅供参考 内核版本: 2.6.35.3 l ...

  2. 常用linux配置文件

    /etc/hosts:用户IP与域名的对应解析表/etc/sysconfig/network:机器名.网卡启动.网关等配置/etc/fstab:记录开机自动挂载的文件系统/etc/rc.local:开 ...

  3. Opengl使用模型视图变换移动光源

    光源绕一个物体旋转,按下鼠标左键时,光源位置旋转. #include <GL/glut.h> static int spin = 0;static GLdouble x_1 = 0.0;s ...

  4. UITextField的文本框部分文本以*的方式来显示

    #import "AppDelegate.h" @interface AppDelegate ()<UITextFieldDelegate>// 添加代理协议 @end ...

  5. C#.NET中Dns类的常用方法及说明

    IP是一种普遍应用于因特网.允许不同主机能够相互找到对方的寻址协议.IP地址由4个十进制的数字号码所组成,而每一个号码的值介于0~255之间,它虽然解决了网络上计算机的识别问题,但是IP地址确不容易记 ...

  6. hive与hbase数据类型对应关系

    hive与hbase数据类型对应关系 当hbase中double,int 类型以byte方式存储时,用字符串取出来必然是乱码. 在hivd与hbase整合时也遇到这个问题:做法是:#b 1.加#b C ...

  7. cf123E Maze

    传送门 分析 见ptx大爷的博客 代码 #include<iostream> #include<cstdio> #include<cstring> #include ...

  8. NETTY4中的BYTEBUF 内存管理

    转 http://iteches.com/archives/65193 Netty4带来一个与众不同的特点是其ByteBuf的重现实现,老实说,java.nio.ByteBuf是我用得很不爽的一个AP ...

  9. 数据结构 nxd(顺序对)

    数据结构 nxd(顺序对) 问题描述 给定 n 个数 a1,a2,...,an,求满足条件的(i,j)数量: i<j 且 a[i]<a[j] ★数据输入输入第一行为一个正整数 n.第二行为 ...

  10. LeetCode第70题:爬楼梯

    问题描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...