UVA 524
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 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
解题思路:如果这题用暴力枚举的话排列总数最高可高达16!=2*10^13,。就会超时。
所以这用回溯。用一个数组来储存要输出的序列,判断条件就是如果目前的数加上他前面的数的和为素数并且没有这数没被标记。满足条件就加1递归并标记。直到走到递归边界(cur==n)。
代码如下:
#include<cstdio>
#include<cstring>
using namespace std;
int vis[],isp[],A[],n;
int is_prime(int x) //用一个函数来判断是不是素数
{
for (int i = ; i*i<= x ; i++)
if (x% i == ) return ;
return ;
} void dfs(int cur)
{
if(cur==n&&isp[A[]+A[n-]]) //判断到递归边界没有,并且不要忘了判断最后一个和第一个加起来是不是素数,因为它是一个圆。
{
for(int i=; i<n-; i++)
printf("%d ", A[i]);//按照题目格式输出,注意空格问题。(我TM栽在这一上午)
printf("%d",A[n-]);
printf("\n");
}
else
for(int i=; i<=n; i++) //从2开始一个一个放
if(!vis[i]&&isp[i+A[cur-]]) //如果满足条件,和没有被标记
{
A[cur]=i;
vis[i]=; //标记
dfs(cur+); //递归
vis[i]=; //清除标记
}
}
int main()
{
int m=;
while(scanf("%d",&n)==&&n)
{
if(m>)
printf("\n");
++m;
for(int i=; i<=n*; i++)
isp[i]=is_prime(i);
printf("Case %d:\n",m);
//memset(vis,,sizeof(vis));
A[]=; //A[0]一定为1
dfs(); //从一开始递归
}
return ;
}
UVA 524的更多相关文章
- 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(回溯法)
传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...
- Uva 524 Prime Ring
如果用全排列生成之后,在判断是否是素数环是会超时的,应该用回溯. 回溯的时候 首先要注意 递归边界 ,结束的时候别忘记判断最后一个和第一个元素能否成立 还有要记得vis的使用和递归之后的清理. # ...
- 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排成一个环,输出相邻两个数的和为素数的序列 照着紫书敲的, 大概就是这个地方需要注意下,初始化的时候a[0]=1,然后dfs(1),从第1个位置开始搜 #include< ...
- 暴力求解——素环数 Prime Ring Problem ,UVa 524
Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers i ...
- UVa 524 Prime Ring Problem(DFS , 回溯)
题意 把1到n这n个数以1为首位围成一圈 输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的 n最大为16 利用回溯法 边生成边推断 就要快非常多了 #inc ...
- UVa 524 - Prime Ring Problem
题目大意:输入正整数n,把整数1,2...,n组成一个环,使得相邻两个整数之和均为素数.输出时从整数1开始逆时针(题目中说的不是很明白??)排列.同一个环应恰好输出一次. 枚举,并在枚举每一个数是进行 ...
- 7-4素数环 uva 524
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> using ...
随机推荐
- c# 友元程序集
在团队开发中,如果一个程序集中要调用另外一个程序集,但是要被调用的那个程序集又不想用public来公开自己的类, 那么怎么办,就是用最后一种internal来用来做类的可见性了. 下面来看一个简单例子 ...
- Adobe Edge Animate --使用HTML5实现手机摇一摇功能
Adobe Edge Animate --使用HTML5实现手机摇一摇功能 版权声明: 本文版权属于 北京联友天下科技发展有限公司. 转载的时候请注明版权和原文地址. HTML5的发展日新月异,其功能 ...
- Linux(CentOS6.5)系统安装Nginx
Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口 ...
- Spring(3.2.3) - Beans(9): @Resoure & @Autowired
@Resource 和 @Autowired 都是用来装配依赖的,它们之间有些异同. @Resoure @Resource 是 JSR-250 规范的注解. @Resource 可以标注在字段.方法上 ...
- SSRS:之为用户“NT AUTHORITY\NETWORK SERVICE”授予的权限不足,无法执行此操作。 (rsAccessDenied)
错误信息: 为用户"NT AUTHORITY\NETWORK SERVICE"授予的权限不足,无法执行此操作. (rsAccessDenied) 如图: 解决方案之检查顺序: 1. ...
- PB11.5创建及调用WebService
我今天做了这个“PB创建及调用WebService”的小作业,下面我来分享一下我的整个实践过程以及我遇到的问题,希望对大家有所帮助,这个小作业是分以下三部来完成的: 一.构建WebService ①创 ...
- 手机看youtube的方法|一个好用的VPN
俗话说:网络无国界.但是由于天朝的限制,我们无法访问YouTube .谷歌.twitter等一些国外网站. 这个时候我们就需要FQ.最直接有效的方法就是:VPN(虚拟专用网络). 虚拟专用网络的功能是 ...
- 类的访问修饰符_C#
访问控制修饰符: 访问控制修饰符 类内部 子类 程序集内 程序集外 Default √ Public √ √ √ √ Private √ Internal √ √ √ Protected √ √ Pr ...
- C/C++ union
叙述原因: union data{ int a;double b;}; 对于union,实际中用的并不多,之前也知道怎样计算union的单元(在字对齐的基础上取最大成员所占的内存大小),比如 unio ...
- [DevExpress][TreeList]条件隐藏节点CheckBox
关键代码: /// <summary> /// 隐藏CheckBox /// 说明 /// 在CustomDrawNodeCheckBox事件中使用 /// eg: /// TreeLis ...