HDOJ——1016(经典dfs搜索题)

题目要求在一个由n个圈组成的环形结构中,从1开始顺时针和逆时针填充自然数,使得相邻两数之和为素数。第一个圈的数字固定为1,解决方案需要满足字典序排列并进行DFS搜索。在搜索过程中,通过标记避免重复路径,并在递归过程中检查相邻数字和是否为素数。注意最后的回溯操作,保持book数组的状态正确。
摘要由CSDN通过智能技术生成

1016:

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.

大意:

一个环由n个圈组成的如图表中所示。把自然数1~n个数单独的放进每个圆圈里去,并且在两个相邻圆圈的数字之和应该是一个素数。

注意:第一个圆的数字应该总是1。


输入Input:

n (0 < n < 20).

输出:

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.

输出在如样例中所示。每一行展示的数字从1开始顺时针方向直至逆时针方向。输出的这些数字必须满

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值