题目链接: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[];
int prime[];
int vis[];
int isPrime(int x)
{
    int i;
    int sqx = sqrt((float)x);
    for(i = ; i <= sqx; i++)
    {
        if(x % i == )
        {    
            return ;
        }
    }
    return ;
} void dfs(int x)
{
    int i;
    if(x == n && prime[cir[]+cir[n]])    //make a circle
    {
        for(i = ; i < n; i++)
        {
            printf("%d ", cir[i]);
        }
        printf("%d\n", cir[n]);
        return ;
    }
    for(i = ; i <= n; i++)
    {
        if(vis[i] == && prime[cir[x]+i])
        {
            cir[x+] = i;
            vis[i] = ;
            dfs(x+);
            vis[i] = ;    //reset
        }
    }
} int main()
{
    int i, Case = ;
    for(i = ; i < ; i++)    //list primes.
    {
        if(isPrime(i))
        {
            prime[i] = ;
        }
    }     while(scanf("%d", &n) != EOF && n)
    {
        printf("Case %d:\n", Case++);
        memset(vis, , sizeof(vis));
        cir[] = ;
        vis[] = ;
        dfs();
        printf("\n");
    }
    return ;
}

[HDOJ1016]Prime Ring Problem的更多相关文章

  1. HDOJ1016 Prime Ring Problem(DFS深层理解)

      Prime Ring Problem                                                                       时间限制: 200 ...

  2. HDOJ-1016 Prime Ring Problem(DFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1016 题意:输入n,代表有一个包含n个节点的环,在环中的节点中填入1,2...n-1,n,要求填入的数与左边的数 ...

  3. uva 524 prime ring problem——yhx

      Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural ...

  4. hdu 1016 Prime Ring Problem(DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. 杭电oj 1016 Prime Ring Problem

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. hdu 1016 Prime Ring Problem(深度优先搜索)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU1016 Prime Ring Problem(DFS回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. HDU 1016 Prime Ring Problem (DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. JAVA 集合List,数组,Set,Map,直接的相互转换

    Java集合转换[List<-->数组.List<-->Set.数组<-->Set.Map-->Set.Map-->List] //List--> ...

  2. linux异步通信之epoll【转】

    转自:http://www.cnblogs.com/snake-hand/archive/2012/08/13/2636229.html 1.简介 epoll是linux提供的一种异步的I/O通知方式 ...

  3. 正确配置Linux系统ulimit值的方法【转】

    转自:http://www.cnblogs.com/ibook360/archive/2012/05/11/2495405.html 在Linux下面部署应用的时候,有时候会遇上Socket/File ...

  4. 华硕本本重装系统后出现can not open file c:\RECOVERY.DAT

    华硕本本重装系统后出现can not open file c:\RECOVERY.DAT很多网友会觉得困惑,不知道为什么会这样,下面我就为大家来解决这个问题,方法一: 这个问题就出在华硕自带系统都是装 ...

  5. Redis 转

    Redis 简介 Redis实践 Redis命令总结

  6. 4.1HTML和Bootstrap css精华

    1.HTML 2.理解Bootstrap HTML元素告诉浏览器,他要表现的是什么类型的内容,当他们不提供任何关于如何显示内容的信息.如何显示内容的信息,由CSS提供. 本书仅包含足够的信息,让你查看 ...

  7. HDU 1827:Summer Holiday(强连通)

    http://acm.hdu.edu.cn/showproblem.php?pid=1827 思路:强连通分量缩点后找入度为0的点,然后对于属于该强连通分量的找一个最小耗费的入口. #include ...

  8. php的内存分配还是很智能的

    <?php echo memory_get_usage().PHP_EOL;$a = 1;$b = $a;echo memory_get_usage().PHP_EOL; <?php ec ...

  9. gets()和getchar()还有getch()的区别

    getch()和getchar()区别:1.getch(): 所在头文件:conio.h 函数用途:从控制台读取一个字符,但不显示在屏幕上例如: char ch;或int ch: getch();或c ...

  10. smb.conf

    [home]comment = All Printerspath = /homevalid users = yorkwriteable=yespublic=yesbrowseable = yescre ...