joj 1305 Prime Ring Problem 经典深搜

joj 1305 Prime Ring Problem 经典深搜

A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 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


很经典的深搜,启发
1,预处理非常重要,
(1),先求出所有的prime数最大 16+16,bool isprime[32],
(2)由于已经给出范围,我们可以先求出1-19的每个数下一个可能的取值,例如15,下一个可以取2,4,------两重for循环,可以避免搜索时重复判断。
2,用一个数组记录使输出的有序,op[n]与dfs(int begin,int n)中n对应
 if( bp[t] )
       {
        op[o] = t ;
        o ++ ;
        bp[t] = false ; 
        dfs( t , n );//
        o -- ;
        bp[t] = true ;
       } 

你可能感兴趣的:(joj 1305 Prime Ring Problem 经典深搜)