题意  把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 , 回溯)的更多相关文章

  1. UVa 524 Prime Ring Problem(回溯法)

    传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...

  2. UVa 524 Prime Ring Problem【回溯】

    题意:给出n,把从1到n排成一个环,输出相邻两个数的和为素数的序列 照着紫书敲的, 大概就是这个地方需要注意下,初始化的时候a[0]=1,然后dfs(1),从第1个位置开始搜 #include< ...

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

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

  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. UVA - 524 Prime Ring Problem(素数环)(回溯法)

    题意:输入n,把1~n组成个环,相邻两个数之和为素数. 分析:回溯法. #pragma comment(linker, "/STACK:102400000, 102400000") ...

  6. Uva 552 Prime Ring Problem(dfs)

    题目链接:Uva 552 思路分析:时间限制为3s,数据较小,使用深度搜索查找所有的解. 代码如下: #include <iostream> #include <string.h&g ...

  7. UVa 524 - Prime Ring Problem

    题目大意:输入正整数n,把整数1,2...,n组成一个环,使得相邻两个整数之和均为素数.输出时从整数1开始逆时针(题目中说的不是很明白??)排列.同一个环应恰好输出一次. 枚举,并在枚举每一个数是进行 ...

  8. uva 524(Prime Ring Problem UVA - 524 )

    dfs练习题,我素数打表的时候j=i了,一直没发现实际上是j=i*i,以后可记住了.还有最后一行不能有空格...昏迷了半天 我的代码(紫书上的算法) #include <bits/stdc++. ...

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

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

随机推荐

  1. 怎样控制界面控件之进度条(ProgressBar)功能

    一.基础知识: 1.ProgressBar在界面文件XML中的布局: [html] <progressBar android:id="@+id/progressbar_updown&q ...

  2. UVA1450-Airport

    题目链接 题意:有一个飞机场.有两条待飞跑到w和e.一条起飞跑道.每一时刻仅仅能起飞一架飞机,然后有w[i]和e[i]架飞机进入w和e跑道.飞机编号从0開始,问说怎样安排起飞能够使得飞机编号的最大值最 ...

  3. 在VM已安装Android4.4 连接小米手环 网络设置

    1.打开一个终端 2.输入su,选择同意 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl5dW4xMjNneA==/font/5a6L5L2T/fon ...

  4. [Python]网络爬虫(十):一个爬虫的诞生全过程(以山东大学绩点运算为例)

    先来说一下我们学校的网站: http://jwxt.sdu.edu.cn:7777/zhxt_bks/zhxt_bks.html 查询成绩需要登录,然后显示各学科成绩,但是只显示成绩而没有绩点,也就是 ...

  5. 【Gapps】安装GooglePlay引发一系列问题

    再次感谢小海的支持,感谢大家的支持! 从安装CM至如今GooglePlay,小海为我提供了非常多方案,能够说是全面支持.仅仅是出于隐私不便公开他的个人信息,仅提供一个他的博客地址http://luha ...

  6. Java EE (13) -- 常用的基础结构模式

    • Replication    • Load balance     • Failover    • Off-load shared resources    • Forward cache • R ...

  7. 推荐一套.NET文档处理组件Spire.Office

    原文:推荐一套.NET文档处理组件Spire.Office 以前的项目中用到一点Word简单处理的功能(文字替换和转PDF格式),当时使用的是一套COM组件,必须在服务器上安装office环境.最近考 ...

  8. Java的图片处理工具类

    import Java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...

  9. Android Graphics专题(1)--- Canvas基础

    作为Android Graphics专题的开篇.毫无疑问,我们将讨论Android UI技术的核心概念--Canvas. Canvas是Android UI框架的基础,在Android的控件体系中.全 ...

  10. cocos2d-x 3.2 它 三消游戏——万圣节大作战

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...