UVa 524 Prime Ring Problem(回溯法)
Description
A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 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 ≤ 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
思路
题意:
输入正整数n,把整数1,2,3,……,n组成一个环,使得相邻两个整数之和均为素数。输出时序列头为1开始的序列,同一个环应恰好输出一次
题解:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 40;
bool is_prime[maxn],vis[maxn];
int n,a[maxn];
void dfs(int cur)
{
if (cur == n && is_prime[a[0] + a[n-1]]) //递归边界,因为是环,所以还要测试第一个和最后一个
{
printf("%d",a[0]);
for (int i = 1;i < n;i++) printf(" %d",a[i]);
printf("\n");
}
else
{
for (int i = 2;i <= n;i++) //尝试放置每个数i
{
if (!vis[i] && is_prime[i + a[cur-1]]) //如果i没有用过,并且与前一个数之和为素数
{
a[cur] = i;
vis[i] = true; //设置使用标志
dfs(cur+1);
vis[i] = false; //清楚标志
}
}
}
}
int main()
{
//提前预处理素数表
memset(is_prime,true,sizeof(is_prime));
is_prime[0] = is_prime[1] = false;
for (int i = 2;i < maxn;i++)
{
if (is_prime[i])
{
for (int j = 2 * i;j < maxn;j += i)
{
is_prime[j] = false;
}
}
}
int tcase = 0;
while (~scanf("%d",&n))
{
memset(vis,false,sizeof(vis));
memset(a,0,sizeof(a));
if (tcase) printf("\n");
printf("Case %d:\n",++tcase);
a[0] = 1;
dfs(1);
}
return 0;
}
UVa 524 Prime Ring Problem(回溯法)的更多相关文章
- 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(DFS , 回溯)
题意 把1到n这n个数以1为首位围成一圈 输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的 n最大为16 利用回溯法 边生成边推断 就要快非常多了 #inc ...
- HDU 1016 Prime Ring Problem (回溯法)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 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 524 Prime Ring Problem【回溯】
题意:给出n,把从1到n排成一个环,输出相邻两个数的和为素数的序列 照着紫书敲的, 大概就是这个地方需要注意下,初始化的时候a[0]=1,然后dfs(1),从第1个位置开始搜 #include< ...
- 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++. ...
- [HDU 1016]--Prime Ring Problem(回溯)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
随机推荐
- querySelector系列方法相比 getElementsBy 系列方法有什么区别?
querySelector 和 querySelectorAll 相比下面这些方法有什么区别? getElementsByTagName getElementsByClassName getElem ...
- BranchCache在sharepoint2013使用
BranchCache 是 Windows 7.Windows 8.Windows Server 2008 R2 和 Windows Server 2012 操作系统的一项功能,此功能可在本地分支机构 ...
- IOS 开发小技巧总结
一.添加自定义字体 1.把字体文件拖到工程中. 2.plist 文件中添加字段:<Array>Fonts provided by application</Array> 把字体 ...
- 使用WebRTC搭建前端视频聊天室——信令篇
博客原文地址 建议看这篇之前先看一下使用WebRTC搭建前端视频聊天室——入门篇 如果需要搭建实例的话可以参照SkyRTC-demo:github地址 其中使用了两个库:SkyRTC(github地址 ...
- iOS 10 开发适配系列 之 权限Crash问题
升级 iOS 10 之后目测坑还是挺多的,记录一下吧,看看到时候会不会成为一个系列. 直入正题吧 今天用一个项目小小练下手,发现调用相机,崩了.试试看调用相册,又特么崩了.然后看到控制台输出了以下信息 ...
- 【代码笔记】iOS-实现网络图片的异步加载和缓存
代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. se ...
- mysql半同步复制问题排查
1.问题背景 默认情况下,线上的mysql复制都是异步复制,因此在极端情况下,主备切换时,会有一定的概率备库比主库数据少,因此切换后,我们会通过工具进行回滚回补,确保数据不丢失.半同步复制则 ...
- 无刷新读取数据库 (ajax)
<html> <head> <script type="text/javascript"> function loadXMLDoc() { va ...
- vim 中替换
将80替换为10.0.0.19:80 :g/80/s//10.0.0.19:80/g
- 4-4 grep及正则表达式
1. grep:Globally search a Regular Expression and Print:根据模式搜索文本,并将符合模式的文本行显示出来 pattern:文本字符和正则表达式的元字 ...