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. 案例:利用累加器计算前N个学生的总成绩和平均成绩

    /* *录入N个学生的成绩,并求出这些学生的总成绩和平均成绩! * */ import java.util.Scanner; public class SumTest{ public static v ...

  2. Linux: FTP服务原理及vsfptd的安装、配置

    1.FTP 服务的安装# yum install -y vsftpd [root@rusky pub]# ls -l /etc/vsftpd/ total 20 -rw-------. 1 root ...

  3. node.js的ejs模版引擎

    ejs版本是0.8.8,生成的views目录下面只有index.ejs and error.ejs,没有layout.ejs. D:\lianchuangfile\nodeDevelop\microb ...

  4. Wpf TextChanged事件导致死循环,事件触发循环问题

    1.实例: 说明:当TextBox控件的Text内容发生变化时,TextChanged事件触发,并且会立即同步执行. 基于这个特点,设置一个全局变量标识,ChangeTxtB,如果是正在修改txtB的 ...

  5. Linux服务器指令

    1.查看cpu信息:/proc/cpuinfo2.查看内存信息:/prco/meminfo3.查看服务器版本信息:cat /etc/issue4.服务器系统位数:uname -a5.网卡信息:ifco ...

  6. Wireshark抓包、过滤器

    查阅于http://blog.sina.com.cn/s/blog_5d527ff00100dwph.html 1.捕捉过滤器 设置捕捉过滤器的步骤是:- 选择 capture -> optio ...

  7. nuc900 nand flash mtd 驱动

    nuc900 nand flash mtd 驱动,请参考! /* * Copyright © 2009 Nuvoton technology corporation. * * Wan ZongShun ...

  8. Smarty中{literal}的使用详解

     {literal} <script>function Login(){ document.LoginForm.submit();}</script>{/literal} == ...

  9. CSS hack技巧

    CSS hack技巧一览,原文来自CSDN freshlover的博客专栏<史上最全CSS Hack方式一览> 什么是CSS hack 由于不同厂商的流览器或某浏览器的不同版本(如IE6- ...

  10. PHP面向对象(OOP)编程完全教程:10.__set(),__get(),__isset(),__unset()四个方法的应用

    一般来说,总是把类的属性定义为private,这更符合现实的逻辑.但是, 对属性的读取和赋值操作是非常频繁的,因此在PHP5中,预定义了两个函数”__get()”和”__set()”来获取和赋值其属性 ...