题目链接: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. Windows NTService 后台框架封装

    对于后台运行的程序,比如基于C/S架构的服务器.各种监控系统的程序.或者是程序额外的功能需要后台运行来实现,在Windows平台中,服务经常会用到,这种对于需要24×7运行的程序是必备的,至少本人经常 ...

  2. JS 利用window.open实现post方式的参数传递

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. Android 使用 intent 实现简单登陆页面

    前言 第一个 Android 程序,应该有些纪念的意义吧~ 主页面布局 给 Button 添加响应函数:android:onClick="login" public void lo ...

  4. YII框架下实现密码修改

    YII2 实现修改密码功能 主要难点: 1.密码加密 YII2对密码加密生成的结果是不同的,即用相同的初始密码在不同时间得到的加密结果不同,所以我们不能用常用的方法去验证密码是否正确(将密码加密后与数 ...

  5. [LeetCode]题解(python):007-Reverse Integer

    题目来源: https://leetcode.com/problems/reverse-integer/ 题意分析: 这道题目很简单,就是将一个数反转,123变321,-123变321. 题目思路: ...

  6. [转]IE和Firefox兼容性问题及解决方法

    今天测试代码时,发现不少IE可以运行的ajax,但在FF中报错.IE和Firefox(火狐)在JavaScript方面的不兼容及统一方法总结如下: 1.兼容firefox的 outerHTML,FF中 ...

  7. Windows Azure 网站 (WAWS) 中的服务器端包含 (SSI)

     编辑人员注释:本文章由 Windows Azure 网站团队的项目经理Erez Benari 撰写. Windows Azure 网站客户普遍关心的一个问题是关于我们对服务器端包含(Server ...

  8. libCurl的文件上传

    最近在需要使用curl的上传功能,使用libCurl来实现.因此,先使用curl命令操作,然后再使用libCurl实现. 基于Http协议的文件上传的标准方法是: 基于POST Form的文件上传  ...

  9. linux下c/c++方式访问curl的帮助手册

    自:http://blog.chinaunix.net/u1/47395/showart_1768832.html 有个业务需求需要通过curl 代理的方式来访问外网 百度了一把,测试可以正常使用.记 ...

  10. HDU 1720 A+B Coming

    #include <string> #include <cstdio> #include <iostream> using namespace std; int c ...