一、题目回顾

题目链接: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. vue-resource+iview上传文件取消上传

    vue-resource+iview上传文件取消上传 子组件: <template> <div class="upload-area-div"> <U ...

  2. 选择排序_C语言_数组

    选择排序_C语言_数组 #include <stdio.h> void select_sort(int *); int main(int argc, const char * argv[] ...

  3. iOS开发- 获取本地视频文件

    下面具体介绍下实现过程.先看效果图.图1. 未实现功能前, iTunes截图 图2. 实现功能后, iTunes截图 图3. 实现功能后, 运行截图 好了, 通过图片, 我们可以看到实现的效果.功能包 ...

  4. js如何判断数据类型

    1.最常见的判断方法:typeof console.log(typeof a) ------------> string console.log(typeof b) ------------&g ...

  5. 用户交互input

    input() 函数 接收到的都是str,如果输入为数字,打印结果想进行运算,此时需要转义.语法:内容=input("提示信息")这里可以直接获取到用户输入的内容. a = inp ...

  6. Linux安装redis PHP安装Redis扩展 and基本命令

    一.安装redis 用超级管理员身份运行: $ mkdir /usr/local/redis #redis安装目录 $ cd /usr/local/src #安装包下载目录 $ wget http:/ ...

  7. 剑指offer—二维数组中的查找

    题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...

  8. Zabbix源码安装部署

    zabbix源码部署安装 参考文档:https://www.zabbix.com/documentation/4.0/manual/installation/install ​ https://www ...

  9. android 自定义滑动按钮

    第一接触公司项目就让我画页面,而且还涉及到我最讨厌的自定义view  但是没办法,讨厌也必须要做啊,经过百度上资源的查找,终于写出了一个滑动控件.废话不多说,上代码. package com.eton ...

  10. 发送post请求几种常见content-type类型

    application/x-www-form-urlencoded 这应该是最常见的 POST 提交数据的方式了.浏览器的原生 form 表单,如果不设置 enctype 属性,那么最终就会以 app ...