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 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
紫书194页原题
题解:输入正整数n,把1—n组成一个环,是相邻的两个整数为素数。输出时从整数1开始,逆时针排列。同一个环恰好输出一次,n (0 < n <= 16)
暴力解决会超时,应用回溯法,深度优先搜索
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int n;
int a[],vis[]; int isp(int n) //判断是否为素数
{
if(n<)
return false;
for (int i=;i*i<=n; i++)
{
if(n % i == )
return false;
}
return true;
} void dfs(int s)
{
if(s==n&&isp(a[]+a[n])) //递归边界。别忘了测试第一个数和最后一个数
{
for(int i=; i<n; i++)
cout<<a[i]<<" ";
cout<<a[n]<<endl;
}
else
{
for(int i=; i<=n; i++)
{
if(!vis[i]&&isp(i+a[s])) //如果i没有用过,并且与钱一个数之和为素数
{
a[s+]=i;
vis[i]=; //标记
dfs(s+);
vis[i]=; //清除标记
}
}
}
}
int main()
{
int t=;
while(cin>>n)
{
memset(vis,,sizeof(vis));
a[]=;
if(t!=) cout<<endl; //一定注意输出格式
t++;
cout<<"Case "<<t<<":"<<endl;
dfs();
}
return ;
}
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 , 回溯)
题意 把1到n这n个数以1为首位围成一圈 输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的 n最大为16 利用回溯法 边生成边推断 就要快非常多了 #inc ...
- 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 素数环 【dfs/回溯法】
Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers ...
- 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++. ...
随机推荐
- A Mini Locomotive(动态规划 01)
/* 题意:选出3个连续的 数的个数 为K的区间,使他们的和最大 分析: dp[j][i]=max(dp[j-k][i-1]+value[j],dp[j-1][i]); dp[j][i]:从 ...
- Java中的数据类型及相互转换方法
本文主要讲解两个部分: 一.Java中的数据类型有哪些? 二.数字类型和字符串类型相互转换的方法? 一.Java中的数据类型有哪些: Java中的数据类型有:基本数据类型和引用数据类型: 基本数据类型 ...
- poj 2187 凸包加旋转卡壳算法
题目链接:http://poj.org/problem?id=2187 旋转卡壳算法:http://www.cppblog.com/staryjy/archive/2009/11/19/101412. ...
- 西安Uber优步司机奖励政策(1月18日~1月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 想挑战AlphaGO吗?先和PostgreSQL玩一玩?? PostgreSQL与人工智能(AI)
1月4日晚,随着古力认输,Master对人类顶尖高手的战绩停留在60胜0负1和,而令人尴尬的是这唯一一场和棋还是因为棋手掉线系统自动判和,并不是棋盘上的局势真的势均力敌了.包括聂卫平.柯洁.朴廷桓.井 ...
- Unity3D 集成 Face++ FacePlusPlus httpClient http协议 byte数组转string
//開始由于要实现跨平台.考虑过用curl封装c++的dll(android *.so)的方式,在c#Dllimport实现 //后来发现Unity3D本身支持http协议.且face++的api都是 ...
- 关于DPM(Deformable Part Model)算法中模型结构的解释
关于可变部件模型的描写叙述在作者[2010 PAMI]Object Detection with Discriminatively Trained Part Based Models的论文中已经有说明 ...
- [Angular 2] Inject Service
TypeScript is used heavily as we build up our application, but TypeScript isn’t required. If you wan ...
- 网络接口 使用NSURLConnection完成Get和Post方法
网络接口 使用NSURLConnection完成Get和Post方法 什么是URL: URL就是统一资源定位器(UniformResourceLocator:URL).通俗地说,它是用来指出某一项信息 ...
- 一些Linux优化方法
1. 利用栈做备胎,减少分配空间的几率,IO自己有一份缓存,如果超了就使用stack空间 2. 分散IO:代表readv,可以通过一次系统调用,将内容读到分散的缓存中,可以减少系统的系统调用