Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 34347 Accepted Submission(s): 15188

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
Source
Recommend
JGShining | We have carefully selected several similar problems for you:10101312
1072

pid=1242">
1242

pid=1175">
1175


<pre name="code" class="html">#include<stdio.h>
#include<string.h>
bool prim[44],visit[22];
int b[22];
int n;
void f(){
prim[2]=prim[3]=prim[5]=prim[7]=prim[11]=prim[13]=prim[17]=prim[19]=1;
prim[23]=prim[29]=prim[31]=prim[37]=prim[41]=1;
}
void dfs(int op){
if(op==n){
if(!prim[b[op-1]+b[0]]) return;
printf("%d",b[0]);
for(int i=1;i<n;++i)
printf(" %d",b[i]);
printf("\n");
return;
}
for(int i=2;i<=n;++i){
if(!visit[i]){
if(prim[b[op-1]+i]){
b[op]=i;visit[i]=1;dfs(op+1);
}
visit[i]=0;
}
}
}
int main(){
f();
int ncas=0;
while(~scanf("%d",&n)){
printf("Case %d:\n",++ncas);
memset(visit,0,sizeof(visit));
b[0]=1;dfs(1);
printf("\n");
}
return 0;
}


hdoj-1016-Prime Ring Problem【深搜】的更多相关文章

  1. hdoj 1016 Prime Ring Problem

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...

  2. HDOJ 1016 Prime Ring Problem素数环【深搜】

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...

  3. hdoj - 1258 Sum It Up && hdoj - 1016 Prime Ring Problem (简单dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1258 关键点就是一次递归里面一样的数字只能选一次. #include <cstdio> #inclu ...

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

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

  5. [HDU 1016]--Prime Ring Problem(回溯)

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

  6. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  7. hdu 1016 Prime Ring Problem(dfs)

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

  8. hdu 1016 Prime Ring Problem(DFS)

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

  9. HDU 1016 Prime Ring Problem(经典DFS+回溯)

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

  10. 杭电oj 1016 Prime Ring Problem

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

随机推荐

  1. Linux线程相互排斥量--进程共享属性

    多线程中.在相互排斥量和 读写锁的 属性中.都有一个叫 进程共享属性 . 对于相互排斥量,查询和设置这个属性的方法为: pthread_mutexattr_getpshared pthread_mut ...

  2. Mysql优化理论知识

    参考文章 http://blog.51cto.com/lizhenliang/2095526 ()硬件优化 如果有条件一定要SSD固态硬盘代替SAS机械硬盘,将RAID级别调整为RAID1+,相对于R ...

  3. k-meas非监督聚类分析

    实验名称: k-meas非监督聚类分析   一.实验目的和要求 目的: 加深对非监督学习的理解和认识 掌握聚类方法K-Means算法的设计方法   要求:     根据聚类数据,采用k-Means聚类 ...

  4. powerdesigner里的table背景色是不是可以修改的?

    Tools->Display Preferences->Format->Table->Modify->Fill->Fill color:

  5. jsp输出金字塔

    <% String str = ""; for(int i = 1; i <= 5; i++){ for(int j = 1; j <= 5-i; j++){ s ...

  6. ASP.NET 获取IIS应用程序池的托管管道模式

    asp.net 中怎样较为简单的获取网站程序池的托管管道模式 目前已知的方式是根据这个帖子https://github.com/kakalotte/... ,利用DirectoryEntry,但是程序 ...

  7. Python开发注意事项

    仅为记录自己在使用python过程的的一些心得!   1.服务器上运行脚本: windows服务器: 显式运行:在cmd中直接用python xxxx.py  运行一个py脚本文件. 后台运行:在cm ...

  8. <Sicily>Rails

    一.题目描述 There is a famous railway station in PopPush City. Country there is incredibly hilly. The sta ...

  9. 使用物化视图解决GoldenGate不能使用中文表名问题

    源端: conn sh/sh create table "学生" ("学号" number primary key,"姓名" varchar ...

  10. Futures and promises

    In computer science, future, promise, delay, and deferred refer to constructs used for synchronizing ...