HDU1016

题意:1-n围成一个圈,要使相邻的两个数之和为素数;

DFS

第二道没看题解自己搜出来的题,好有成就感!!(虽然比较水吧= =

上代码咯~

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <math.h>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 const int dx[4]= {1,0,-1,0};
 8 const int dy[4]= {0,1,0,-1};
 9 int vis[105],prime[40],n,k1,s[10110],cas;
10 int dfs(int x,int cnt)
11 {
12     if(cnt==n&&prime[x+1])//满足条件时,输出。
13     {
14         printf("1 ");
15         for(int i=0; i<k1; i++)
16         {
17             printf("%d",s[i]);
18             if(i!=k1-1)
19             {
20                 printf(" ");
21             }
22             else
23             {
24                 printf("\n");
25             }
26         }
27         x=1;//进行下面的搜索的初始化。
28         cnt=1;
29     }
30     else
31     {
32         for(int i=2; i<=n; i++)
33         {
34             if(!vis[i]&&prime[x+i])
35             {
36                 vis[i]=1;
37                 s[k1++]=i;
38                 dfs(i,cnt+1);
39                 vis[i]=0;//如果搜不到就让标记消失~
40                 k1--;
41             }
42         }
43     }
44     return 0;
45 }
46 int main()
47 {
48     memset(prime,0,sizeof(prime));//打一个素数表
49     for(int i=2; i<=40; i++)
50     {
51         int k=sqrt(i);
52         int f=0;
53         for(int j=2; j<=k; j++)
54         {
55             if(i%j==0)
56             {
57                 prime[i]=0;
58                 f=1;
59                 break;
60             }
61         }
62         if(f==0)
63         {
64             prime[i]=1;
65         }
66     }
67     cas=0;
68     while(~scanf("%d",&n))
69     {
70         cas++;
71         k1=0;
72         memset(vis,0,sizeof(vis));//每次都要初始化
73         printf("Case %d:\n",cas);
74         dfs(1,1);//从一开始搜
75         printf("\n");
76     }
77     return 0;
78 }

时间: 2024-08-06 07:53:52

HDU1016的相关文章

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

Prime Ring Problem hdu-1016 DFS

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. Inputn (0 < n < 2

HDU-1016【素数环】

题意:本题题意就是构成一个素数环.即相邻两数之和要为素数.环的元素个数在1到20之间. 素数-只能被1和它本身整除的数 输入6就是1-6排成一个相邻数相加是素数:环,第一个和最后一个加起来也要是素数 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 /*su shu huan*/

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

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

HDU1016 素数环---(dfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1016 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 (0 < n < 20). 找出n个整数,能组成一个环,环中任意相邻两数之和为素数,字典序输出所有情况,每种情况1

hdu1016(简单深搜)

这是一个简单深搜问题,要求相邻的数之间相加为素数.采用深搜,把满足条件的值放在parent[]中.最后输出parent[]. 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: th

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,

hdu-1016素数环

这个题就是给出一个数字n,表示有n个数,编号为1~n, 然后要求我们将这n个数连起来变成一个环,要求任意两个数相加所得值必须为素数. 如果满足条件就将这个环输出来! 这个题:dfs+回溯+判断.然后注意先是将值放到一条线上, 如果头尾相加和也为素数,则可以连成环,然后就可以输出了! 代码如下: #include<iostream> #include<cstring> #include<cstdio> using namespace std; int n,a[22]; b