Problem UVA524-Prime Ring Problem

Accept:6782  Submit:43814

Time Limit: 3000 mSec

 Problem Description

A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 1,2,...,n 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 Ouput

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

题解:回溯法经典题目,回溯就是剪枝嘛,这个题的剪枝就是题意,水题。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std; const int maxn = ,maxp = ;
bool vis[maxn];
int n,ans[maxn]; bool is_prime[maxp];
int prime[maxp]; void Euler(){
memset(is_prime,true,sizeof(is_prime));
int cnt = ;
is_prime[] = is_prime[] = false;
for(int i =;i < maxp;i++){
if(is_prime[i]) prime[cnt++] = i;
for(int j = ;j<cnt && prime[j]<=maxp/i;j++){
is_prime[i*prime[j]] = false;
if(i%prime[j] == ) break;
}
}
} void dfs(int cur,int *ans){
if(cur==n && is_prime[ans[n-]+ans[]]){
for(int i = ;i < n;i++){
if(i != ) printf(" ");
printf("%d",ans[i]);
}
printf("\n");
return;
}
for(int i = ;i <= n;i++){
if(!vis[i] && is_prime[ans[cur-]+i]){
vis[i] = true;
ans[cur] = i;
dfs(cur+,ans);
vis[i] = false;
}
}
} int main()
{
//freopen("input.txt","r",stdin);
int iCase = ;
Euler();
while(~scanf("%d",&n)){
if(iCase > ) printf("\n");
memset(vis,false,sizeof(vis));
ans[] = ;
vis[] = true;
printf("Case %d:\n",iCase++);
dfs(,ans);
}
return ;
}

UVA524-Prime Ring Problem(搜索剪枝)的更多相关文章

  1. HDU1016 Prime Ring Problem (回溯 + 剪枝)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5398684.html 题意: 给你一个数字N(N <= 20),要求你把这N个数组成一个环,环内的数字不能重复,左右 ...

  2. UVA524 素数环 Prime Ring Problem

    题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016:  https://vjudge.net/problem/HDU-10 ...

  3. hdu 1016 Prime Ring Problem(深度优先搜索)

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

  4. ZOJ 1457 Prime Ring Problem(dfs+剪枝)

     Prime Ring Problem Time Limit: 10 Seconds      Memory Limit: 32768 KB A ring is compose of n circ ...

  5. HDUOJ----(1016)Prime Ring Problem

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

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

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

  7. hdu 1016 Prime Ring Problem(DFS)

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

  8. HDU 1016 Prime Ring Problem(经典DFS+回溯)

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

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

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

随机推荐

  1. Redirection

    Typically, the syntax of these characters is as follows, using < to redirect input, and > to r ...

  2. web站点和windows服务项目发布时如何排除指定文件

    在发布asp.net站点和windows服务项目时,有的时候这样的需求:msbuild编译之后发布到服务器指定目录时要排除指定文件,比如通过jenkins构建时,不希望覆盖原来的Web.config和 ...

  3. Request method 'POST' not supported错误和解决方法

    在使用SpringBoot的时候,在html页面用form表单post提交数据的时候报错: Request method 'POST' not supported 错误解析: 我是用的前端页面是HTM ...

  4. JavaScript之Number、String、Array常用属性与方法手册

    Number isFinite函数 Number.isFinite() 方法用来检测传入的参数是否是一个有穷数(finite number). 语法: Number.isFinite(value) 例 ...

  5. 【代码笔记】Web-JavaScript-JavaScript字符串

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  6. redis cluster是如何做到集两家之长的

    站在读写分离的层次看redis的时候,redis和master和slave存在明显的主从关系,也就是说master处于管理状态,salve跟着大哥混,master给小弟slave发粮食[发送内存快照数 ...

  7. Java面试题总结(不定期更新)

    1.HashMap和Hashtable的区别? HashMap:key.value都可以为空,线程不安全.初始容量16,扩容方式每次为2倍 Hashtable:不支持null key 和null va ...

  8. pyinstaller使用错误 SyntaxError: Non-UTF-8 code starting with '\xb4' in file C:......

    注:我的博客原本在CSDN,现转到博客园,图片采用以前的图片,并没有盗图.​​ 在将.py文件打包时,出现了下列错误 >>C:\Users\小呆\PycharmProjects\pycha ...

  9. 服务器CPU繁忙或内存压力引起网络掉包的浅析与总结

      最近一段时间遇到了两起有意思的故障,现象都是网络掉包或网络断开,不过这些只是表面现象,引起现象出现的本质才是我们需要关注的重点: 案例1: 平台   :VMware平台 操作系统 :Windows ...

  10. MHA快速搭建

    很早之前写过MHA的文章,但是常常在技术群看到有同学问MHA搭建的问题,不是权限问题就是配置问题,我在这里就再次一写下配置过程以及快速的搭建.如果想知道更多的细节与原理,请参考:MySQL高可用架构之 ...