题目链接: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 ;
}

最新文章

  1. 探索c#之递归APS和CPS
  2. PostgreSQL Replication之第一章 理解复制概念(1)
  3. 【皇甫】☀Struts_第一节课
  4. 与众不同 windows phone (35) - 8.0 新的启动器: ShareMediaTask, SaveAppointmentTask, MapsTask, MapsDirectionsTask, MapDownloaderTask
  5. 详解Java中ArrayList、Vector、LinkedList三者的异同点
  6. 【读书笔记】读《JavaScript DOM 编程艺术-第2版》
  7. 告诉你一个真实的OpenStack:都谁在用,用来干什么?
  8. redhat 5.4 下rabbitMQ单机安装.md
  9. listview必须设置数据适配器才能显示出来
  10. Python的list用法笔记
  11. 【BZOJ1778】[Usaco2010 Hol]Dotp 驱逐猪猡
  12. Python学习(一) —— 基础
  13. 二叉树的基础题目学习(EPI)
  14. Flask内置URL变量转换器
  15. [UE4]修改射击方向
  16. 镜像仓库管理:与Portus不得不说的那些事
  17. android checkbox样式
  18. > ch05-01
  19. bzoj 4358: permu 莫队
  20. Ionic Js一:上拉菜单(ActionSheet)

热门文章

  1. JS学习之事件冒泡
  2. Android 开发笔记“Application 理解”
  3. (IOS)关于Xcode的架构(Architectures)设置
  4. 我的Fedora环境
  5. java学习之坦克大战游戏
  6. HashMap和HashTable 学习
  7. JavaScript 数字相关的转换和方法
  8. python之高阶函数编程
  9. c# 流程控制
  10. Android 屏幕尺寸知识