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(int j = i+i; j <= 24; j +=i)
		prime[j] = 1;
	}
}
void dfs(int step)
{
	int i;
	if(step == n){
		if(prime[a[step-1]+a[step-2]]==0&&prime[a[step-1]+1] == 0){  //这处就是判断最后一位能不能和邻接的两个环能否构成素数
			printf("1");
			for(int j = 1; j < n; j ++)
			printf(" %d", a[j]);
			printf("\n");
		}
		return ;
	}
	for(i = 2; i <= n; i ++){
		if(vis[i] == 0&&prime[i+a[step-1]] == 0){  //如果i没有被用并且满足跟前面一位的构成素数,就将其存到相应的数组中
			a[step] = i;
			vis[i] = 1;
			dfs(step+1);
			vis[i] = 0;
		}

	}
}
int main(){
	f();
	int v = 1;
	while(scanf("%d", &n) == 1){
		memset(vis, 0, sizeof(vis));
		printf("Case %d:\n", v++);
		a[0] = 1;
		vis[1] = 1;
		dfs(1);
		printf("\n");
	}
	return 0;
}

hdoj 1016 Prime Ring Problem 【DFS】

时间: 2024-10-01 21:18:12

hdoj 1016 Prime Ring Problem 【DFS】的相关文章

HDOJ-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): 47217    Accepted Submission(s): 20859 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

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

思路:第一个数填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 (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

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): 28144    Accepted Submission(s): 12533 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

hdoj 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 of first circle should always be 1

HDOJ 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 of first circle should always be 1

UVa 524 Prime Ring Problem【回溯】

题意:给出n,把从1到n排成一个环,输出相邻两个数的和为素数的序列 照着紫书敲的, 大概就是这个地方需要注意下,初始化的时候a[0]=1,然后dfs(1),从第1个位置开始搜 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<m