题目链接: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. 三层架构与MVC的区别

    我们平时总是将混为一谈,殊不知它俩并不是一个概念.下面我来为大家揭晓我所知道的一些真相. 首先,它俩根本不是一个概念. 三层架构是一个分层式的软件体系架构设计,它可适用于任何一个项目. MVC是一个设 ...

  2. 薛非《品悟C-抛弃C程序设计中的谬误与恶习》读后感part1【转】

    薛非<品悟C-抛弃C程序设计中的谬误与恶习>读后感part1 作者:宝贝孙秀楠﹣大连程序员 发表于2012年10月5日由admin 出处:http://sunxiunan.com/?p=2 ...

  3. UINavigationController(转)

    UINavigationController是IOS编程中比较常用的一种容器view controller,很多系统的控件(如UIImagePickerViewController)以及很多有名的AP ...

  4. ImagXpress中如何修改Alpha通道方法汇总

    ImagXpress支持处理Alpha通道信息来管理图像的透明度,Alpha通道支持PNG ,TARGA和TIFF文件,同时还支持BMP和ICO文件.如果说保存的图像样式不支持Alpha通道,就将会丢 ...

  5. V4L2应用程序框架--一【转】

    本文转载自:http://blog.csdn.net/tommy_wxie/article/details/11369667 V4L2是V4L的升级版本,linux下视频设备程序提供了一套接口规范. ...

  6. 【python】__future__模块

    转自:http://www.jb51.net/article/65030.htm Python的每个新版本都会增加一些新的功能,或者对原来的功能作一些改动.有些改动是不兼容旧版本的,也就是在当前版本运 ...

  7. 2015.01.15(android AsyncTask)

    参考网址:http://www.cnblogs.com/devinzhang/archive/2012/02/13/2350070.html /* * Params 启动任务执行的输入参数,比如HTT ...

  8. linux-exp 工具+小技巧

    # 工具篇 # pwntools ,gdb-peda ROPgadget-tool . EDB ## pwntools获取.安装和文档帮助 ## - pwntools: github可以搜索到 htt ...

  9. javaWeb 使用 jsp 和 javaBean 实现计算器功能

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  10. Linux异步IO【转】

    转自:http://blog.chinaunix.net/uid-24567872-id-87676.html Linux® 中最常用的输入/输出(I/O)模型是同步 I/O.在这个模型中,当请求发出 ...