UVA - 524 Prime Ring Problem

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

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回溯法)的更多相关文章

  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 , 回溯)

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

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

  6. UVA 524 素数环 【dfs/回溯法】

    Description   A ring is composed of n (even number) circles as shown in diagram. Put natural numbers ...

  7. Uva 552 Prime Ring Problem(dfs)

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

  8. UVa 524 - Prime Ring Problem

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

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

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

随机推荐

  1. Using Apache Web Server with Jboss AS 7

    In real-world projects, it's common to find Apache web server as a front door to your application se ...

  2. [Locked] Binary Tree Vertical Order Traversal

    Binary Tree Vertical Order Traversal Given a binary tree, return the vertical order traversal of its ...

  3. SRM 404(1-250pt, 1-500pt)

    DIV1 250pt 题意:对于1-9数字三角形如下图,设其为a[i][j],则a[i][j] = (a[i-1][j] + a[i-1][j+1]) % 10.现在对于某个数字三角形, 每行告诉你某 ...

  4. IT项目经理应具备的十大软技能

    现在,企业对IT部项目经理的要求越来越多.如果你认为IT项目成员只需要技术性能力,那可就错了. 据IT招聘公司调查发现,几年人们对项目管理软技能的兴趣明显浓厚起来.许多企业尽量避免把IT部门看成只是成 ...

  5. TCP/IP之分层

    网络协议通常分不同层次进行开发,每一层分别负责不同的通信功能.一个协议族,比方T C P / I P,是一组不同层次上的多个协议的组合.T C P / I P通常被觉得是一个四层协议系统. 1.每层的 ...

  6. Core Python Notes

    开发需要在读 Python 核心编程,一些 Point 记录如下. ******************************************** 版本相关 标准版的 Python 是用 C ...

  7. LinkButton和HyperLink的页面跳转用法

    <%--<asp:HyperLink ID="HyperLink1" NavigateUrl='<%#"/Fxy_Admin/Pro_ClassNew. ...

  8. 关于timestamp的二三事

    之所以要写timestamp的随笔,是因为之前对它的理解存在误区,so. I have to remind myself by writing this informal essay. 微软文档链接: ...

  9. oracle问题 《经由直接路径由 EXPORT:V10.02.01 创建的导出文件 IMP-00013: 只有 DBA 才能导入由其他 DBA 导出的文件》

    问题:  经由直接路径由 EXPORT:V10.02.01 创建的导出文件 : 只有 DBA 才能导入由其他 DBA 导出的文件 解决方法:用sys 登录,给当前用户授权,授权语句:grant dba ...

  10. Nginx反向代理配置配置实例

    为了节省支出,公司需要将分布在不同机器的站点都迁移到一台机器,而目前不同机器运行的是不同的web服务,部分是nginx,部分是apache,由于牵涉较多rewrite规则,为了节省修改功夫,打算迁移后 ...