HDU 1016 素数环(DFS)

              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.

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

如果是奇数直接No Answer,搜索的时候加上判断当前项和前一项和是否是素数的剪枝。

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4
 5 int n,a[21],p[41]={0};
 6 bool used[21];
 7 bool flag,key;
 8
 9 void init()
10 {
11     for(int i=2;i*i<40;i++)
12     for(int j=i+i;j<40;j+=i)
13     p[j]=1;
14 }
15
16 bool check(void)
17 {
18     for(int i=0;i<n;i++)
19     if(p[a[i]+a[(i+1)%n]])
20     return false;
21     return true;
22 }
23
24 void dfs(int cur)
25 {
26     int i;
27     if(cur==n)
28     {
29         if(check())
30         {
31             flag=true;
32             for(i=0;i<n-1;i++)
33             printf("%d ",a[i]);
34             printf("%d\n",a[i]);
35         }
36         return;
37     }
38     for(i=2;i<=n;i++)
39     {
40         if(!used[i]&&!p[i+a[cur-1]])
41         {
42             a[cur]=i;
43             used[i]=true;
44             dfs(cur+1);
45             used[i]=false;
46         }
47     }
48 }
49
50 int main()
51 {
52     int i,j;
53     init();
54     int kase=0;
55     while(scanf("%d",&n)!=EOF)
56     {
57         a[0]=1,flag=false,used[1]=true;
58         memset(used,0,sizeof(used));
59         if(n%2&&n!=1)
60         {
61             printf("No Answer\n\n");
62             continue;
63         }
64         printf("Case %d:\n",++kase);
65         dfs(1);
66         if(!flag)
67         printf("No Answer\n");
68         printf("\n");
69     }
70     return 0;
71 }
时间: 2024-08-28 06:57:23

HDU 1016 素数环(DFS)的相关文章

HDU 1016 素数环(深搜)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25134 Accepted Submission(s): 11222 Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1,

HDU 1016 素数环问题

题目大意: 给定1-n这n个数,组成以1开头的素数环,保证相邻两个数相加均为素数 题目用dfs搜索再回溯,这样碰到不成立的立刻退出递归,就减少了很多步骤,不然暴力来就是n!次复杂度,肯定是超时的 每次添入数据都要判断是否相邻数相加为素数,所以我们可以提前打个素数表,这样使自己判断素数更加方便 1 #include <cstdio> 2 #include <cstring> 3 4 using namespace std; 5 6 #define N 22 7 int n , a[N

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

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

hdu 1016 Prime Ring Problem (dfs)

一切见注释. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; bool vis[22]; int n; int ans[22]; int top; bool isprime(int x)//判断素数 { for(int i=2;i<x;i++) if(x%i==0)return false; return

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存储放置的数 /

【dfs】hdu 1016 Prime Ring Problem

[dfs]hdu 1016 Prime Ring Problem 题目链接 刚开始接触搜索,先来一道基本题目练练手. 注意对树的深度进行dfs dfs过程中注意回退!!! 素数提前打表判断快一些 参考代码 /*Author:Hacker_vision*/ #include<bits/stdc++.h> #define clr(k,v) memset(k,v,sizeof(k)) using namespace std; const int _max=1e3+10;//素数打表 int n,pr

hdu 1016 Prime Ring Problem (简单DFS)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25700    Accepted Submission(s): 11453 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

HDU 1016 Prime Ring Problem(DFS)

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