HDOJ-1016 Prime Ring Problem(DFS)

http://acm.hdu.edu.cn/showproblem.php?pid=1016

题意:输入n,代表有一个包含n个节点的环,在环中的节点中填入1,2...n-1,n,要求填入的数与左边的数之和,与右边的数之和,都为素数

输出所有符合要求的环(第一个数总为1)

用DFS模拟,从第2位到第n位依次选取一个与上一个选取的数之和为素数的数

直到选取完第n个数,判断第n个数和1之和是否为素数,是则输出,否则不进行操作,判断完毕返回上一层

直到遍历完所有情况

*:因为素数必然是奇数,所以一条符合要求的素数环每两个相邻的数肯定是一奇一偶

然而当n为奇数且不等于1时,不管怎么排列,总会出现相邻的两个的数同为奇数或同为偶数,使得两者之和必是偶数,不是素数

所以,当n为奇数且不等于1时,可以不用进行处理,剪枝50%

**:但是杭电的数据里n貌似都是偶数,特判n和不特判n的时间花费都差不多,所以并没什么卵用

# include <stdio.h>
# include <math.h>
# include <string.h>

int Prime[50], Ring[25], n;
bool Flag[25];

bool IsPrime(int num)
{
	int t = sqrt(double(num));
	for(int i = 3; i <= t; i++)
		if(num % i == 0)
			return false;
	return true;
}

void DFS(int Depth)
{
	if(Depth == n + 1)//若以全部选择完毕
	{
		if(Prime[Ring[Depth - 1] + 1])//查看最后一个和第一个之和是否是素数
		{
			printf("1");
			for(int i = 2; i <= n; i++) printf(" %d", Ring[i]);
			printf("\n");
		}
		return ;
	}

	for(int i = 2; i <= n; i++)
	{
		if(!Flag[i] && Prime[i + Ring[Depth - 1]])//i与上一个选的数之和是否是素数
		{
			Ring[Depth] = i;
			Flag[i] = true;//每选取一个数就将其标记

			DFS(Depth + 1);

			Flag[i] = false;//搜索完选取该数之后的所有可能后,取消标记
		}
	}
}

int main()
{
	for(int i = 3; i < 50; i += 2) Prime[i] = 1;
	Prime[2] = 1;
	for(int i = 3; i < 50; i += 2)
	{
		if(IsPrime(i))
		{
			for(int j = i * 2; j < 50; j += i)
				Prime[j] = 0;
		}else Prime[i] = 0;
	}//素数打表

	int Case = 0;
	while(scanf("%d",&n) != EOF)
	{
		printf("Case %d:\n", ++Case);

		if(n == 1)
			printf("1\n");
		else if(n % 2 == 0)
		{
			memset(Flag, false, sizeof(Flag));
			Flag[1] = true;
			Ring[1] = 1;//第一个始终为1
			DFS(2);
		}

		printf("\n");
	}

	return 0;
}

  

时间: 2024-08-01 19:51:17

HDOJ-1016 Prime Ring Problem(DFS)的相关文章

hdu 1016 Prime Ring Problem (dfs)

一切见注释. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; bool vis[22]; int n; int ans[22]; int top; bool isprime(int x)//判断素数 { for(int i=2;i<x;i++) if(x%i==0)return false; return

hdoj 1016 Prime Ring Problem 【DFS】

策略如题 链接 http://acm.hdu.edu.cn/showproblem.php?pid=1016 代码: #include<stdio.h> #include<string.h> int prime[25] = {1, 1}, n, vis[25]; //vis作用:标记是否用过 int a[25]; void f() //找出来前20的素数 判定为0 { for(int i = 2; i <= 24; i ++){ if(prime[i] == 0) for(i

hdoj - 1258 Sum It Up &amp;&amp; hdoj - 1016 Prime Ring Problem (简单dfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1258 关键点就是一次递归里面一样的数字只能选一次. 1 #include <cstdio> 2 #include <cstring> 3 4 int n,t; 5 int b[15],c[15]; 6 bool flag; 7 void dfs(int k,int sum,int l) 8 { 9 if(sum==t) 10 { 11 for(int i=0;i<l-1;i++) 12 p

HDU 1016 Prime Ring Problem (素数筛+DFS)

题目链接 题意 : 就是把n个数安排在环上,要求每两个相邻的数之和一定是素数,第一个数一定是1.输出所有可能的排列. 思路 : 先打个素数表.然后循环去搜..... 1 //1016 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 6 using namespace std ; 7 8 bool vis[21]; 9 int prime[42] ,cs[21]; 10 int n ; 11

[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 --- 经典DFS

思路:第一个数填1,以后每个数判断该数和前一个数想加是否为素数,是则填,然后标记,近一步递归求解. 然后记得回溯,继续判断下一个和前一个数之和为素数的数. /* HDU 1016 Prime Ring Problem --- 经典DFS */ #include <cstdio> #include <cstring> int n; bool primer[45], visit[25]; //primer打素数表,visit标记是否访问 int a[25]; //数组a存储放置的数 /

hdu 1016 Prime Ring Problem (简单DFS)

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

HDU 1016 Prime Ring Problem(素数环问题)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 63806    Accepted Submission(s): 27457 Problem Description A ring is compos

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