Prime Ring Problem 经典素数环

Prime Ring Problem

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.

Inputn (0 < n < 20). 
OutputThe 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

小范围数据,当然是递归+打表啦~这里掌握某种规律后也可以提高搜索效率,比如
素数环:给定n,1~n组成一个素数环,相邻两个数的和为素数。
      首先偶数(2例外,但是本题不会出现两个数的和为2)不是素数,
      所以素数环里奇偶间隔。如果n是奇数,必定有两个奇数相邻的情况。
      所以当n为奇数时,输出“No Answer”。
      当n == 1时只1个数,算作自环,输出1
      所有n为偶数的情况都能变成奇偶间隔的环-----所以都有结果。


#include<stdio.h>
#include<string.h>
int n,c=0,i;
int a[25],b[25];
int jo(int a)
{
    return a%2==0?0:1;
}
int prime[40]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0};
void dfs(int step)
{
    int i;
    if(step>n&&prime[a[1]+a[n]]){
        for(i=1;i<=n;++i){
            if(i==1) printf("%d",a[i]);
            else printf(" %d",a[i]);
        }
        printf("\n");
        return;
    }
    if(jo(a[step-1])){
        for(i=2;i<=n;i+=2){
            if(!b[i]&&prime[a[step-1]+i]){
                b[i]=1;
                a[step]=i;
                dfs(step+1);
                b[i]=0;
            }
        }
    }
    else{
        for(i=3;i<=n;i+=2){
            if(!b[i]&&prime[a[step-1]+i]){
                b[i]=1;
                a[step]=i;
                dfs(step+1);
                b[i]=0;
            }
        }
    }
}
int main()
{
    while(~scanf("%d",&n)){
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        a[1]=1;b[1]=1;
        if(n==1) printf("Case %d:\n1\n\n",++c);
        else if(!jo(n)){
            printf("Case %d:\n",++c);
            dfs(2);
            printf("\n");
        }
    }
    return 0;
} 
时间: 2024-12-23 09:40:59

Prime Ring Problem 经典素数环的相关文章

A - Prime Ring 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

HDU 1016 Prime Ring Problem --- 经典DFS

思路:第一个数填1,以后每个数判断该数和前一个数想加是否为素数,是则填,然后标记,近一步递归求解. 然后记得回溯,继续判断下一个和前一个数之和为素数的数. /* HDU 1016 Prime Ring Problem --- 经典DFS */ #include <cstdio> #include <cstring> int n; bool primer[45], visit[25]; //primer打素数表,visit标记是否访问 int a[25]; //数组a存储放置的数 /

hdu 1016 Prime Ring Problem (素数环)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 题目大意:输入一个n,环从一开始到n,相邻两个数相加为素数. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int visit[22],q[100],n; 6 int sushu(int n) 7 { 8 int i; 9 for (i=2

UVA524 素数环 Prime Ring Problem

题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016:  https://vjudge.net/problem/HDU-1016 zoj 1457  :https://vjudge.net/problem/ZOJ-1457 题意翻译 输入正整数n,把整数1,2,...,n组成一个环,使得相邻两个整数之和均为素数.输出时,从整数1开始逆时针排列.同一个环恰好输出一次.. 多组数据,读入到EOF结束. 第i组数据输出前加上一

uva 524 prime ring problem——yhx

  Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 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

HDU1016 Prime Ring Problem(深度优先搜索)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27488 Accepted Submission(s): 12248 Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1,

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2

hdu1016 Prime Ring Problem dfs 素数打表

意思是给你一个数n,要构成一个素数环,这个素数由1-n组成,它的特征是选中环上的任意一个数字i,i与它相连的两个数加起来都分别为素数,满足就输出. 这个题的做法和hdu1015做法差不多都是使用dfs 回溯.不同之处在于这个要全部搜索,而hdu1015只需要搜索第一组就可以. 其次在这个题目中使用素数打表的方式简化素数判定,在一定情况下也是对效率有所提高的. Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limi

HDU 1016 Prime Ring Problem (素数筛+DFS)

题目链接 题意 : 就是把n个数安排在环上,要求每两个相邻的数之和一定是素数,第一个数一定是1.输出所有可能的排列. 思路 : 先打个素数表.然后循环去搜..... 1 //1016 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 6 using namespace std ; 7 8 bool vis[21]; 9 int prime[42] ,cs[21]; 10 int n ; 11