相邻数字相加为质数

 

题意:

     

A ring is composed of n (even number) circles as shown in diagram. Put natural numbers $1, 2, \dots, 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 <= 16)

 

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.

 

You are to write a program that completes above process.

 

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

 

 

 

思路分析:

             哈!DFS!

             1、可以从第二个数开始放(题目要求第一个数为1),每次放一个数要跟前面的数相加,判断是否为质数。

           是:标记该数已经用过,可以继续往下放

           否:撤销标记,回溯上一个数

            2、判断越界:数字都用完了&&最后一个数跟第一个数相加也是一个质数

 

源代码:

       

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 int n;
 5 int pri[11] = { 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };    //质数表
 6 int ans[20];              //储存给的数
 7 int used[20];            //用来标记用过的数
 8 int is_pri(int a, int b)            //判断相加是否是质数
 9 {
10     int w = 0;
11     for (int i = 0; i < 10; i++)
12     {
13 
14         if (a + b == pri[i])
15             
16         
17             return 1;
18     }
19     return 0;
20 }
21 void dfs(int a)
22 {
23     
24     if (a == n && is_pri(ans[0], ans[n - 1]))        //结束条件
25     {
26         for (int i = 0; i < n; i++)
27         {
28             
29             if (i == n - 1)
30             {
31                 cout << ans[i];        //最后一个单独输出,要求格式。
32                 break;
33             }
34             cout << ans[i] << " ";
35         }cout << endl;
36     }
37     else
38     
39         for (int i = 2; i <= n; i++)
40           if (!used[i] && is_pri(i, ans[a - 1]))
41             {
42                 used[i] = 1;            //该数已经使用
43                 ans[a] = i;             //把i放进a位置
44                 dfs(a + 1);
45                 used[i] = 0;            //清除使用记录(不满足的数)
46             }
47 }
48 int main()
49 {
50 
51     memset(ans, 0, sizeof(ans));
52     memset(used, 0, sizeof(used));
53     ans[0] = 1;                       //题目要求,第一个数要为1.
54     int count = 0,k=0;
55     while (cin >> n)
56     {
57         if (k++)
58             cout << endl;
59         count++;
60         if (n == 0)
61             return 0;
62         
63         printf("Case %d:\n", count);
64         dfs(1);
65         
66     }
67 
68     return 0;
69 }

 

 

心得:

        瞬间发现DFS的题就是按照模板在做题。。。。嗯,细节还是要注意的!

 

 

 

 

 

 

 

 

 

 

posted @ 2015-07-30 20:16  白一  阅读(526)  评论(0编辑  收藏  举报