题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016

Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

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
 



Source
 



Recommend
JGShining   |   We have carefully selected several
similar problems for you:  1010 1241 1312 1072 1242 
 
题目大意:有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环。
     为了简便起见,我们规定每个素数环都从1开始。有多组测试数据,每组输入一个n(0<n<20),n=0表示输入结束。
     输出每组第一行输出对应的Case序号,从1开始。如果存在满足题意叙述的素数环,从小到大输出。
 
解题思路:回溯的思想就是了,一个dfs搞定,最坑爹的是,每输完一组末尾都要加上换行,我没加结果提交wa,明明是pe,各种改,
     彻底无爱了,Orz~~~
 
代码如下:
 #include <iostream>
#include <cstring>
using namespace std; #define maxn 40
int vis[], x[], T, n;
int prime[maxn] = { , , };
void init()
{
int i, j;
for (i = ; i <= maxn; i++){
if (!prime[i]){
for (j = ; i*j <= maxn; j++)
prime[i*j] = ;
}
}
} void dfs(int cur){
if (cur == n&&!prime[ + x[n - ]]){
for (int i = ; i < n; i++){
if (i) cout << ' ';
cout << x[i];
}
cout << endl;
}
else for (int i = ; i <= n; i++){
if (!vis[i] && !prime[i + x[cur - ]]){
x[cur] = i;
vis[i] = ;
dfs(cur + );
vis[i] = ;
}
}
} int main(){
init();
while (cin >> n){
cout << "Case " << ++T << ':' << endl;
if (n == )
cout << << endl;
else if (n & )
cout << endl;
else{
memset(vis, , sizeof(vis));
x[] = ;
dfs();
}
cout << endl;//没加这一句pe来个wa,我也是醉了,各种改,无爱了~~~~
}
return ;
}

[HDU 1016]--Prime Ring Problem(回溯)的更多相关文章

  1. HDU 1016 Prime Ring Problem (回溯法)

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

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

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

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

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

  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(深度优先搜索)

    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. Hdu 1016 Prime Ring Problem (素数环经典dfs)

    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 A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...

随机推荐

  1. Spring学习之Aop的各种增强方法

    AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5一样的注解,但并没有使用AspectJ的 ...

  2. shopnc b2b2c如何开启伪静态??

    shopnc b2b2c开启伪静态的方法 一. windows环境下 1.先下载isapi rewrite插件,安装,然后我们把根目录下面的htaccess.txt那么修改成.htaccess即可. ...

  3. hdu 3333 Turing Tree

    题目链接 给n个数, m个询问, 每次询问输出区间内的数的和, 相同的数只计算一次. 数组里的数是>-1e9 <1e9, 可以把它离散以后用莫队搞... #include <iost ...

  4. 卡特兰数(Catalan)简介

    Catalan序列是一个整数序列,其通项公式是 h(n)=C(2n,n)/(n+1) (n=0,1,2,...) 其前几项为 : 1, 1, 2, 5, 14, 42, 132, 429, 1430, ...

  5. 宣布在日本地区正式发布 Windows Azure

     昨天,我与 Microsoft 日本的集团副总裁 Yasuyuki Higuchi 一同站在台上,宣布在两个新地区正式发布 Windows Azure:日本东部和日本西部.能够亲自见证 Micr ...

  6. HDU 3350 #define is unsafe

    题目大意:给定一个只含有MAX和+操作的式子,求加法运行了多少次,其中MAX使用宏定义. 题解:注意一个规律,对于MAX(A,B)其中A中加a次,B中加b次若A>B,则加a*2+b次,否则a+b ...

  7. $.getJSON(url,function success(){})回调函数不起作用

    有个问题好久没有解决,就是: $.getJSON(url,function success(){}) 其中的回调函数,总也不执行. 以前也做过,但那都是CTRL+C,CTRL+V,也没有细想. 目标就 ...

  8. java项目组会议纪要

    上周五项目经理开例会让我记录会议纪要,以下是我记录的纪要.给大家分享一下! 一.时间:2014年04月25日 二.地点:研发部 三.人物:xx,xx,xx 四.内容(相关项目的一些事项): 1.对待需 ...

  9. c语言实现tree数据结构

    该代码实现了tree的结构.依赖dyArray数据结构.有first一级文件夹.second二级文件夹. dyArray的c实现參考这里点击打开链接  hashTable的c实现參考这里点击打开链接 ...

  10. uva311 - Packets(贪心)

    题目:311 - Packets 题目大意:给出1*1, 2*2,3 *3, 4*4, 5*5, 6*6的箱子的个数,如今有若干个6*6的箱子,问最少用多少个箱子能够将给定的箱子都装进去. 解题思路: ...