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. JetBrains PyCharm 2017.2 字体放大缩小 功能

  2. Eclipse Git插件切换分支的时候不要Reset

    今天做了一件蠢事,我在当前分支上改了很多代码,后来切换分支的时候,有一个文件有冲突,eclipse提示这个文件冲突,我可以选择commit/stash/reset,我一看这个文件没什么关系,不需要提交 ...

  3. excel之VLOOKUP函数的使用

    VLOOKUP 函数是excel中比较常用的一个函数.该函数具有有四个参数: 1.查找值:指本表中的值,需要根据本表中的某一值在本表或其他表中查找我们想要获取的值就称为查找值. 2.数据表:指查找的范 ...

  4. linux安装软件报错: Can't locate ExtUtils/Embed.pm in @INC...

    安装snmp服务, 中间报错: Can't locate ExtUtils/Embed.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/l ...

  5. arcgis投影测试

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. HTML 实体字符

    有些字符,像(<)这类的,对HTML来说是有特殊意义的,所以这些字符是不允许在文本中使用的.要在HTML中显示(<)这个字符,我们就必须使用实体字符. 实体字符 有一些字符对HTML来讲是 ...

  7. 遇到的bug

    1  div出现莫名其妙的空白bug 之前写了一个后台管理系统,项目不小加上是改版,很多的js都是用的之前的,  bug多到自己都不想看, 其中有个是用iframe 框架加载表格页面,但是右边跟下边出 ...

  8. Mac下搭建python开发环境

    目录 1. 安装brew 2. 安装 mysql 3. 安装 pycharm 4. 安装python3.6 5. 安装virtualenvwrapper 6. 虚拟环境下安装mysqlclient 1 ...

  9. 总结 ESP8266 RTOS 开发环境搭建

    总结 ESP8266 RTOS 开发环境搭建 仔细看官方文档. 必须一步一步操作. 不要想当然,以为 make 就可以. 忽略编译警告,除非是错误. 工具链必须使用官方提供的. 多看看 Issues ...

  10. 部署zabbix3.2.7,升级到3.4、proxy部署

    一.Server安装 可以使用mysql.percona.mariadb等,本例使用mariadb.由于是事后整理,只截取部分命令. #yum install mariadb-server maria ...