码迷,mamicode.com
首页 > 其他好文 > 详细

素数环,暴力剪枝

时间:2019-07-02 22:44:01      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:name   mat   alt   cst   sqrt   tis   you   ring   sample   

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.

 

剪枝还是很暴力的,学到了

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <set>
 4 #include <cmath>
 5 #include <cstring>
 6 using namespace std;
 7 set<int> prime;
 8 int record[23];
 9 bool is[23];
10 void dfs(int now,int n)
11 {
12     if(now==n-1&&prime.find(1+record[n-1])!=prime.end())
13     {
14         for(int i=0;i<n-1;i++)
15         printf("%d ",record[i]);
16         printf("%d",record[n-1]);
17         printf("\n");
18         return;
19     }
20     else
21     {
22         if(record[now]%2==1)
23         {
24             for(int i=2;i<=n;i+=2)
25             {
26                 if(is[i]==false&&prime.find(i+record[now])!=prime.end())
27                 {
28                     record[now+1]=i;
29                     is[i]=true;
30                     dfs(now+1,n);
31                     is[i]=false;
32                 }
33             }
34         }
35         
36         if(record[now]%2==0)
37         {
38             for(int i=1;i<=n;i+=2)
39             {
40                 if(is[i]==false&&prime.find(i+record[now])!=prime.end())
41                 {
42                     record[now+1]=i;
43                     is[i]=true;
44                     dfs(now+1,n);
45                     is[i]=false;
46                 }
47             }
48         }
49     }
50 }
51 int main()
52 {
53     for(int i=2;i<=42;i++)
54     {
55         int flag=0;
56         for(int j=2;j<=sqrt(i);j++)
57         {
58             if(i%j==0)
59             {
60                 flag=1;
61                 break;
62             }
63         }
64         if(flag==0)
65         {
66         prime.insert(i);}
67     }
68     int mm=0;
69     int n;
70     while(scanf("%d",&n)!=EOF)
71     {
72         mm++;
73         printf("Case %d:\n",mm);
74         if(n%2==1)
75         {
76             printf("\n\n");
77         }
78         else
79         {
80            record[0]=1;
81            is[1]=true;    
82            dfs(0,n);
83            printf("\n");
84         }
85         
86     }
87 }

 

素数环,暴力剪枝

标签:name   mat   alt   cst   sqrt   tis   you   ring   sample   

原文地址:https://www.cnblogs.com/coolwx/p/11123530.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!