Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 74594    Accepted Submission(s): 31690

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.

Input

n (0 < n < 20).

Output

The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.

You are to write a program that completes above process.

Print a blank line after each case.

Sample Input

6

8

Sample Output

Case 1:

1 4 3 2 5 6

1 6 5 2 3 4

Case 2:

1 2 3 8 5 6 7 4

1 2 5 8 3 4 7 6

1 4 7 6 5 8 3 2

1 6 7 4 3 8 5 2

Source

Asia 1996, Shanghai (Mainland China)

题意大致就是说要找到由1~n组成的环,且相邻的数两两相加都要是素数,输出所有满足条件的序列。

AC代码如下:

#include<stdio.h>
#include<string.h>
int f[100]={0};
int ans[21],v[21],n;
void dfs(int i)
{
    int m;
    if(i==n&&f[ans[i-1]+ans[0]]==0) //找到8个满足条件的数时输出
    {
        for(m=0;m<n-1;m++)
            printf("%d ",ans[m]);
        printf("%d\n",ans[n-1]);
    }
    else                      
    {
        for(m=2;m<=n;m++)        //找未用过的数
        {
            if(v[m]==0)          //找到还没有用过的数
            {
                if(f[m+ans[i-1]]==0)  //如果满足相邻数和为素数
                {
                    v[m]=-1;     //标记已用过
                    ans[i++]=m;  //将m放进数组
                    dfs(i);      //找下一个数
                    v[m]=0;      //接触标记
                    i--;         //回溯
                }
            }
        }
    }
}
int main()
{
    int i,j,cases=0;
    //素数打表
    for(i=2;i<100;i++)
        if(f[i]==0)
            for(j=i;i*j<100;j++)
                f[i*j]=1;
    
    while(scanf("%d",&n)!=EOF)
    {
        cases++;
        ans[0]=1;
        printf("Case %d:\n",cases);
        memset(v,0,sizeof(v));
        dfs(1);
        printf("\n");
    }
    return 0;
}
---------------------
作者:yongtaozheng
来源:CSDN
原文:https://blog.csdn.net/Twinkle_sone/article/details/95123292
版权声明:本文为博主原创文章,转载请附上博文链接!

hdu1016 Prime Ring Problem【素数环问题(经典dfs)】的更多相关文章

  1. Hdu 1016 Prime Ring Problem (素数环经典dfs)

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

  2. 题目1459:Prime ring problem(素数环问题——递归算法)

    题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  3. HDOJ 1016 Prime Ring Problem素数环【深搜】

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...

  4. Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏

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

  5. hdu1016 Prime Ring Problem(DFS)

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

  6. HDU1016 Prime Ring Problem(DFS回溯)

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

  7. Prime is problem - 素数环问题

    题目描述: A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each ...

  8. HDU1016 Prime Ring Problem (回溯 + 剪枝)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5398684.html 题意: 给你一个数字N(N <= 20),要求你把这N个数组成一个环,环内的数字不能重复,左右 ...

  9. hdu1016 Prime Ring Problem

    dfs,用全局数组和变量保存并更新当前状态. 答案可以直接在搜索结束时打印,n为奇数时方案数为0. acm.hdu.edu.cn/showproblem.php?pid=1016 #include & ...

随机推荐

  1. Building a Service Mesh with HAProxy and Consul

    转自:https://www.haproxy.com/blog/building-a-service-mesh-with-haproxy-and-consul/ HashiCorp added a s ...

  2. struct udphdr

    udphdr结构包含在/usr/src/linux/include/linux/udp.h struct udphdr { __u16 source; __u16 dest; __u16 len; _ ...

  3. 安装关系型数据库MySQL 安装大数据处理框架Hadoop

    作业要求来自:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3161 1.Hadoop的介绍 Hadoop最早起源于Nutch.Nut ...

  4. SQL Delta实用案例介绍

    概述 本篇文章主要介绍SQL DELTA的简单使用.为了能够更加明了的说明其功能,本文将通过实际项目中的案例加以介绍. 主要容 Ÿ   SQL DELTA 简介 Ÿ   创建SQL DELTA项目 Ÿ ...

  5. 【mybatis源码学习】ResultMap查询结果映射

    一.ResultMap包含的元素 constructor - 用于在实例化类时,注入结果到构造方法中 idArg - ID 参数:标记出作为 ID 的结果可以帮助提高整体性能 arg - 将被注入到构 ...

  6. 搭建redis cluster集群服务

    redis 5.0以下为ruby编写,运行命令时需要安装ruby,而5.0以上则为c编写,可直接安装后运行.因此本文使用redis5.0.5 1.编写配置文件 在 /home 下新建 redis-cl ...

  7. [转]IntelliJ IDEA 2019 上手

    原文地址:https://www.jianshu.com/p/77f81d5fcf02 一.聊一聊Java IDE 作为程序员,经常会看到这么一类的话题:文本编辑器与IDE哪家强.常见的文本编辑器如E ...

  8. Java基础 变量名的开头可以使用$

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  9. 《精通CSS第3版》(3)可见格式化模型+(4)网页排版

  10. typescript - 8.命名空间

    基础 略. https://www.tslang.cn/docs/handbook/namespaces.html 多文件中的命名空间(一个文件分解为几个) 现在,我们把Validation命名空间分 ...