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

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
int Case = 0;
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
int a[] = new int[n];
//初始数组1-n
int color[] = new int[n];
//判断数字是否已经存在
int prant[] = new int[n];
//输出数据排序
int count =0;//计数器
for(int i=0;i<n;i++){
a[i]=i+1;
color[i] = -1;
}//初始化数据 Case++;
System.out.println("Case "+(Case)+":"); dfs(a,color,prant,count,0);
System.out.println(); }
} private static void dfs(int[] a, int[] color, int[] prant, int count,int m) {
//System.out.println(count);
count++;//计数器加1
if(count == a.length&&p(prant[0],a[m])){
//注意第一个数和最后一个数相加的和也必须为素数
prant[count-1]=a[m];
for(int i=0;i<a.length-1;i++){
System.out.print(prant[i]+" ");
}
System.out.println(prant[a.length-1]); //return ;
} for(int i=0;i<a.length;i++){
color[m] =1;
if(p(a[m],a[i])&&color[i]==-1){
color[i]=1;
prant[count-1]=a[m]; dfs(a,color,prant,count,i); color[i]=-1; } } } //判断是不是素数
private static boolean p(int i, int j) {
int sum = i+j;
for(int a=2;a*a<=sum;a++){
if(sum%a==0){
return false;
}
}
return true;
} }

C语言:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; int n;
int df[21];
int t=1;
int m[21];
int mi;
bool pn(int x,int y){//判断素数
for(int i=2;i*i<=x+y;i++){
if((x+y)%i==0){
return false;
}
}
return true;
} void dfs(int x){
if(mi==n&&pn(m[1],m[n])){
for(int i=1;i<n;i++){
printf("%d ",m[i]);
}
printf("%d\n",m[n]);
return;
} for(int i=2;i<=n;i++){
if(df[i]==0&&pn(x,i)){
df[x]=1;
mi++;//当前小球数
m[mi]=i;
dfs(i);
df[x]=0;
mi--;//必须减一
}
} } int main()
{
while(~scanf("%d",&n)){
printf("Case %d:\n",t);
t++;
memset(df,0,sizeof(df));
mi=1;
m[mi]=1;
dfs(1);
printf("\n");
}
return 0;
}

HDOJ 1016 Prime Ring Problem素数环【深搜】的更多相关文章

  1. Hdu 1016 Prime Ring Problem (素数环经典dfs)

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

  2. 题目1459:Prime ring problem(素数环问题——递归算法)

    题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  3. hdoj 1016 Prime Ring Problem

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

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

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

  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. HDOJ(HDU).1016 Prime Ring Problem (DFS)

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

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

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

  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 ...

随机推荐

  1. Linux驱动开发学习的一些必要步骤

      1. 学会写简单的makefile 2. 编一应用程序,可以用makefile跑起来 3. 学会写驱动的makefile 4. 写一简单char驱动,makefile编译通过,可以insmod, ...

  2. <<java 并发编程>>第七章:取消和关闭

    Java没有提供任何机制来安全地终止线程,虽然Thread.stop和suspend等方法提供了这样的机制,但是存在严重的缺陷,应该避免使用这些方法.但是Java提供了中断Interruption机制 ...

  3. ubuntu下安装tomcat和配置mysql

    1.到官网 http://jakarta.apache.org/tomcat-7.0.2.tar.gz 安装文件,JDK假设已经安装完毕,接下来直接安装tomcat-7.0.2.tar.gz # ls ...

  4. Android(java)学习笔记245:ContentProvider使用(银行数据库创建和增删改查的案例)

    1. Android的四大组件: (1)Activity  用户交互的UI界面 (2)Service  后台运行的服务 (3)BroadcastReceiver 广播接收者 (4)ContentPro ...

  5. java多态---内存关系

    在该列中,a.lookDoor()会报错,因为azhong没有lookDoor这个方法,同理,a.playGame()也会报错. 注意!!! 最后一句Dog dd=(Dog)a: 这句话非常错误! 在 ...

  6. htm5 user-scalable 的意思

    <meta name="viewport" content="width=device-width,user-scalable=yes,minimum-scale= ...

  7. 关于PHP导入项目的时候导入不了的情况

    导入的时候,会发现明明是一个手动创建的一个项目, 才能导入, 有时候会发现这样导入不了的情况 那是因为,可能这个项目是手动创建的,如果通过IDE可能看不出来 不过如果你进入项目的根目录的时候就会知道 ...

  8. lucene查询排序结果原理总结

    参考文章 Lucene3.0结果排序原理+操作+示例 Lucene的排序算法 一句话总结lucene排序算法是什么样的 关键几个概念 参考文档: http://lucene.apache.org/co ...

  9. SSM框架入门和搭建 十部曲

    又快到毕业设计的时候了,有的学弟说想用ssm做毕业设计,在网上找到资料看不懂,基础差.我就帮他写了一个demo,顺便也整理一下. SSM框架,顾名思义,就是Spring+SpringMVC+mybat ...

  10. _Obj* __STL_VOLATILE* __my_free_list

    今天在读<STL源码剖析>空间配置器第二级时看到了这句,有点不解,于是查阅后知: obj后面是个指针 STL_VOLATILE也应该是个类型定义的吧,程序中应该有define来对它定义.所 ...