[HDOJ1016]Prime Ring Problem

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016

原题:

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这N个数字连成环以后两两相加均为素数的所有情况。

非常经典的DFS题。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
int n;
int cir[21];
int prime[41];
int vis[21];
int isPrime(int x)
{
    int i;
    int sqx = sqrt((float)x);
    for(i = 2; i <= sqx; i++)
    {
        if(x % i == 0)
        {    
            return 0;
        }
    }
    return 1;
}

void dfs(int x)
{
    int i;
    if(x == n && prime[cir[1]+cir[n]])    //make a circle
    {
        for(i = 1; i < n; i++)
        {
            printf("%d ", cir[i]);
        }
        printf("%d\n", cir[n]);
        return ;
    }
    for(i = 2; i <= n; i++)
    {
        if(vis[i] == 0 && prime[cir[x]+i])
        {
            cir[x+1] = i;
            vis[i] = 1;
            dfs(x+1);
            vis[i] = 0;    //reset
        }
    }
}

int main()
{
    int i, Case = 1;
    for(i = 1; i < 41; i++)    //list primes.
    {
        if(isPrime(i))
        {
            prime[i] = 1;
        }
    }

    while(scanf("%d", &n) != EOF && n)
    {
        printf("Case %d:\n", Case++);
        memset(vis, 0, sizeof(vis));
        cir[1] = 1;
        vis[1] = 1;
        dfs(1);
        printf("\n");
    }
    return 0;
}
时间: 2024-10-19 15:23:30

[HDOJ1016]Prime Ring Problem的相关文章

HDOJ-1016 Prime Ring Problem【DFS】

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

HDOJ1016 Prime Ring Problem(DFS深层理解)

Prime Ring Problem 时间限制: 2000ms               内存限制: 32768KB                HDU       ID: 1016 题目描述 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

HDOJ-1016 Prime Ring Problem(DFS)

http://acm.hdu.edu.cn/showproblem.php?pid=1016 题意:输入n,代表有一个包含n个节点的环,在环中的节点中填入1,2...n-1,n,要求填入的数与左边的数之和,与右边的数之和,都为素数 输出所有符合要求的环(第一个数总为1) 用DFS模拟,从第2位到第n位依次选取一个与上一个选取的数之和为素数的数 直到选取完第n个数,判断第n个数和1之和是否为素数,是则输出,否则不进行操作,判断完毕返回上一层 直到遍历完所有情况 *:因为素数必然是奇数,所以一条符合

hdu 1016 Prime Ring Problem DFS解法 纪念我在杭电的第一百题

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

[2016-02-19][UVA][524][Prime Ring Problem]

UVA - 524 Prime Ring Problem Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the

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

[ACM] 1016 Prime 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 first circle

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

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