题目链接: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)的更多相关文章

  1. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  2. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  3. UVa 524 Prime Ring Problem(DFS , 回溯)

    题意  把1到n这n个数以1为首位围成一圈  输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的  n最大为16  利用回溯法 边生成边推断  就要快非常多了 #inc ...

  4. uva 524 prime ring problem——yhx

      Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural ...

  5. HDU 1016 Prime Ring Problem (DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. Prime Ring Problem(dfs水)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  7. 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 ...

  8. Prime Ring Problem (DFS练习题)

    K - Prime Ring Problem ============================================================================= ...

  9. hdu1016 Prime Ring Problem(DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. .net-一般处理程序及生命周期

    IsReusable属性用来表示在IHttpHandlerFactory对象创建IHttpHandler的时候是否能够将这个Handler存入池中以便重用. 一般处理程序(HttpHandler):是 ...

  2. BZOJ 1207: [HNOI2004]打鼹鼠( dp )

    dp.. dp[ i ] = max( dp[ j ] + 1 ) ------------------------------------------------------------------ ...

  3. [LeetCode]题解(python):001-Two-Sum

    题目来源: https://leetcode.com/problems/two-sum/ 题意分析: 这道题目是输入一个数组和target,要在一个数组中找到两个数字,其和为target,从小到大输出 ...

  4. IOS 特定于设备的开发:UIDevice

    UIDevice类展示了一些关键的特定于设备的属性,包括使用的iPhone ,Ipad或iPod Touch型号.设备名称.以及OS名称和版本. 他是一种一站式解决方案,用于提取出某些系统详细信息.每 ...

  5. C++模板:二分查找

    bool find(int x,int l,int r){ if(l>r)return false; int mid=(l+r)/2; if(s[mid]==x) return true; el ...

  6. jQuery.fn.extend与jQuery.extend 的区别

    1 jquery.extend 是jquery 静态的方法 实例 jQuery.extend({     liu: function(){         alert('liu');     } }) ...

  7. GCD自己做的一些简单总结

    GCD总结 GCD  Grand Central Dispatch  牛逼的中枢调度器 GCD中各种队列的执行效果 想看线程  必须是异步函数  并且不是主队列 注意:使用sync函数往当前串行队列添 ...

  8. GDB调试之暂停

    暂停机制: 有3种方式可以通知GDB暂停程序的执行. a.断点: 通知GDB在程序中的特定位置暂停执行: b.监视点:通知GDB当特定内存位置(或者涉及一个或多个位置的表达式)的值发生变化时暂停执行: ...

  9. Live Writer Test

    测试下LiveWriter写CNblog: 1.Source code plug-in: @Override public List getAll(String orgid,String start, ...

  10. 使用uWSGI+nginx部署Django项目

    最近使用django写了一些项目,不过部署到服务器上碰到一些问题,还有静态文件什么的一堆问题,这里总结一下碰到的问题和解决方案,总体思路是按照官方文档走的. 原文地址:http://uwsgi-doc ...