A - 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.

Inputn (0 < n < 20). 
OutputThe 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 OutpuCase 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

题目意思:给你一个20以内的正整数n,将1到n围成一个圈,每个数都和旁边数的和为素数输出所以可能的情况

解法:普通的应用DFS的题目,注意跳出条件就可以了;

 1 #include <iostream>
 2 #include <string.h>
 3 using namespace std;
 4
 5 const int  MAX = 100;
 6 int n;
 7 int visit[MAX];
 8 int Map[MAX];
 9 int a[MAX] = {3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61};
10
11 void print()
12 {
13     for(int i = 1;i <=n;i++)
14         if(i==1)
15         cout<<Map[i];
16         else
17         cout<<" "<<Map[i];
18     cout<<endl;
19 }
20
21 int pand(int x)
22 {
23     for(int i = 0;i<16;i++)
24         if(x==a[i])
25             return 1;
26     return 0;
27 }
28
29 void dfs(int x,int ti)
30 {
31     if(ti==n)
32     {
33         if(pand(x+1))
34         {
35             print();
36         }
37     }
38
39     for(int i = 2;i <= n;i++)
40     {
41         if(visit[i]==0 &&pand(i+x))
42         {
43             visit[i] = 1;
44             Map[ti+1] = i;
45             dfs(i,ti+1);
46             visit[i] = 0;
47         }
48     }
49
50 }
51
52 int main()
53 {
54     int N=0;
55     while(cin>>n)
56     {
57         cout<<"Case "<<++N<<":"<<endl;
58         if(n==1)
59         {
60             cout<<1<<‘\n‘<<endl;
61         continue;
62         }
63
64         memset(visit,0,sizeof(visit));
65         visit[1] = 1;
66         Map[1] = 1;
67         dfs(1,1);
68         cout<<endl;
69
70     }
71
72     return 0;
73 }




时间: 2024-10-24 18:04:46

A - Prime Ring Problem (素数圈)的相关文章

题目1459:Prime ring problem(素数环问题——递归算法)

题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: // // 1459 Prime ring problem.cpp // Jobdu // // Created by PengFei_Zheng on 23/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #

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

hdu 1016 Prime Ring Problem (素数环)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 题目大意:输入一个n,环从一开始到n,相邻两个数相加为素数. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int visit[22],q[100],n; 6 int sushu(int n) 7 { 8 int i; 9 for (i=2

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

hdu1016 Prime Ring Problem dfs 素数打表

意思是给你一个数n,要构成一个素数环,这个素数由1-n组成,它的特征是选中环上的任意一个数字i,i与它相连的两个数加起来都分别为素数,满足就输出. 这个题的做法和hdu1015做法差不多都是使用dfs 回溯.不同之处在于这个要全部搜索,而hdu1015只需要搜索第一组就可以. 其次在这个题目中使用素数打表的方式简化素数判定,在一定情况下也是对效率有所提高的. Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limi

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

传送门: 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)Total Submission(s): 63806    Accepted Submission(s): 27457 Problem Description A ring is compos

UVa 524 Prime Ring Problem(DFS , 回溯)

题意  把1到n这n个数以1为首位围成一圈  输出所有满足任意相邻两数之和均为素数的所有排列 直接枚举排列看是否符合肯定会超时的  n最大为16  利用回溯法 边生成边判断  就要快很多了 #include<cstdio> using namespace std; const int N = 50; int p[N], vis[N], a[N], n; int isPrime(int k) { for(int i = 2; i * i <= k; ++i) if(k % i == 0)

[2016-02-19][UVA][524][Prime Ring Problem]

UVA - 524 Prime Ring Problem Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the

[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