hdu1016Prime Ring Problem深搜

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
#include<stack>
#include<list>
#include<stdlib.h>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
using namespace std;
int n;
int prime[100];

void Prime()
{
    for(int i=2;i<=100;i++)
        for(int j=2;j<=100&&i*j<=100;j++)
        if(!prime[i]) prime[i*j]=1;
    prime[0]=1;prime[1]=1;
}

int vis[100];
int a[100];
void dfs(int x)
{
    if(x==n-1){
        for(int i=1;i<=n;i++){
            if(!vis[i]&&!prime[1+i]&&!prime[a[x-1]+i]){
                a[x]=i;
                printf("%d",1);
                for(int j=1;j<n;j++)
                    printf(" %d",a[j]);
                printf("\n");
            }
        }
        return ;
    }
    for(int i=1;i<=n;i++){
        if(!vis[i]&&!prime[a[x-1]+i]){
            a[x]=i;
            vis[i]=1;
            dfs(x+1);
            vis[i]=0;
        }
    }
}

int main()
{
    int t=0;
    Prime();
    while(~scanf("%d",&n)){
        printf("Case %d:\n",++t);
        memset(vis,0,sizeof(vis));
        a[0]=1;vis[1]=1;
        dfs(1);
        printf("\n");
    }
    return 0;
}

hdu1016Prime Ring Problem深搜

时间: 2024-08-02 11:44:31

hdu1016Prime Ring Problem深搜的相关文章

HDU 1016 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. Input n (0 < n

DFS——hdu1016Prime Ring Problem

一.题目回顾 题目链接: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

hdu1016Prime Ring Problem

 就是说,给你一个数n, 要你把1到n都连在一起成环. 每一个数不可反复, 且相连的两个数的和要是素数. 把全部情况输出来. 我是用dfs暴力出来的. 首先把素数打表, 然后每次顺时针预測下一个数, 由于这个数必需要是素数减去上一个数, 非常好枚举. 我的代码例如以下: #include<iostream> #include<cstring> #include<cstdlib> using namespace std; int map[30],num,prime[]

HDU 1016 Prime Ring Problem(深搜)

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

HDU-1016 Prime Ring Problem(DFS深搜+打表)

题目回顾(HDU-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

hdoj-1016-Prime Ring Problem【深搜】

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

hdu 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

(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 ACM 1016 Prime Ring Problem

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