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. HDU 1724 自适应辛普森法

    //很裸的积分题,直接上模板 #include<stdio.h> #include<math.h> int aa, bb; //函数 double F(double x){ - ...

  2. vs code 配置c/c++环境

    1. 编译 通过 code-runner插件 运行编译 安装code-runner后在settings.json中找到code-runner.executorMap,可以看到其中的cpp 文件运行方式 ...

  3. 扩展 Microsoft.Owin.Security

    微软在 OWIN 框架中对 OAuth 认证的支持非常好, 使用现有的 OWIN 中间件可以做到: 使用 Microsoft.Owin.Security.OAuth 搭建自己的 OAuth2 服务端, ...

  4. 2018-3-4-dotnet-设计规范-·-结构体定义

    title author date CreateTime categories dotnet 设计规范 · 结构体定义 lindexi 2018-03-04 17:48:44 +0800 2018-2 ...

  5. POJ 1845 (洛谷 :题目待添加)Sumdiv

    约数和 题目描述 给出a和b求a^b的约数和. 输入格式: 一行两个数a,b. 输出格式: 一个数表示结果对 9901 的模. Input 2 3 Output 15 SB的思路: 这是一道典型的数论 ...

  6. 2018-12-25-win10-uwp-release-因为-Entry-Point-Not-Found-无法启动

    title author date CreateTime categories win10 uwp release 因为 Entry Point Not Found 无法启动 lindexi 2018 ...

  7. TCPThree_C杯 Day2

    T1 我已经被拉格朗日插值蒙蔽了双眼,变得智障无比. 第一反应就是拉格朗日插值,然后就先放下了它. 模数那么小,指数那么大,这是一套noip模拟题,拉格朗日,你脑袋秀逗了? 无脑暴力20分贼开心. 正 ...

  8. acm一路走来的体验和想法

    2017年参加天梯赛 作为大一萌新去参加人生第一场正规的比赛,学校拿钱砸的.我呢,是十分激动的,是时候检验下自己的编程水平了,也有一丝慌张,怕给团队抹黑. 然后呢,晒一下自己成绩. 我本来觉得我和他们 ...

  9. request header....

    root@xxx# curl -i --get --include 'http://ali-barcode.showapi.com/barcode?code=6938166920785' -H 'Au ...

  10. 反射技术的入口 获取类的Class信息

    package com.sxt.reflect; import com.sxt.reflect.entity.Student; /* * 获取类的Class信息 */ public class Tes ...