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

输入:

n (1 < n < 17).

输出:

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.

样例输入:
6
8
样例输出:
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

为了解决该问题,我们可以采用回溯法枚举每一个值。当第一个数位为1确定时,我们尝试放入第二个数,使其和1的和为素数,放入后再尝试放入第三个数,使其与第二个数的和为素数,直到所有的数全部被放入环中,且最后一个数与1的和也是素数,那么这个方案即为答案,输出;若在尝试放数的过程中, 发现当前位置无论放置任何之前未被使用的数均不可能满足条件,那么我们回溯 改变其上一个数,直到产生我们所需要的答案,或者确实不再存在更多的解。

#include "stdafx.h"
#include <stdio.h>
using namespace std;

int prime[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 };
int number[20];
bool hash[20];
int n;
bool isPrime(int num)
{
    for (int i = 0; i < 12;i++)
        if (num == prime[i])
            return true;
    return false;
}

void printArray()
{
    if (isPrime(number[n] + number[1]) == false)
        return;
    for (int i = 1; i <= n; i++)
    {
        if (i != 1)
            printf(" ");
        printf("%d", number[i]);
    }
    printf("\n");
}

void DFS(int num)
{
    if (num > 1 && isPrime(number[num] + number[num - 1]) == false)
        return;
    if (num == n)
    {
        printArray();
        return;
    }

    for (int i = 2; i <= n; i++)
    {
        if (hash[i] == false)
        {
            hash[i] = true;
            number[num + 1] = i;
            DFS(num + 1);
            hash[i] = false;
        }
    }
}

int main()
{
    int cas = 0;
    while (scanf("%d", &n) != EOF)
    {
        cas++;
        for (int i = 0; i < 20; i++)
            hash[i] = false;
        number[1] = 1;
        printf("Case %d:\n", cas);
        hash[1] = true;
        DFS(1);
        printf("\n");
    }

    return 0;
}

时间: 2024-12-04 22:11:28

Prime is problem - 素数环问题的相关文章

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

题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: // // 1459 Prime ring problem.cpp // Jobdu // // Created by PengFei_Zheng on 23/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #

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

A - 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 < 2

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

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)Total Submission(s): 63806    Accepted Submission(s): 27457 Problem Description A ring is compos

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

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)

题目链接 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

素数环 -- C++实现

问题描述:将从1到n这n个整数围成一个圆环,若其中任意2个相邻的数字相加, 结果均为素数,那么这个环就成为素数环. n=20时,下面的序列就是一个素数环: 1 2 3 4 7 6 5 8 9 10 13 16 15 14 17 20 11 12 19 18 英文名:Prime Ring Problem 解题分析: 对于这样的问题, 我的想法就是首先要判断每个数是否是素数, 接着继续判断如何是否相邻的两个数的和是素数. 代码: #include <iostream> #include <c