hdu1016 Prime Ring Problem【素数环问题(经典dfs)】

简介: hdu1016 Prime Ring Problem【素数环问题(经典dfs)】

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;
}
目录
相关文章
|
虚拟化 UED Windows
Vmware workstation 安装教程
Vmware workstation 安装教程
427 1
|
移动开发 算法 调度
【贪心算法】一文让你学会“贪心”(贪心算法详解及经典案例)
贪心算法是一种非常常见的算法,它的简单和高效性使其在实际应用中被广泛使用。 贪心算法的核心思想是在每一步都采取当前状态下最优的选择,而不考虑未来可能产生的影响。虽然贪心算法不能保证总是得到最优解,但在很多情况下,它可以获得很好的结果。 本篇文章将介绍贪心算法的基本概念和一些经典应用,以及如何通过贪心算法来解决一些实际问题。希望通过本文的阅读,读者可以对贪心算法有更加深刻的理解,并能够在实际问题中应用贪心算法来得到更好的解决方案。 让我们暴打贪心算法吧!
6446 0
|
7月前
|
前端开发 JavaScript 安全
7.6K Star Shadcn Admin:颜值与实力并存的后台管理系统,前端开发者的新宠!
"基于 Shadcn UI 和 Vite 打造的现代化管理后台,开箱即用的响应式设计 + 无障碍访问,让后台开发从未如此优雅!" —— 来自 GitHub 7.6K 星认证
1585 26
|
10月前
|
人工智能 Serverless API
测评报告:零门槛、轻松部署您的专属 DeepSeek 模型
### 测评报告:零门槛、轻松部署您的专属 DeepSeek 模型 DeepSeek 是一款强大的推理模型,尤其擅长数学、代码和自然语言处理任务。由于算力限制,官方服务支持不稳定,阿里云提供了四种云上调用及部署方案,包括基于百炼调用API、PAI平台部署、函数计算部署和GPU云服务器部署。测评显示,PAI平台最具灵活性和易用性,但整体方案在文档指引、部署时间和成本方面仍有改进空间。建议优化文档和技术支持,提升用户体验。
606 3
|
机器学习/深度学习 数据采集 人工智能
探索人工智能中的深度学习模型优化策略
探索人工智能中的深度学习模型优化策略
494 13
|
编译器 C语言 C++
详解C/C++动态内存函数(malloc、free、calloc、realloc)
详解C/C++动态内存函数(malloc、free、calloc、realloc)
2370 1
|
算法 数据挖掘
文献解读-Consistency and reproducibility of large panel next-generation sequencing: Multi-laboratory assessment of somatic mutation detection on reference materials with mismatch repair and proofreading deficiency
Consistency and reproducibility of large panel next-generation sequencing: Multi-laboratory assessment of somatic mutation detection on reference materials with mismatch repair and proofreading deficiency,大panel二代测序的一致性和重复性:对具有错配修复和校对缺陷的参考物质进行体细胞突变检测的多实验室评估
118 6
文献解读-Consistency and reproducibility of large panel next-generation sequencing: Multi-laboratory assessment of somatic mutation detection on reference materials with mismatch repair and proofreading deficiency
|
前端开发 UED 容器
CSS基础-文本样式:颜色、字体、大小、对齐
【6月更文挑战第8天】本文介绍了CSS中设置文本颜色、字体、大小和对齐的基础知识。通过color属性改变文本颜色,使用font-family设置字体,用font-size调整大小,text-align控制对齐方式。注意浏览器兼容性、文本溢出和响应式设计的问题。提供的代码示例展示了这些属性的用法,帮助创建更具吸引力的网页。
355 6
|
存储 C语言 C++
从C语言到C++_31(unordered_set和unordered_map介绍+哈希桶封装)(上)
从C语言到C++_31(unordered_set和unordered_map介绍+哈希桶封装)
201 3