深搜回溯-素数环-1459-jobdu

题目1459:Prime ring problem
时间限制:2 秒内存限制:128 兆特殊判题:否提交:747解决:306
题目描述:
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.

输入:
n (1 < n < 17).
输出:
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.
样例输入:
6
8
样例输出:
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
提示:
用printf打印输出。

 

 

 

时间: 2024-11-04 22:40:04

深搜回溯-素数环-1459-jobdu的相关文章

HDOJ1181变形课 深搜回溯

变形课 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 18474 Accepted Submission(s): 6663 Problem Description 呃--变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规律:如果咒语是以a

穷举搜索:回溯与深搜

计算机常用算法大致有两大类,一类叫蛮力算法,一类叫贪心算法,前者常使用的手段就是搜索,对全部解空间进行地毯式搜索,直到找到指定解或最优解. [建立解空间] 问题的解应该如何描述,如何建立?借助图论的思想,我们可以用图来描述,图的定义为G,由顶点集和边集构成,顶点即实实在在的数 据.对象,而边可以抽象为关系,即顶点间的关系,这种关系不一定非要在数据结构上表现出来,用数据结构的语言来描述,如果关系是一对一,则为线性表,如果 关系是一对多,则为树,如果关系是多对多,则为图,如果完全没有关系,则为集合.

深搜算法实例:老鼠走迷宫(一)

这个是简单的深搜,应该输入深搜中抛砖型的,联系下代码,回顾一下深搜的思想. 本题的要求是,在开始点(1,1)和终点(5,5)放一只老鼠,让老鼠找到一条路径走出去(暂时不考虑最短路径),找到后输出路径. 最简单的想法就是对于上下左右四个进行刨根型的搜索,找到就返回输出,进入死胡同了就原路返回,找最近的有其他路径的点,继续搜索,知道找出为止. 下面是代码部分. #include <stdio.h> #include <stdlib.h> #define SUCCESS 1 #defin

acm-邻接矩阵能进行深搜么。现在会邻接表的深搜,还用把邻接矩阵转化为邻接表吗

问题描述 邻接矩阵能进行深搜么.现在会邻接表的深搜,还用把邻接矩阵转化为邻接表吗 邻接矩阵如果能进行的话.麻烦大神给点代码啊.小白..如题...... 解决方案 void dfs(int i){ visited[i]=1; for(int j=0;j<n;j++){ if(G[i][j]==1&&visited[j]==0){ dfs(j); } } } 解决方案二: 这是用邻接矩阵求最小生成树的,题主看下能否帮的上忙,如果能帮的上,希望可以采纳 #include #include

c++-UVa1374快速幂运算迭代深搜法 ,C++初学者,求优化方法

问题描述 UVa1374快速幂运算迭代深搜法 ,C++初学者,求优化方法 这是我的代码,优化过几次,但是还是很慢很慢,求大神给优化途经,就是在我的代码的进行优化 #include #include #include #include using namespace std; bool search(int,set&,int dep,int); int MAX=1; int tempMax; int main() { sets;int n; for(n=2;n!=1000;++n) { s.cle

格式错误-素数环 问题PE 求解~~

问题描述 素数环 问题PE 求解~~ 输出格式错误了,不知道题目对格式的具体要求 附上代码: #include #include #include using namespace std; int prime[40]={1,1,0,0,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1,1,0,1,0,1,1,1,1,1,0,1,1}; int n; int a[20]; int mark[20]; int cnt,flag; void dfs(int

HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)

Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approa

HDOJ/HDU 1015 Safecracker(深搜)

Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein safe behind a painting in the second-floor library. Klein safes are extremely rare; most of them, along with Klein and his factory, were destroyed in Wo

ZOJ(ZJU) 1002 Fire Net(深搜)

Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through which to shoot. The fo