一、题目回顾

题目链接: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. webpack4——打包html报错解决

    ①先引入html-webpack-plugin插件,然后在终端下载 npm install --save-dev html-webpack-plugin ②我的文件结构 ③修改webpack.dev. ...

  2. TortoiseSVN SendRpt.exe not found解决方案

    重启了Explorer.exe即可.这里也补充下简单的重启Explorer.exe的方法:打开任务管理器,找到“Windows资源管理器”,右键--重新启动. 或者,右键--结束任务,然后点击 文件- ...

  3. LeetCode 翻转链表

    基本思路 从元首节点之后每次取一个节点,并将节点接到元首节点前面 代码实现 /** * Definition for singly-linked list. * struct ListNode { * ...

  4. C#下载局域网共享文件夹中的文件

    在公司的局域网内部,有个主机,共享了几个文件夹给下面的客户机使用. 想要利用这个文件夹上传最新的winform程序版本,每次运行exe的时候检测局域网的软件版本达到更新exe的目的. 这里有个例子,是 ...

  5. Python3 operator模块关联代替Python2 cmp() 函数

    Python2 cmp() 函数 描述 cmp(x,y) 函数用于比较2个对象,如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1. Python ...

  6. less学习三---父选择器

    引用父选择器需要用到“&”符号 &运算符表示嵌套规则的父选择器,并且在修改类或伪类选择器的应用中非常普遍 ul{ li{ &:nth-child(2) a { color: r ...

  7. SQL优化之语句优化

    昨天与大家分享了SQL优化中的索引优化,今天给大家聊一下,在开发过程中高质量的代码也是会带来优化的 网上关于SQL优化的教程很多,但是比较杂乱.整理了一下,写出来跟大家分享一下,其中有错误和不足的地方 ...

  8. 继上一篇bootstrap框架(首页)弄的资讯页面

    还是和上一篇首页一样给出每一步的注解: 做的有点简单,但是,以后还是会加深的.毕竟是初学者嘛! <html lang="zh-cn">   <head>   ...

  9. 让UltraEdit-32成为Delphi 7编译器的工具设置

    UltraEdit-32编译Delphi的工具设置 {================================================}Dcc32 命令行(&C):C:\Pro ...

  10. 码云配置webhooks自动触发拉取代码

    webhooks的使用 码云和github的钩子叫webhooks 每次您 push 代码后,都会给远程 HTTP URL 发送一个 POST 请求 码云项目管理页面的webhooks设置: http ...