hdoj--1016--Prime Ring Problem(递归回溯)

Prime Ring Problem

                                                                            Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
                                                                                                Total Submission(s): 37129    Accepted Submission(s): 16363


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
今天帮同学写题解,顺便又写了一遍这道题直接上题解

这道题算是回溯中比较经典的一道,题目意思是说给定一个数字n,然后输出1--n这些数经过排列之后得到一个数列,数列中相邻的两个数和为素数(这里什么是素数就不说了),数列开始的第一位是1,并且此数列第一个数与最后一个数相加也为素数,

这道题中我们用a数组来记录得到的数据,v数组来标记对应的数字是否是用过。每次递归的时候我们传递的参数是找到的数字的个数,递归结束的标志就是找到了n位并且首位和末位的和是素数,或者找到了n位首位和末位和不为素数(代码中没有体现,可以自己添加),如果找到的数字个数不够n位,就进入for循环中,for循环枚举出每一个没有使用过的数字,同时判断这个没有使用过的数与前一位是否相加和为素数,如果条件满足,我们标记这个数已经使用过,同时也把这个数存入a数组,然后开始下一步搜索,但是下一步搜索之后就应该取消对上一个数的标记,这也就是回溯的精髓所在,就在这一段代码中

if(!v[i]&&pre(a[c-1]+i))

{

     a[c]=i;

 v[i]=1;

 dfs(c+1);

 v[i]=0;//回溯释放标志

}

如果这里不理解的话可以找个例子,假如我们在6以内寻找素数环,现在找到了1,4,3可能后面的搜索的分支结束了,但是我们还可以再次使用3,因为3完全可以在第四位,所以这一步我们需要释放标记,从而得到每一种可能继而判断。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int num[100100],vis[100100];
int n;
int prim(int x)
{
	for(int i=2;i*i<=x;i++)
	if(x%i==0)
	return 0;
	return 1;
} 
void dfs(int c)
{
	if(c==n&&prim(num[n-1]+1))
	{
		printf("1");
		for(int i=1;i<n;i++)
		printf(" %d",num[i]);
		printf("\n");
	}
	if(c==n) return ;
	for(int i=2;i<=n;i++)
	{
		if(!vis[i]&&prim(num[c-1]+i))
		{
			num[c]=i;
			vis[i]=1;
			dfs(c+1);
			vis[i]=0;
		}
	}
}
int main()
{
	int k=1;
	while(scanf("%d",&n)!=EOF)
	{
		memset(vis,0,sizeof(vis));
		num[0]=1;
		vis[1]=1;
		printf("Case %d:\n",k++);
		dfs(1);
		printf("\n");
	}
	return 0;
}



转载于:https://www.cnblogs.com/playboy307/p/5273578.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值