UVa524 Prime Ring Problem (回溯法)

链接:http://acm.hust.edu.cn/vjudge/problem/19667分析:先打个素数表,题目要求从1开始打印,直接把1固定到A[0]位置,打印的时候就可以直接从A[0]开始打印了,然后枚举2~n,vis判断之前是否用过,和是否为素数,都满足则继续递归枚举,然后回溯将vis还原。
 1 #include <iostream>
 2 #include <cstring>
 3 using namespace std;
 4
 5 int n, isp[35], A[20], vis[20];
 6
 7 int is_prime(int x) {
 8     for (int i = 2; i < x; i++)
 9         if (!(x % i)) return 0;
10     return 1;
11 }
12
13 void dfs(int cur) {
14     if (cur == n)
15         if (isp[A[0] + A[n - 1]]) {
16             for (int i = 0; i < n; i++) {
17                 if (i != 0) cout << " ";
18                 cout << A[i];
19             }
20             cout << endl;
21         } else return;
22     else for (int i = 2; i <= n; i++)
23         if (!vis[i] && isp[i + A[cur - 1]]) {
24             A[cur] = i;
25             vis[i] = 1;
26             dfs(cur + 1);
27             vis[i] = 0;
28         }
29 }
30
31 int main() {
32     int kase = 0;
33     A[0] = 1;
34     for (int i = 2; i < 32; i++) isp[i] = is_prime(i);
35     while (cin >> n && n) {
36         memset(vis, 0, sizeof(vis));
37         if (kase++) cout << endl;
38         cout << "Case " << kase << ":" << endl;
39         dfs(1);
40     }
41     return 0;
42 }
时间: 2024-10-13 12:26:11

UVa524 Prime Ring Problem (回溯法)的相关文章

[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) Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2

HDU1016 Prime Ring Problem (回溯 + 剪枝)

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

UVA - 524 Prime Ring Problem(dfs回溯法)

UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the sum of number

UVa 524 Prime Ring Problem(DFS , 回溯)

题意  把1到n这n个数以1为首位围成一圈  输出所有满足任意相邻两数之和均为素数的所有排列 直接枚举排列看是否符合肯定会超时的  n最大为16  利用回溯法 边生成边判断  就要快很多了 #include<cstdio> using namespace std; const int N = 50; int p[N], vis[N], a[N], n; int isPrime(int k) { for(int i = 2; i * i <= k; ++i) if(k % i == 0)

UVA524 素数环 Prime Ring Problem

题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016:  https://vjudge.net/problem/HDU-1016 zoj 1457  :https://vjudge.net/problem/ZOJ-1457 题意翻译 输入正整数n,把整数1,2,...,n组成一个环,使得相邻两个整数之和均为素数.输出时,从整数1开始逆时针排列.同一个环恰好输出一次.. 多组数据,读入到EOF结束. 第i组数据输出前加上一

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

[2016-02-19][UVA][524][Prime Ring Problem]

UVA - 524 Prime Ring Problem Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the

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

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