HDU1016 DFS+回溯(保存路径)

Prime Ring Problem

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


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   n个数排序 1必须在第一位 两个数相加要为素数(1与最后一个数相加也必须为素数) 输出所有满足上述的序列。
解析 我们先1~n分别求出与它相加为素数的数存进数组,然后就相当于建了一个图 从1出发 一层一层向下推 基础DFS 然后在回溯一下 记录路径就好了
AC代码
 1 #include <stdio.h>
 2 #include <math.h>
 3 #include <string.h>
 4 #include <stdlib.h>
 5 #include <iostream>
 6 #include <sstream>
 7 #include <algorithm>
 8 #include <string>
 9 #include <queue>
10 #include <vector>
11 using namespace std;
12 const int maxn= 25;
13 const int maxm= 1e4+10;
14 const int inf = 0x3f3f3f3f;
15 typedef long long ll;
16 int visit[maxn],order[maxn];
17 vector<int> v[maxn];
18 int n,cnt;
19 int isPrime(int num )
20 {
21      int tmp =sqrt( num);
22      for(int i= 2;i <=tmp; i++)
23         if(num %i== 0)
24           return 0 ;
25      return 1 ;
26 }
27 void dfs(int x)
28 {
29     visit[x]=1;    //标记访问过
30     order[cnt++]=x; //记录路径
31     for(int i=0;i<v[x].size();i++)    //df搜边
32     {
33         if(visit[v[x][i]]==0)
34         {
35             dfs(v[x][i]);
36         }
37     }
38     visit[x]=0;    //回溯
39     if(cnt==n&&isPrime(x+1))   //长度为n且最后一个数与1相加为素数
40     {
41         for(int i=0;i<cnt;i++) 
42         {
43             if(i==cnt-1)
44                 printf("%d\n",order[i]);
45             else
46                 printf("%d ",order[i]);
47         }
48     }
49     cnt--;
50 }
51 int main()
52 {
53     int kase=1;
54     while(scanf("%d",&n)!=EOF)
55     {
56         for(int i=1;i<=n;i++)
57             v[i].clear();
58         for(int i=1;i<=n;i++)
59         {
60             for(int j=1;j<=n;j++)
61             {
62                 if(isPrime(i+j)==1&&i!=j)    //保存能够与i相加为素数的数
63                     v[i].push_back(j);
64             }
65         }
66 //        for(int i=1;i<=n;i++)
67 //        {
68 //            cout<<i;
69 //            for(int j=0;j<v[i].size();j++)
70 //                printf(" %d",v[i][j]);
71 //            cout<<endl;
72 //        }
73         memset(visit,0,sizeof(visit));   //初始化访问数组 0未访问过
74         printf("Case %d:\n",kase++);
75         cnt=0;   //路径长度初始化
76         dfs(1);  
77         printf("\n");
78     }
79 }

 

 

转载于:https://www.cnblogs.com/stranger-/p/7805782.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值