题目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.
//

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
#define PRIME 13
#define MAX_SIZE 21
using namespace std;

int n;
int ans[MAX_SIZE];
bool used[MAX_SIZE];

int prime[]={2,3,5,7,11,13,17,19,23,29,31,37,41};
bool judge(int x){
    for(int i = 0 ; i < PRIME ; i++){
        if(prime[i]==x)
            return true;
    }
    return false;
}

void check(){
    if(!judge(ans[n]+ans[1])) return;
    for(int i = 1 ; i <= n ; i++){
        if(i!=1) printf(" ");
        printf("%d",ans[i]);
    }
    printf("\n");
}

void DFS(int num){
    if(num>1){
        if(!judge(ans[num] + ans[num - 1])) return;
    }
    if(num==n){
        check();
        return;
    }
    for(int i = 2 ; i <= n ; i++){
        if(!used[i]){
            used[i] = true;
            ans[num+1] = i;
            DFS(num+1);
            used[i] = false;
        }
    }
}

int main(){
    int kase = 0;
    while(scanf("%d",&n)!=EOF){
        kase++;
        memset(used,false,sizeof(used));
        ans[1] = 1;
        used[1]=true;
        printf("Case %d:\n",kase);
        DFS(1);
        printf("\n");
    }
    return 0;
}
/**************************************************************
    Problem: 1459
    User: zpfbuaa
    Language: C++
    Result: Accepted
    Time:590 ms
    Memory:1520 kb
****************************************************************/
时间: 2024-10-10 04:26:57

题目1459: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

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

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 step 4.3.2)Prime Ring Problem(n个数成环,输出两两之和为质数的所有情况)

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

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(回溯)

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