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

简介: HDOJ 1016 Prime Ring Problem素数环【深搜】

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.

image.png


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;
    }
}
目录
相关文章
|
6月前
hdoj 4715 Difference Between Primes 素数筛选+二分查找
hdoj 4715 Difference Between Primes 素数筛选+二分查找
26 1
|
8月前
|
算法
1091 zoj Knight Moves的BFS算法和DFS
1091 zoj Knight Moves的BFS算法和DFS
35 0
|
5月前
|
Java
hdu1016 Prime Ring Problem【素数环问题(经典dfs)】
hdu1016 Prime Ring Problem【素数环问题(经典dfs)】
24 0
|
6月前
|
开发框架 .NET
poj 3468 A Simple Problem with Integers线段树区间修改
题目意思很简单,有N个数,Q个操作, Q l r 表示查询从l到r 的和,C l r v 表示将从l到r 的值加上v,明显的线段树,不知道线段树的人肯定暴力,肯定超时,哈哈!!
19 0
|
7月前
|
算法
The kth great number(小根堆思想,模板题)
The kth great number(小根堆思想,模板题)
23 0
The kth great number(小根堆思想,模板题)
HDU-1016,Prime Ring Problem(DFS+素数)
HDU-1016,Prime Ring Problem(DFS+素数)
|
C语言
HDOJ 1016 Prime Ring Problem素数环【深搜2】
HDOJ 1016 Prime Ring Problem素数环【深搜】
78 0
HDOJ(HDU) 2136 Largest prime factor(素数筛选)
HDOJ(HDU) 2136 Largest prime factor(素数筛选)
89 0
|
Go
HDOJ(HDU) 1977 Consecutive sum II(推导、、)
HDOJ(HDU) 1977 Consecutive sum II(推导、、)
87 0