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. [vivado系列]Vivado软件的下载

    时间:2016.10.27 ------------------ 前言:我们知道vivado软件是用于xilinx的7系列及以上器件的FPGA开发工具. 随着版本的不断更新,也变得越来越庞大.臃肿! ...

  2. 安装Maven

    下载进入官网下载页面:http://maven.apache.org/download.html我用的是windows,下载apache-maven-3.2.5-bin.zip: Maven 3.2. ...

  3. seajs的CMD模式的优势以及使用

    之前有一篇博客非常详细的介绍了sea.js的加载流程,以及源代码实现,链接地址:http://www.cnblogs.com/chaojidan/p/4123980.html 这篇博客我主要讲下sea ...

  4. PowerDesigner使用教程

    PowerDesigner是一款功能非常强大的建模工具软件,足以与Rose比肩,同样是当今最著名的建模软件之一.Rose是专攻UML对象模型的建模工具,之后才向数据库建模发展,而PowerDesign ...

  5. AFNetworking图片缓存问题

    AFNetworking网络库已经提供了很好的图片缓存机制,效率是比较高的,但是我发现没有直接提供清除缓存的功能,可项目通常都需要添加 清除功能的功能,因此,在这里我以UIImageView+AFNe ...

  6. 网络编程——基于TCP协议的Socket编程,基于UDP协议的Socket编程

    Socket编程 目前较为流行的网络编程模型是客户机/服务器通信模式 客户进程向服务器进程发出要求某种服务的请求,服务器进程响应该请求.如图所示,通常,一个服务器进程会同时为多个客户端进程服务,图中服 ...

  7. Content is not allowed in prolog ---UTF-8 无bom

  8. String、StringBuffer、StringBuilder的区别

    在日常开发过程中String字符串估计是被用到最多的变量了,最近看了一些String.StringBuffer和StringBuilder的东西,三者都可以对字符串进行操作,他们究竟有什么区别,以及适 ...

  9. PHP实现CSV大文件数据导入到MYSQL数据库

    <?php $db_host="192.168.1.10"; $db_user="root"; $db_psw="11111"; $d ...

  10. 2016CCPC 合肥--最大公约数//每一年通向它的路上,多少人折戟沉沙,多少人功败垂成,有人一战成名,有人从头再来。

    有这样一个有关最大公约数的函数:函数 f(x, y): { c=0 当 y>0: { c +=1 t = x % y x = y y = t } 返回 c * x * x} 给出三个正整数n,m ...