一、题目回顾

题目链接:Prime Ring Problem

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
 
题意:输入一个数n,把1到n的自然数放到一个环里,保证相邻的两个数的和是素数。
 
 
二、解题思想
  • dfs+素数打表
  • 经典的一道DFS

三、代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 50;
int a[maxn];
int b[maxn];
int n,prime[2*maxn];
bool vis[maxn]; //素数打表,相当于int prime[40]={0,1,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0};
void isprime()
{
int i,j;
for(i = 0;i<50;i++)
prime[i] = 1;
prime[0] = prime[1] = 0;
for(i = 2;i<50;i++)
{
if(prime[i])
for(j = i+i;j<50;j+=i)
prime[j] = 0;
}
}
//深搜
void dfs(int x) //x:当前搜索第几个数
{
if(x==n+1 && prime[b[n]+b[1]]){ //满足条件了,就输出来
for(int i=1;i<n;i++) printf("%d ",b[i]);
printf("%d\n",b[n]);
return;
}
for(int i=2;i<=n;i++){
if(!vis[i] && prime[a[i]+b[x-1]]){ //此数未用并且与上一个放到环中的数相加是素数
vis[i] = 1; //标记
b[x] = a [i]; //放进数组
dfs(x+1);
vis[i] = 0; //退去标记
}
} }
int main()
{
int kase = 1;
isprime();
while(cin>>n && (n>0&&n<20)){
for(int i=1;i<=n;i++)
a[i] = i;
memset(vis,0,sizeof(vis));
b[1] = 1;
printf("Case %d:\n",kase++);
dfs(2);
printf("\n");
}
return 0;
}

DFS——hdu1016Prime Ring Problem的更多相关文章

  1. 素数环:NYOJ--488--dfs||hdu-1016-Prime Ring Problem

    /* Name: NYOJ--488--素数环 Author: shen_渊 Date: 15/04/17 15:30 Description: DFS,素数打个表,37以内就够用了 */ #incl ...

  2. 搜索专题: HDU1016Prime Ring Problem

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

  3. hdu1016Prime Ring Problem

     就是说,给你一个数n, 要你把1到n都连在一起成环. 每一个数不可反复, 且相连的两个数的和要是素数. 把全部情况输出来. 我是用dfs暴力出来的. 首先把素数打表, 然后每次顺时针预測下一个数 ...

  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. HDU1016 Prime Ring Problem(DFS回溯)

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

  6. HDU 1016 Prime Ring Problem (DFS)

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

  7. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  8. UVa 524 Prime Ring Problem(DFS , 回溯)

    题意  把1到n这n个数以1为首位围成一圈  输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的  n最大为16  利用回溯法 边生成边推断  就要快非常多了 #inc ...

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

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

随机推荐

  1. Zabbix: Database Monitor Installation

    1.     Install ODBC MS SQL Connector On Zabbix Server    https://support.zabbix.com/browse/ZBX-6839 ...

  2. CRS

    CRS是集群就绪服务(cluster ready service)的简称,主要负责集群中的资源管理以及OCR管理.为了与10gR2集群管理软件名称crs区分,这里用CRSD代替CRS.相关概念:--资 ...

  3. redis 高性能应用

    redis可达到512M/per key 512M=512*1024KB=512*1024*1000B=512*1024*1000*8bit=40亿+ 化整为零40亿,也就是说一位代表一个用户,40亿 ...

  4. Redux学习笔记-----基础部分

    Redux的基本原则 唯一数据源(应用的状态数据应该只存储在唯一的一个store上): 保持状态只读(不能直接修改Store的状态,而是应该通过派发一个action对象来完成) 数据改变只能通过纯函数 ...

  5. PTA 最多删除3个字符(DP) - 30分

    给定一个全部由小写英文字母组成的字符串,允许你至多删掉其中 3 个字符,结果可能有多少种不同的字符串? 输入格式: 输入在一行中给出全部由小写英文字母组成的.长度在区间 [4, 1] 内的字符串. 输 ...

  6. 使用Python对SQLite数据库操作

    SQLite是一种嵌入式数据库,它的数据库就是一个文件.由于SQLite本身是C写的,而且体积很小,所以,经常被集成到各种应用程序中,甚至在IOS和Android的APP中都可以集成. Python内 ...

  7. docker API 配置与使用

    在网上看到一大堆乱乱七八招的博客,很多都不能用,我根据这些天踩的坑来总结一下吧 首先!怎么配置 docker API 两种方法 在/etc/sysconfig/docker文件里加一行OPTIONS= ...

  8. 使用MapReduce读取HBase数据存储到MySQL

    Mapper读取HBase数据 package MapReduce; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hba ...

  9. Ubuntu设置代理服务器

    由于公司网络的原因,apache的网站访问不了,对于需要经常访问apache网站查看文档的我,最近想了一种方法,在自己的阿里云服务器上搭建一个代理服务器.经过查资料,最终决定使用TinyProxy. ...

  10. 网站标题被篡改成北京赛车、PK10的解决处理办法

    客户网站于近日被跳转到赌博网站,打开后直接跳转到什么北京赛车,PK10等内容的网站上去,客户网站本身做了百度的推广,导致所有访问用户都跳转到赌博网站上去,给客户带来很大的经济损失,再一个官方网站的形象 ...