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. bzoj 3158 千钧一发(最小割)

    3158: 千钧一发 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 767  Solved: 290[Submit][Status][Discuss] ...

  2. 用Processon在线绘制UML的尝试

    地址https://www.processon.com/ ProcessOn是一个面向垂直专业领域的作图工具和社交网络,成立于2011年6月并于2012年启动.ProcessOn将全球的专家顾问.咨询 ...

  3. Power Calculus 快速幂计算 (IDA*/打表)

    原题:1374 - Power Calculus 题意: 求最少用几次乘法或除法,可以从x得到x^n.(每次只能从已经得到的数字里选择两个进行操作) 举例: x^31可以通过最少6次操作得到(5次乘, ...

  4. 简单的FOLLOW集演示程序

    /* * 该程序用于计算某个非终结符的 FOLLOW 集合 * RexfieldVon * 2013年6月30日16:02:47 */ #include <stdio.h> #includ ...

  5. hdu4696 想法题

    就像1.2元人民币可以凑成任意你想要的面值一样.由于一定会有环,只要有C[i] == 1 就可以造成任何数.够坑吧 #include <cstdio> #include <cstri ...

  6. 1002 A + B Problem II [ACM刷题]

    这一段时间一直都在刷OJ,这里建一个博客合集,用以记录和分享算法学习的进程. github传送门:https://github.com/haoyuanliu/Online_Judge/tree/mas ...

  7. 关于C++函数思考1(缺省的六大函数)

    我们知道大神们在设计C++时候就给C++有六个默认的函数,所谓默认就是,无需我们这些程序猿们动手去写,仅仅要你在将类实例化.即创建一个对象,在利用对象进行数据操作时候,就会编译器自己主动调用默认的函数 ...

  8. [Flexbox] Using order to rearrange flexbox children

    Using the order property we alter the order in which flexbox children appear on the page, without ma ...

  9. (转)void指针(void *的用法)

    指针有两个属性:指向变量/对象的地址和长度 但是指针只存储地址,长度则取决于指针的类型 编译器根据指针的类型从指针指向的地址向后寻址 指针类型不同则寻址范围也不同,比如: int*从指定地址向后寻找4 ...

  10. Python开发【第八篇】:网络编程

    Python之路[第六篇]:socket   Socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字&quo ...