hdoj-1016-Prime Ring Problem【深搜】

Prime Ring Problem

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

Total Submission(s): 34347 Accepted Submission(s): 15188

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

Asia 1996, Shanghai (Mainland China)

Recommend

JGShining | We have carefully selected several similar problems for you:10101312
1072 1242 1175

<pre name="code" class="html">#include<stdio.h>
#include<string.h>
bool prim[44],visit[22];
int b[22];
int n;
void f(){
	prim[2]=prim[3]=prim[5]=prim[7]=prim[11]=prim[13]=prim[17]=prim[19]=1;
	prim[23]=prim[29]=prim[31]=prim[37]=prim[41]=1;
}
void dfs(int op){
	if(op==n){
		if(!prim[b[op-1]+b[0]])	return;
		printf("%d",b[0]);
		for(int i=1;i<n;++i)
		   printf(" %d",b[i]);
		printf("\n");
		return;
	}
	for(int i=2;i<=n;++i){
		if(!visit[i]){
			if(prim[b[op-1]+i]){
				b[op]=i;visit[i]=1;dfs(op+1);
			}
			visit[i]=0;
	   }
	}
}
int main(){
	f();
	int ncas=0;
	while(~scanf("%d",&n)){
		printf("Case %d:\n",++ncas);
		memset(visit,0,sizeof(visit));
		b[0]=1;dfs(1);
		printf("\n");
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-02 11:44:27

hdoj-1016-Prime Ring Problem【深搜】的相关文章

HDU 1016 Prime Ring Problem 深搜

 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

hdoj 1016 Prime Ring Problem 【DFS】

策略如题 链接 http://acm.hdu.edu.cn/showproblem.php?pid=1016 代码: #include<stdio.h> #include<string.h> int prime[25] = {1, 1}, n, vis[25]; //vis作用:标记是否用过 int a[25]; void f() //找出来前20的素数 判定为0 { for(int i = 2; i <= 24; i ++){ if(prime[i] == 0) for(i

hdoj 1016 Prime Ring Problem

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

HDOJ 1016 Prime Ring Problem (素数环问题)

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

hdoj - 1258 Sum It Up &amp;&amp; hdoj - 1016 Prime Ring Problem (简单dfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1258 关键点就是一次递归里面一样的数字只能选一次. 1 #include <cstdio> 2 #include <cstring> 3 4 int n,t; 5 int b[15],c[15]; 6 bool flag; 7 void dfs(int k,int sum,int l) 8 { 9 if(sum==t) 10 { 11 for(int i=0;i<l-1;i++) 12 p

HDU 1016 Prime Ring Problem (素数筛+DFS)

题目链接 题意 : 就是把n个数安排在环上,要求每两个相邻的数之和一定是素数,第一个数一定是1.输出所有可能的排列. 思路 : 先打个素数表.然后循环去搜..... 1 //1016 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 6 using namespace std ; 7 8 bool vis[21]; 9 int prime[42] ,cs[21]; 10 int n ; 11

hdu1016Prime Ring Problem深搜

#include<stdio.h> #include<iostream> #include<string.h> #include<queue> #include<stack> #include<list> #include<stdlib.h> #include<algorithm> #include<vector> #include<map> #include<set> us

HDU 1016 Prime Ring Problem 题解

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

[ACM] 1016 Prime Ring Problem (深度优先搜索)

Prime Ring Problem 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

HDU 1016 Prime Ring Problem --- 经典DFS

思路:第一个数填1,以后每个数判断该数和前一个数想加是否为素数,是则填,然后标记,近一步递归求解. 然后记得回溯,继续判断下一个和前一个数之和为素数的数. /* HDU 1016 Prime Ring Problem --- 经典DFS */ #include <cstdio> #include <cstring> int n; bool primer[45], visit[25]; //primer打素数表,visit标记是否访问 int a[25]; //数组a存储放置的数 /