Prime Ring Problem

HDU - 1016

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.

 

Inputn (0 < n < 20). 
OutputThe 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

  1. 6
  2. 8

Sample Output

  1. Case 1:
  2. 1 4 3 2 5 6
  3. 1 6 5 2 3 4
  4.  
  5. Case 2:
  6. 1 2 3 8 5 6 7 4
  7. 1 2 5 8 3 4 7 6
  8. 1 4 7 6 5 8 3 2
  9. 1 6 7 4 3 8 5 2
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<cstring>
  4. #include<queue>
  5.  
  6. using namespace std;
  7.  
  8. int n;
  9. int a[];
  10. int vis[];
  11. bool mark[];
  12.  
  13. void init() // 素数筛
  14. {
  15. for(int i = ; i < ; ++i)
  16. mark[i] = true;
  17.  
  18. for(int i = ; i < ; ++i)
  19. {
  20. if(mark[i] == true)
  21. {
  22. for(int j = i*i; j < ; j += i)
  23. {
  24. mark[j] = false;
  25. }
  26. }
  27. }
  28. }
  29.  
  30. // 边枚举边判断,不要最后一次性判断,会超时
  31. void dfs(int step)
  32. {
  33. if(step > )
  34. {
  35. if(mark[a[step-]+a[step-]] == false) // 判断最后两个数
  36. return;
  37. }
  38.  
  39. if(step == n+)
  40. {
  41. if(mark[a[n]+a[]] == false) // 判断最后一个数与第一个数
  42. return;
  43. for(int i = ; i < n; ++i)
  44. printf("%d ", a[i]);
  45. printf("%d\n", a[n]);
  46.  
  47. }
  48.  
  49. for(int i = ; i <= n; ++i)
  50. {
  51. if(!vis[i])
  52. {
  53. a[step] = i;
  54. vis[i] = ;
  55. dfs(step+);
  56. vis[i] = ;
  57. }
  58.  
  59. }
  60. }
  61.  
  62. int main()
  63. {
  64. init();
  65. int cas = ;
  66. a[] = ;
  67. while(scanf("%d", &n) != EOF)
  68. {
  69. printf("Case %d:\n", cas++);
  70. memset(vis, , sizeof(vis));
  71. dfs();
  72. printf("\n");
  73. }
  74.  
  75. return ;
  76. }

Prime Ring Problem HDU - 1016 (dfs)的更多相关文章

  1. Prime Ring Problem HDU - 1016

    A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle ...

  2. HDU 1016 Prime Ring Problem(经典DFS+回溯)

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

  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 (DFS)

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

  5. Hdu 1016 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. [HDU 1016]--Prime Ring Problem(回溯)

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

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

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

  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. thinkphp 快捷查询

    快捷查询方式是一种多字段查询的简化写法,可以进一步简化查询条件的写法,在多个字段之间用|分割表示OR查询,用&分割表示AND查询,可以实现下面的查询,例如: 大理石平台价格 一.不同字段相同的 ...

  2. poj 3660 Cow Contest (bitset+floyd传递闭包)

    传送门 解题思路 考试题,想到传递闭包了,写了个O(n^3)的,T了7个点...后来看题解是tm的bitset优化???以前好像没听过诶(我太菜了),其实也不难,时间复杂度O(n^3/32) #inc ...

  3. 自定义UICollectionViewLayout之CATransform3D

    1.自定义UICollectionViewLayout旋转效果 之前有自定义UICollectionViewLayout(适用于多个section),本文是一个对cell进行CATransform3D ...

  4. SPSS分析过程可自动化,你知道吗

    SPSS分析过程可自动化,你知道吗 在使用SPSS的过程中,有时候会遇到重复进行相同分析操作的情况,或者分析过程很复杂的情况. 这时候我们多么希望SPSS能够记住上一次的分析步骤,不要让我们重复的去点 ...

  5. scrapy中的ImagePipeline下载图片到本地、并提取本地的保存地址

    通过scrapy内置到ImagePipeline下载图片到本地 在settings中打开 ITEM_PIPELINES的注释,并在这里面加入 'scrapy.pipelines.images.Imag ...

  6. kuangbin带我飞QAQ 并查集

    1. POJ 2236 给出N个点,一开始图是空白的,两个操作,一个是增加一个点(给出坐标),一个是查询两个点间是否相通,当两点间的距离小于D或者两点通过其他点间接相连时说这两个点相通.并查集维护,每 ...

  7. Leetcode300. Longest Increasing Subsequence最长上升子序列

    给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4. 说 ...

  8. SVN 环境搭建

    安装配置 安装环境 #查看系统版本环境 [root@svn ~]# cat /etc/redhat-release CentOS release 6.7 (Final) [root@svn ~]# u ...

  9. ES6 class继承

    ES6 class继承 class类的继承 class可以通过extends关键字实现继承,这笔ES5的通过修改原型连实现继承要清晰和方便很多. class Point{ } class ColorP ...

  10. AdaBoost笔记之代码

    最近要做二分类问题,先Mark一下知识点和代码,参考:Opencv2.4.9源码分析——Boosting   以下内容全部转自此文 一 原理 二 opencv源码 1.先看构建Boosting的参数: ...