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思想与框架 ...
随机推荐
- 慕尼黑大学公开课 Competitive Strategy(竞争策略)总结
第一章博弈 同时的博弈:双方同时定制策略 如果有显著的次优策略总是不如另一个,则剔除它. 如果一个策略组合中没有一方可以单独改变其策略以提高回报,则称为Nash均衡.一个游戏可能没有也可能有多个Nas ...
- OCP读书笔记(15) - 管理SQL性能调优
SQL Tuning Advisor(STA): 使用oracle提供的程序包进行sql优化 SQL> conn scott/tiger SQL), name )); SQL> inser ...
- MyEclipse导入主题文件epf后xml及jsp等页面中点击标签之后显示灰白
MyEclipse导入主题文件epf后xml及jsp等页面中点击标签之后显示灰白,症状例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvVVAxOT ...
- 30岁生日,媳妇赏的,U-BOAT手表一枚-数字尾巴
http://bbs.dgtle.com/thread-139611-1-1.html
- 【翻译】我钟爱的Visual Studio前端开发工具/扩展
原文:[翻译]我钟爱的Visual Studio前端开发工具/扩展 怎么样让Visual Studio更好地编写HTML5, CSS3, JavaScript, jQuery,换句话说就是如何更好地做 ...
- ubuntu 搭建svn服务器
1.安装Subversion sudo apt-get install subversion 2.创建资源库 cd /home/username/ svnserve -d -r /home/usern ...
- VC6.0入门使用
软件下载地址 http://pan.baidu.com/s/1qWuqFAO 新建win console 32 project,然后新建header文件.最后新建source cpp文件.如图所看到的
- 外网SSH访问内网LINUX的N种方法
外网SSH访问内网LINUX的N种方法 http://www.nat123.com/Pages_8_260.jsp 一,动态公网IP环境 1,环境描述: 路由器分配的是动态公网IP,且有路由管理权限, ...
- Java EE (11) - 影响性能的因素
垂直层(Tier)影响性能的因素 资源层数据库性能通常考虑以下方面的优化(MySQL为例):--使用哪种存储引擎:MyISAM vs. InnoDB, MERGE, MEMORY, Federated ...
- 在iOS7中改动状态栏字体的颜色
状态栏的字体为黑色:UIStatusBarStyleDefault 状态栏的字体为白色:UIStatusBarStyleLightContent 一.在info.plist中,将View contro ...