HDU1016 Prime Ring Problem(深度优先搜索)

Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 27488 Accepted Submission(s): 12248

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

问题描述:将从1到n这n个整数围成一个圆环,若其中任意2个相邻的数字相加,结果均为素数,那么这个环就成为素数环。

n=20时,下面的序列就是一个素数环:

1 2 3 4 7 6 5 8 9 10 13 16 15 14 17 20 11 12 19 18

思路如下:

对除奇数个以外的20以内的所有情况进行筛选,若能同时满足相邻相加为素数,且保证在数组中第一次使用则可选,向下继续搜索。关于素数的判断,建议提前打表,节约时间啊有木有。。。

#include<iostream>
#include<cmath>
using namespace std;
int t[21];
int pre[20];
int N,count;
int prime(int m)
{
	int x=(int)sqrt((double)m);
	for(int k=2;k<=x;k++)
	{
		if(m%k==0)
			return 0;
	}
	return 1;
}
int judge(int m)
{
	for(int i=1;i<=N&&t[i]!=0;i++)
	{
		if(m==t[i])
			return 0;
	}
	return 1;
}
int prim(int m)
{
	for(int i=0;i<20;i++)
		if(m==pre[i])return 1;
		return 0;
}
void DFS(int n)
{
	if(n==N+1)
	{
		if(prime(t[N]+t[1]))
		{

			for(int k=1;k<N;k++)
				cout<<t[k]<<" ";
			cout<<t[N]<<endl;
		}
	}
	for(int i=2;i<=N;i++)
	{

		if(judge(i)&&prim(t[n-1]+i))
		{

			t[n]=i;
			DFS(n+1);
			t[n]=0;
		}
	}
}
int main()
{
	int k=0;
	for(int i=2;i<40;i++)
		if(prime(i))
			pre[k++]=i;
		while(cin>>N&&N%2==0)
		{
			count++;
			cout<<"Case "<<count<<":"<<endl;
			memset(t,0,sizeof(t));
			t[1]=1;
			DFS(2);
			cout<<endl;
		}
		return 0;
}

之后看了一个别人的,发现也是不错的哦,时间和空间上都有很大的优化。

#include"stdio.h"
#include"string.h"
int n;
int a[123],used[123];
int ok(int n)
{
    int i;
    for(i=2;i<n;i++)
    {
        if(n%i==0) return 0;
    }
    return 1;
}
void dfs(int x)
{
    int i;
    if(x==n)
    {
        int j;
        if(ok(1+a[x-1])==1)  //头尾和判断
        {
            printf("1");
            for(j=1;j<n;j++)  printf(" %d",a[j]);  //构造够n个了 输出数组。
            printf("\n");
            return ;
        }
    }
    for(i=2;i<=n;i++)
    {
        if(used[i]==0&&ok(i+a[x-1])==1)  //加上判断和是不是素数
        {
            a[x]=i;
            used[i]=1;  //标记使用了
            dfs(x+1);    //对第x+1个进行构造
            used[i]=0;  //标记复原
        }
    }
    return ;
}
int main()
{
    int cas=1;
    while(scanf("%d",&n)!=-1)
    {
        memset(used,0,sizeof(used)); // 赋值都没被使用过。
        used[1]=1;
        a[0]=1;

        printf("Case %d:\n",cas++);
        dfs(1);  //从第1个数开始构造,因为以1开始
        printf("\n");
    }
    return 0;
}

这里注意,used数组是用来标记已被选用的i值,而a数组则是从0开始存储依次选取的数值,这也正解决了一个数组无法使用标记搜索。

时间: 2024-09-28 20:07:54

HDU1016 Prime Ring Problem(深度优先搜索)的相关文章

hdu1016 Prime Ring Problem dfs 素数打表

意思是给你一个数n,要构成一个素数环,这个素数由1-n组成,它的特征是选中环上的任意一个数字i,i与它相连的两个数加起来都分别为素数,满足就输出. 这个题的做法和hdu1015做法差不多都是使用dfs 回溯.不同之处在于这个要全部搜索,而hdu1015只需要搜索第一组就可以. 其次在这个题目中使用素数打表的方式简化素数判定,在一定情况下也是对效率有所提高的. Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limi

HDU-1016 Prime Ring Problem(DFS深搜+打表)

题目回顾(HDU-1016): Prime Ring Problem 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

HDU1016 Prime Ring Problem

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 35961    Accepted Submission(s): 15867 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

HDU1016 Prime Ring Problem(DFS回溯)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 34609    Accepted Submission(s): 15327 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

HDU1016——Prime Ring Problem

http://acm.hdu.edu.cn/showproblem.php?pid=1016 这道题是经典的素数环问题,相邻的两个数之和是素数. 解题方法:用的是深搜,以1为起点,搜索,下一个数为出去前面的数字的集合.(用vis数组记录访问过的节点) 剪枝:当前搜索值与数组前一个值之和不为素数的时候返回. #include <iostream> #include <cstdio> #include <cstring> #include<math.h> usi

HDU1016 Prime Ring Problem DFS 简单题

题意:输入n,代表有一个n个节点的环,然后在节点分别填入1到n这n个数,规定,第一个填入的必须是1. 0<n<40 要求填入后满足,任意相邻的2个节点的数之和为素数. 将满足条件的填法按照字典序的顺序小到大依次输出. 其实刚开始做的时候我觉得这道题会做很久的,想好后就开始打了,打着打着发现这道题打好了,有点意外,原来这么简单. 注意回溯,在每次DFS后要记得把状态恢复原样,比如这道题是每次DFS后都要把当前DFS的数字i恢复为vis[i]=false; 先考虑好初始化,结束条件,回溯. 先来一

HDU1016 Prime Ring Problem (回溯 + 剪枝)

题意: 给你一个数字N(N <= 20),要求你把这N个数组成一个环,环内的数字不能重复,左右相邻的两个的和是素数.给出最后的答案. 思路: 利用回溯剪枝算法,N个数,每个数有N种状态,枚举这N个状态,枚举过程中剪枝优化. 代码: #include <cstdio> #include <iostream> #include <cstring> #include <cstring> #include <cmath> #include <

[ACM] 1016 Prime Ring Problem (深度优先搜索)

Prime Ring Problem 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

hdu 1016 Prime Ring Problem(深度优先搜索)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12105    Accepted Submission(s): 5497 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb