Prime Ring Problem

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

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
 
Recommend
JGShining   |   We have carefully selected several similar problems for you:  1072 1372 1015 1258 1180 
 
这是我第一道接触的搜索题,当时参考学长的代码写,却始终不能理解代码的意思,于是就一遍一遍第敲,直到整个代码都背下来了,后来接触的搜索逐渐增加,终于理解了这道题的代码,每次看到这道题,就能想起当年苦逼的敲代码。。。
 
题意:输入一个数n,代表从1到n个数,排序排成一个环,使得相邻两个数之和为素数(第一个和最后一个数也要满足),如果有多组情况,必须按从小到大的顺序输出,
 
附上代码:
 
 #include <iostream>
using namespace std;
int a[]= {,},b[],sushu[],n; //a表示素数环中的数字,b用来标记,sushu标记是否是素数
void DFS(int s,int k) //s代表当前循环到第几个数,k标记此时是否是第一次进人深搜函数
{
int i,j;
if(s>n)
{
if(sushu[a[]+a[n]]) //最后一个数字和第一个数字的和
{
for(i=; i<=n; i++) //全部满足,输出这组数据的全部数
{
if(i>) cout<<" ";
cout<<a[i];
}
cout<<endl;
}
else return; //若最后一组数据不满足,则返回上一次循环
}
for(i=; i<=n; i++)
{
if(k)
for(j=; j<; j++) //第一次进来,所有的数字都可以使用
b[j]=;
if(sushu[i+a[s-]]&&b[i]) //i表示当前数字,a[s-1]表示上一个数字。两个数字之和为素数,且没有出现过i这个数字
{
a[s]=i; //s位置上的数字记录为i
b[i]=; //0表示为已使用
DFS(s+,); //一个满足条件,进入下一个数字的选择
b[i]=; //若返回上一层循环,标记已使用了的数字必须还原为未使用
}
}
}
int main()
{
int i,j,t=;
for(i=; i<; i++)
sushu[i]=;
sushu[]=sushu[]=;
for(i=; i<=; i++)
if(sushu[i])
for(j=i+i; j<; j+=i)
sushu[j]=; //素数打表,减少代码运行时间,素数为1,非素数为0
while(cin>>n)
{
cout<<"Case "<<t++<<":"<<endl;
DFS(,);
cout<<endl;
}
return ;
}

hdu 1016 Prime Ring Problem(dfs)的更多相关文章

  1. hdu 1016 Prime Ring Problem (dfs)

    一切见凝视. #include <cstdio> #include <iostream> #include <cstring> #include <algor ...

  2. hdu 1016 Prime Ring Problem(DFS)

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

  3. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  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(深度优先搜索)

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

  6. HDOJ-1016 Prime Ring Problem(DFS)

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

  7. HDU 1016 Prime Ring Problem (素数筛+DFS)

    题目链接 题意 : 就是把n个数安排在环上,要求每两个相邻的数之和一定是素数,第一个数一定是1.输出所有可能的排列. 思路 : 先打个素数表.然后循环去搜..... #include <cstd ...

  8. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  9. [HDU 1016]--Prime Ring Problem(回溯)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

随机推荐

  1. 使用openpyxl模块时出现错误: zipfile.BadZipFile: File is not a zip file

    通过Pycharm工具新建一个xlsx文件. 再通过openpyxl模块读取该表时,报错: zipfile.BadZipFile: File is not a zip file 如下所示: 解决办法: ...

  2. Linux 配置yum源(互联网)

    Linux 配置yum源(互联网) 环境:操作系统Redhat 7.5 1.卸载现有的yum源 rpm -qa|grep yum|xargs rpm -e --nodeps     #移除与原yum有 ...

  3. DOM修改元素的方法总结

    今天我们要谈谈DOM元素的修改(包括修改内容,属性,样式).修改内容的方法----3种:elem.innerHTML:获取或设置元素开始标签到结束标签之间的原始HTML代码片段:elem.textCo ...

  4. SpringMvc表单标签库

    HTML密码框 <td><form:label path="password">密码:</form:label></td><t ...

  5. PHPCMS快速建站系列之常用标签

    <span class="Nmore"><a href="/index.php?m=content&c=index&a=lists&am ...

  6. 使用 git 来管理 PCB 版本

    使用 git 来管理 PCB 版本 在传统的 PCB 版本管理是复制一份,再重命名,写上日期,写上修改日志. 自从接触了 git 后,发现 git 的版本管理完全可以胜任,且可以做的更好. 原来使用商 ...

  7. thinkphp5.0 模板包含文件

    在index.html里包含layout.html:{include file=“layout”}它这里是以绝对路径查找所包含的文件,默认是view目录下 在这种情况下,要在在index.html里包 ...

  8. [转]The Curse of Dimensionality(维数灾难)

    原文章地址:维度灾难 - 柳枫的文章 - 知乎 https://zhuanlan.zhihu.com/p/27488363 对于大多数数据,在一维空间或者说是低维空间都是很难完全分割的,但是在高纬空间 ...

  9. 【NS2】NS2中802.11代码深入理解—packet传输的流程(转载)

    如何传送一个封包(How to transmit a packet?)首先,我们要看的第一个function是在mac-802_11.cc内的recv( ),程式会先判断目前呼叫recv( )这个pa ...

  10. Centos7环境下搭建Nginx+Lua+Redis进行数据存取

    1.安装依赖环境 yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel 2.安装LuaJIT cd /usr/loca ...