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的数全排列,将相邻两个数互为素数且第一个数为1,第一个数与最后一个数也互为素数的排列进行输出
 
 #include <stdio.h>
#include <string.h>
int n;
int vis[], a[];//数组vis记录改点是否被访问过,数组a记录符合条件的数
int judge(int s)//判断s是否是素数
{
int flag = ;
for(int i = ; i <= s/; i++)
{
if(s%i == )
{
flag = ;
break;
}
}
return flag;
}
void dfs(int s, int cnt)//利用深搜来解决该问题,cnt是将要往数组中放入第几个数,s是放入数组中的末端的数
{
int i, j;
if(cnt == n+ && judge(a[]+a[n]))//当放入数组中的数(cnt-1)等于n时,且数组第一个数与最后一个数也互为素数时,进行输出
{
for(j = ; j < n+; j++)
{
if(j != )
printf(" ");
printf("%d", a[j]);
}
printf("\n");
return ;
}
for(i = ; i <= n; i++)
{
if(judge(i + s) && !vis[i])//如果i与s互为素数并且i没有被访问过时,访问i,将i放入数组中
{
a[cnt] = i;
vis[i] = ;
dfs(i, cnt + );//i进行与它上一个数相同的操作,将要往数组中放入第cnt+1个数
vis[i] = ;//返回后,要将i标记为未访问 }
}
return ;
}
int main()
{
int num = ;
while(~scanf("%d", &n))
{
printf("Case %d:\n", num++);
memset(vis, , sizeof(vis));
vis[] = ;//将1标记为已访问
a[] = ;//将1放入数组中
dfs(, );
printf("\n");
}
return ;
}

hdoj 1016 Prime Ring Problem的更多相关文章

  1. HDOJ 1016 Prime Ring Problem素数环【深搜】

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...

  2. hdoj - 1258 Sum It Up && hdoj - 1016 Prime Ring Problem (简单dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1258 关键点就是一次递归里面一样的数字只能选一次. #include <cstdio> #inclu ...

  3. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  4. [HDU 1016]--Prime Ring Problem(回溯)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  5. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  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. HDU 1016 Prime Ring Problem(经典DFS+回溯)

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

  8. 杭电oj 1016 Prime Ring Problem

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

  9. hdu 1016 Prime Ring Problem(深度优先搜索)

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

随机推荐

  1. vba 笔记

    1.PlanWS5.Range("D5:E13").Copy   复制PlanWS5.Range("G5:H13").PasteSpecial Paste:=x ...

  2. Linux_10------Linux之shell编程------变量

    .-9 vim num.sh #! /bin/bash num1=$1 num2=$2 sum=$(($num1+$num2)) #变量sum是num1和num2的综合 echo $sum 执行 ./ ...

  3. php面向对象之final的应用

    final从英文字面上很容易理解,翻译成中文就是“最终的”之意.在php面向对象编程中,final的应用主要有两个作用:   1.使用final修饰的类,该不能被继承   01 <?php 02 ...

  4. OLE/COM 对象查看器 & OLE常用术语

    "OLE/COM Object Viewer"(OLE/COM 对象查看器)查看你系统上安装的所有 COM 对象时,是一个非常便利的工具. 它是 Windows 2000 资源套件 ...

  5. 现在开始(Do It Now)

    现在开始(Do It Now) 很多年前读大学的时候,我决定制定一个计划挑战自己:只用三个学期完成其他人通常花费四年的课程,能否毕业.这篇文章(此文为翻译)详细的说明了我在成功实现该目标过程中的所有时 ...

  6. c++多态

    #include <cstdio> using namespace std; class Base { public: virtual void A() { puts("Base ...

  7. select 选中 option的问题

    1.[可以实现 不推荐  适合多选] $("#organize_type").find("option:eq("+j+")").attr(& ...

  8. Linux下MySQL数据库常用基本操作 一

    1.显示数据库 show databases; 2.选择数据库 use 数据库名; 3.显示数据库中的表 show tables; 4.显示数据表的结构 describe 表名; 5.显示表中记录 S ...

  9. Git命令学习摘要

    1.git init  --初始化git项目 2.git status --查看项目的状态 3.git add filename --添加文件到项目 4.git diff filename --查看工 ...

  10. 自动验证是ThinkPHP

    自动验证是ThinkPHP模型层提供的一种数据验证方法,可以在使用create创建数据对象的时候自动进行数据验证. 数据验证有两种方式: 静态方式:在模型类里面通过$_validate属性定义验证规则 ...