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) return 0;
return 1;
} void dfs(int cur)
{
if(cur == n && p[a[n - 1] + 1])
{
printf("%d", a[0]);
for(int i = 1; i < n; ++i)
printf(" %d", a[i]);
printf("\n");
} for(int i = 2; cur < n && i <= n; ++i)
{
if(!vis[i] && p[a[cur - 1] + i])
{
vis[i] = a[cur] = i;
dfs(cur + 1);
vis[i] = 0;
}
}
} int main()
{
int cas = 0;
a[0] = 1;
for(int i = 2; i < N; ++i)
p[i] = isPrime(i); while(~scanf("%d", &n))
{
if(cas) printf("\n");
printf("Case %d:\n", ++cas);
dfs(1);
} return 0;
}
Prime Ring Problem |
A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 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 <= 16)
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.
You are to write a program that completes above process.
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
UVa 524 Prime Ring Problem(DFS , 回溯)的更多相关文章
- UVa 524 Prime Ring Problem(回溯法)
传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...
- UVa 524 Prime Ring Problem【回溯】
题意:给出n,把从1到n排成一个环,输出相邻两个数的和为素数的序列 照着紫书敲的, 大概就是这个地方需要注意下,初始化的时候a[0]=1,然后dfs(1),从第1个位置开始搜 #include< ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- uva 524 prime ring problem——yhx
Prime Ring Problem A ring is composed of n (even number) circles as shown in diagram. Put natural ...
- UVA - 524 Prime Ring Problem(素数环)(回溯法)
题意:输入n,把1~n组成个环,相邻两个数之和为素数. 分析:回溯法. #pragma comment(linker, "/STACK:102400000, 102400000") ...
- Uva 552 Prime Ring Problem(dfs)
题目链接:Uva 552 思路分析:时间限制为3s,数据较小,使用深度搜索查找所有的解. 代码如下: #include <iostream> #include <string.h&g ...
- UVa 524 - Prime Ring Problem
题目大意:输入正整数n,把整数1,2...,n组成一个环,使得相邻两个整数之和均为素数.输出时从整数1开始逆时针(题目中说的不是很明白??)排列.同一个环应恰好输出一次. 枚举,并在枚举每一个数是进行 ...
- uva 524(Prime Ring Problem UVA - 524 )
dfs练习题,我素数打表的时候j=i了,一直没发现实际上是j=i*i,以后可记住了.还有最后一行不能有空格...昏迷了半天 我的代码(紫书上的算法) #include <bits/stdc++. ...
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
随机推荐
- 解压tar.gz文件报错gzip: stdin: not in gzip format解决方法
解压tar.gz文件报错gzip: stdin: not in gzip format解决方法 在解压tar.gz文件的时候报错 1 2 3 4 5 [Sun@localhost Downloads] ...
- hdu4280(最大流)
传送门:Island Transport 题意:有N个岛屿 M条无向路 每个路有一最大允许的客流量,求从最西的那个岛屿最多能运用多少乘客到最东的那个岛屿. 分析:无向图正反都加弧,权值一样,这题点多, ...
- uva796(求桥数目)
传送门:Critical Links 题意:给出一个无向图,按顺序输出桥. 分析:模板题,求出桥后排个序输出. #include <cstdio> #include <cstring ...
- UML它 时序图
在本文中,我们重点谈谈UML时序图,UML包括在主时序图的建模元素:对象(Actor).生命线(Lifeline).聚焦控制(Focusofcontrol).消息(Message)等等. 一.UML时 ...
- Android的编译系统
一.Makefile的主要流程 以下主要流程都在build/core/main.mk里安排. l 初始化相关的参数设置(buildspec.mk.envsetup.mk.config.mk) ...
- www.centos.org
https://www.centos.org/forums/viewtopic.php?t=5770 In order to conserve the limited bandwidth availa ...
- 使用python向Redis批量导入数据
1.使用pipeline进行批量导入数据.包含先使用rpush插入数据,然后使用expire改动过期时间 class Redis_Handler(Handler): def connect(self) ...
- 每天一个JavaScript实例-递归实现反转数组字符串
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- Python数据结构之注意事项
1.列表 列表是Python中使用最频繁的数据结构,列表提供很多函数操作,比如下标存取,分片,index,append,remove等等. 例如: list=[1,2,'hello','python ...
- java7 API详解
Java™ Platform, Standard Edition 7API Specification This document is the API specification for the J ...