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. 【Java每日一题】20170320

    20170317问题解析请点击今日问题下方的“[Java每日一题]20170320”查看(问题解析在公众号首发,公众号ID:weknow619) package Mar2017; public cla ...

  2. 元类实现ORM

    1. ORM是什么 ORM 是 python编程语言后端web框架 Django的核心思想,"Object Relational Mapping",即对象-关系映射,简称ORM. ...

  3. 计算机1&操作系统硬件

    1.什么是编程语言? 语言是一种事物与另外一种事物沟通的表达方式   而编程则是人与计算机沟通的表达方式 2:什么是编程? 编程就是程序员用计算机能理解的的表达方式,把程序员想要表达的内容写到文件里, ...

  4. 封装个 Android 的高斯模糊组件

    本篇文章已授权微信公众号 hongyangAndroid (鸿洋)独家发布 最近基于 Android StackBlur 开源库,根据自己碰到的需求场景,封装了个高斯模糊组件,顺便记录一下. 为什么要 ...

  5. phpcms有二级导航并且高亮效果代码

    <div class="collapse navbar-collapse" id="example-navbar-collapse"> <ul ...

  6. Tars 负载均衡

    // 传入主控地址,在 db_tars t_registry_info 表中 Communicator communicator = CommunicatorFactory.getInstance() ...

  7. 深入理解Java虚拟机02--Java内存区域与内存溢出异常

    一.概述 我们在进行 Java 开发的时候,很少关心 Java 的内存分配等等,因为这些活都让 JVM 给我们做了.不仅自动给我们分配内存,还有自动的回收无需再占用的内存空间,以腾出内存供其他人使用. ...

  8. Linux中对逻辑卷的移除

    移除前先df -mT 看一下:(在上一篇的基础上:Linux中对逻辑卷进行扩容) 1.取消挂载同时删除/etc/fstab下的记录 取消挂载 umount /dev/zhi/lv-zhi 删除记录 v ...

  9. linux文件统计命令和目录统计命令

    1.统计本目录下除`./apps/myapp/migrations`的所有py文件 wc -l `find -path . -o -name '*py' ! -path "./apps/my ...

  10. spring4笔记----“零配置”:spring提供的几个Annotation标注

    @Component  :标注一个普通的Spring Bean类 @Controller    :标注一个控制器组件器 @Service        :标注一个业务逻辑组件器 @Repository ...