回溯法——素数环C++实现

回溯法简介

回溯法按深度优先策略搜索问题的解空间树。首先从根节点出发搜索解空间树,当算法搜索至解空间树的某一节点时,先利用剪枝函数判断该节点是否可行(即能得到问题的解)。如果不可行,则跳过对该节点为根的子树的搜索,逐层向其祖先节点回溯;否则,进入该子树,继续按深度优先策略搜索。

回溯法的基本行为是搜索,搜索过程使用剪枝函数来为了避免无效的搜索。剪枝函数包括两类:

  • 1. 使用约束函数,剪去不满足约束条件的路径;
  • 2.使用限界函数,剪去不能得到最优解的路径。

问题的关键在于如何定义问题的解空间,转化成树(即解空间树)。解空间树分为两种:子集树和排列树。两种在算法结构和思路上大体相同。

素数环问题

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

C++代码

 

 

留下评论

您的电子邮箱地址不会被公开。 必填项已用*标注