题目链接: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. html规范总结

    这个链接有规范的html 描述:http://nec.netease.com/standard 相关链接: 1. http://www.zhangxinxu.com/wordpress/2010/09 ...

  2. mirrors

    http://mirrors.163.com/http://mirrors.aliyun.com/http://mirrors.aliyun.com/centos/7.2.1511/os/x86_64 ...

  3. JProfiler入门笔记

    看链接:http://blog.csdn.net/chendc201/article/details/22897999 收集一下.

  4. linux死锁检测的一种思路【转】

    转自:http://www.cnblogs.com/mumuxinfei/p/4365697.html 前言:  上一篇博文讲述了pstack的使用和原理. 和jstack一样, pstack能获取进 ...

  5. Android 标签的主题样式

    Android平台定义的主题样式: android:theme="@android:style/Theme.Dialog"   将一个Activity显示为对话框模式 •andro ...

  6. iOS中 项目开发易错知识点总结

    点击return取消textView 的响应者 - (BOOL)textFieldShouldReturn:(UITextField *)textField { [_contactTextFiled  ...

  7. Mysql 如何做双机热备和负载均衡

    MySQL数据库没有增量备份的机制,但它提供了一种主从备份的机制,就是把主数据库的所有的数据同时写到备份数据库中.实现MySQL数据库的热备份. 下面是具体的主从热备份的步骤:假设主服务器A(mast ...

  8. 22、JSON/jQuery上

      1)掌握JSON及其应用 2)了解jQuery的背景和特点 3)理解js对象和jQuery对象的区别 4)掌握jQuery九类选择器及应用(上)   声明:今天服务端我们使用Struts2技术 一 ...

  9. PHP学习当中遗漏的知识点

    一, 当双引号中包含变量时,变量会与双引号中的内容连接在一起: 当单引号中包含变量时,变量会被当做字符串输出. <?php $love = "I love you!"; $s ...

  10. MongoDB在windows自启动

    D:\mongodb\Server\3.0\bin>mongod --logpath D:\mongodb\log\mongo.log --logappend--dbpath D:\mongod ...