Uva 552 Prime Ring Problem(dfs)
题目链接:Uva 552
思路分析:时间限制为3s,数据较小,使用深度搜索查找所有的解。
代码如下:
#include <iostream>
#include <string.h>
using namespace std; const int MAX_N = ;
int n;
int A[MAX_N], vis[MAX_N]; int is_prime( int n )
{
if ( n == )
return true; for ( int i = ; i * i <= n; ++i )
if ( n % i == )
return false; return true;
} void dfs( int cur )
{
if ( cur == n && is_prime( A[] + A[n - ] ) )
{
int i = ; for ( i = ; i < n - ; ++i )
printf( "%d ", A[i] );
printf( "%d", A[i] );
printf( "\n" );
}
else
{
for ( int i = ; i <= n; ++i )
{
if ( !vis[i] && is_prime( i + A[cur - ] ) )
{
A[cur] = i;
vis[i] = ;
dfs( cur + );
vis[i] = ;
}
}
}
} int main( )
{
int count = ; while ( scanf( "%d", &n ) != EOF )
{
if ( count != )
printf( "\n" ); A[] = ;
memset( vis, , sizeof( vis ) );
printf( "Case %d:\n", count++ );
dfs( );
} return ;
}
Uva 552 Prime Ring Problem(dfs)的更多相关文章
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- 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 ...
- HDU 1016 Prime Ring Problem (DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Prime Ring Problem(dfs水)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Prime Ring Problem (DFS练习题)
K - Prime Ring Problem ============================================================================= ...
- hdu1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- iOS中Block介绍(一)基础
ios开发block的使用指南,以及深入理解block的内存管理,也适用于osx开发.讨论范围:block的使用,内存管理,内部实现.不包含的内容:gc arc下的block内存,block在c++中 ...
- iOS面试题02-数据存储
1.如果后期需要增加数据库中的字段怎么实现,如果不使用CoreData呢? 回答:编写SQL语句来操作原来表中的字段 1>增加表字段 ALETER TABLE 表名 ADD COLUMN 字段名 ...
- jquery+easy ui 实现表格列头筛选
示例代码 1.筛选的下拉 <a href="javascript:void(0)" id="filterStatus" class="easyu ...
- Android 开发笔记 “Sqlite Cursor 使用”
使用过 SQLite 数据库的童鞋对 Cursor 应该不陌生,如果你是搞.net 开发你大可以把Cursor理解成 Ado.net 中的数据集合相当于dataReader.今天特地将它单独拿出来谈, ...
- SQL中 and or优先级问题
资源来源:http://www.linuxidc.com/Linux/2012-03/56267.htm 刚刚在项目中遇到这样一个问题,SQL语句如下: select * from LOAN_BACK ...
- .htaccess Rewrite apache重写和配置
首先: 必须要空间支持 Rewrite 以及对站点目录中有 .htaccess 的文件解析,才有效. 如何让空间支持Rewrite 和 .htaccess 的文件解析呢 往下看 第一步:要找到apac ...
- Linux下 保存 git账号密码
一.通过文件方式 1.在~/下, touch创建文件 .git-credentials, 用vim编辑此文件,输入内容格式: touch .git-credentials vim .git-crede ...
- ssh远程登录linux服务器
ssh远程登录linux服务器 用法: ssh -l user -p port server_ip 或者 ssh -p port user@server_ip 参数: -l 后接要登录的远程系统用户名 ...
- linux 命令大全
工作了一段时间,开始整理资料,好记性不如烂笔头啊. linux命令大全下载路径: 1.http://www.pc6.com/SoftView/SoftView_28912.html 2.http:// ...
- (Problem 70)Totient permutation
Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number ...