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 ...
随机推荐
- reset.css(详细说明)
@charset "utf-8";/************************************************************************ ...
- javascript——马步之Array篇
数组 Array == 1.建立数组==* 通过变量赋值为[]来建立数组 var arr = []; // arr 是一个数组 * 需要注意数组的下标是从0开始中的 ==2.获取数组长度== * 通过 ...
- linux-进程相关
ps查看当前运行的进程 ps -ef|grep httpd ps是静态的 ps -aux|grep httpd top 是动态的 kill 杀进程 kill -[选项] 进程号 -9强制杀死 kil ...
- [改善Java代码]自由选择字符串拼接方法
对一个字符串拼接有三种方法:加号,contact方法,StringBuffer或者StringBuilder的append方法,其中加号是最常用的.其他两种方式偶尔会出现在一些开源项目中,那么这三者有 ...
- 关于关闭Eclipse的控制台自动跳出
参考文章: http://my.oschina.net/mn1127/blog/161093 Eclipse的控制台console有时候经常的跳出来,非常的烦人! 尤其是在调试期间跳出,以下是分享一下 ...
- 用VS2015打开cshtml识图文件的时候会报错 如指定的文件不存在
用vs2015打开cshtml识图文件的时候会报错.百度后得到解决方法如下: 先关闭VS2015, 拷贝:%LocalAppData%\Microsoft\VisualStudio\14.0\Comp ...
- SQL Server批量更新数据
项目中有一个位置需要批量插入几万条数据,批量insert等待时间简直...用SqlBulkCopy后,之前需要1分钟左右的sql现在只要一眨眼(真的只要一眨眼) 稍后这个功能要加到另外一个项目中,另外 ...
- Jersey(1.19.1) - Hello World, Get started with a Web application
1. Maven Dependency <properties> <jersey.version>1.19.1</jersey.version> </prop ...
- Redis - HyperLogLogs
A HyperLogLog is a probabilistic data structure used in order to count unique things (technically th ...
- ms mpi error: unable to allocate launching block
问题描述: 在VS 2015中使用Microsoft MPI(ms mpi)构建控制台应用,使用" mpiexec -n 4 myprog.exe"运行时退出并提示"un ...