Prime Ring Problem HDU - 1016 (dfs)
Prime Ring Problem
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
- 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
- #include<iostream>
- #include<stdio.h>
- #include<cstring>
- #include<queue>
- using namespace std;
- int n;
- int a[];
- int vis[];
- bool mark[];
- void init() // 素数筛
- {
- for(int i = ; i < ; ++i)
- mark[i] = true;
- for(int i = ; i < ; ++i)
- {
- if(mark[i] == true)
- {
- for(int j = i*i; j < ; j += i)
- {
- mark[j] = false;
- }
- }
- }
- }
- // 边枚举边判断,不要最后一次性判断,会超时
- void dfs(int step)
- {
- if(step > )
- {
- if(mark[a[step-]+a[step-]] == false) // 判断最后两个数
- return;
- }
- if(step == n+)
- {
- if(mark[a[n]+a[]] == false) // 判断最后一个数与第一个数
- return;
- for(int i = ; i < n; ++i)
- printf("%d ", a[i]);
- printf("%d\n", a[n]);
- }
- for(int i = ; i <= n; ++i)
- {
- if(!vis[i])
- {
- a[step] = i;
- vis[i] = ;
- dfs(step+);
- vis[i] = ;
- }
- }
- }
- int main()
- {
- init();
- int cas = ;
- a[] = ;
- while(scanf("%d", &n) != EOF)
- {
- printf("Case %d:\n", cas++);
- memset(vis, , sizeof(vis));
- dfs();
- printf("\n");
- }
- return ;
- }
Prime Ring Problem HDU - 1016 (dfs)的更多相关文章
- 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 ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- HDU 1016 Prime Ring Problem (DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Hdu 1016 Prime Ring Problem (素数环经典dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1016 Prime Ring Problem(dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- [HDU 1016]--Prime Ring Problem(回溯)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- HDU 1016 Prime Ring Problem(素数环问题)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- hdu 1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- thinkphp 快捷查询
快捷查询方式是一种多字段查询的简化写法,可以进一步简化查询条件的写法,在多个字段之间用|分割表示OR查询,用&分割表示AND查询,可以实现下面的查询,例如: 大理石平台价格 一.不同字段相同的 ...
- poj 3660 Cow Contest (bitset+floyd传递闭包)
传送门 解题思路 考试题,想到传递闭包了,写了个O(n^3)的,T了7个点...后来看题解是tm的bitset优化???以前好像没听过诶(我太菜了),其实也不难,时间复杂度O(n^3/32) #inc ...
- 自定义UICollectionViewLayout之CATransform3D
1.自定义UICollectionViewLayout旋转效果 之前有自定义UICollectionViewLayout(适用于多个section),本文是一个对cell进行CATransform3D ...
- SPSS分析过程可自动化,你知道吗
SPSS分析过程可自动化,你知道吗 在使用SPSS的过程中,有时候会遇到重复进行相同分析操作的情况,或者分析过程很复杂的情况. 这时候我们多么希望SPSS能够记住上一次的分析步骤,不要让我们重复的去点 ...
- scrapy中的ImagePipeline下载图片到本地、并提取本地的保存地址
通过scrapy内置到ImagePipeline下载图片到本地 在settings中打开 ITEM_PIPELINES的注释,并在这里面加入 'scrapy.pipelines.images.Imag ...
- kuangbin带我飞QAQ 并查集
1. POJ 2236 给出N个点,一开始图是空白的,两个操作,一个是增加一个点(给出坐标),一个是查询两个点间是否相通,当两点间的距离小于D或者两点通过其他点间接相连时说这两个点相通.并查集维护,每 ...
- Leetcode300. Longest Increasing Subsequence最长上升子序列
给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4. 说 ...
- SVN 环境搭建
安装配置 安装环境 #查看系统版本环境 [root@svn ~]# cat /etc/redhat-release CentOS release 6.7 (Final) [root@svn ~]# u ...
- ES6 class继承
ES6 class继承 class类的继承 class可以通过extends关键字实现继承,这笔ES5的通过修改原型连实现继承要清晰和方便很多. class Point{ } class ColorP ...
- AdaBoost笔记之代码
最近要做二分类问题,先Mark一下知识点和代码,参考:Opencv2.4.9源码分析——Boosting 以下内容全部转自此文 一 原理 二 opencv源码 1.先看构建Boosting的参数: ...