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. 用Linux自带的Logrotate来管理日志

    Logrotate是由cron控制,cron在规定的时间执行 " logrotate  /etc/logrotate.conf "命令.将对象日志进行转储,删除,压缩等操作... ...

  2. http请求的开销

    很多人都说要减少http请求,可关注为什么要减少请求的人却少很多,本文是对我在几篇博客以及知乎上看到的内容的整理. http请求头的数据量 每次请求都会带上一些额外的信息进行传输,当请求的资源很小,比 ...

  3. python基础知识7——迭代器,生成器,装饰器

    迭代器 1.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外,迭代器 ...

  4. -moz-transform: rotate(-5deg);

    目前越来越多的浏览器兼容CSS3标准了,就连IE浏览器老大哥也开始向CSS3低头,微软宣布IE9浏览器支持更多的CSS3属性,IE9更注重 HTML5标准.不过CSS3里有一个使对象旋转的属性tran ...

  5. api服务端接口安全

    api服务端接口安全性解析 http://blog.csdn.net/tenfyguo/article/details/8225279 常用的基于token的实现方案 http://blog.csdn ...

  6. Android 学习 (一)

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 } p. ...

  7. jquery实现淡入淡出

    fade方法包括四个: (1)fadeIn(speed,callback):淡入的方法,speed代表淡入的速度,可以是slow,fast,毫秒,不填等 例如: $(document).ready(f ...

  8. PHP用户名用星号处理

    PHP用户名用*号处理: 用户名:英文.中文.中英文混合的.中英文字符混合的 处理为:首字母和末尾保留,中间用*号代替(一个字符直接显示,两个字符:张*,三个以上字符:宋*丹) 首先判断字符中是否包含 ...

  9. 新博客地址: kuangbin.org

    RT. 买了新域名,原来的kuangbin.net已经废了,数据已经移动到了kuangbin.org

  10. String.prototype运用

    1.去掉字符串前后空格 String.prototype.ltrim = function () { return this.replace(/^\s+/, ""); } Stri ...