UVA524-Prime Ring Problem(搜索剪枝)
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
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(搜索剪枝)的更多相关文章
- HDU1016 Prime Ring Problem (回溯 + 剪枝)
本文链接:http://www.cnblogs.com/Ash-ly/p/5398684.html 题意: 给你一个数字N(N <= 20),要求你把这N个数组成一个环,环内的数字不能重复,左右 ...
- UVA524 素数环 Prime Ring Problem
题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016: https://vjudge.net/problem/HDU-10 ...
- hdu 1016 Prime Ring Problem(深度优先搜索)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- ZOJ 1457 Prime Ring Problem(dfs+剪枝)
Prime Ring Problem Time Limit: 10 Seconds Memory Limit: 32768 KB A ring is compose of n circ ...
- HDUOJ----(1016)Prime Ring Problem
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- hdu 1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
随机推荐
- Java学习笔记之——多态、抽象
1. 多态 多态:同一种事物调用同一个方法有不同的表现行为.(同一类型操作,作用于某一类对象,可以有不同的解释,产生不同的执行结果) 应用场景;当你定义一个功能性的方法可以使用多态的概念 前提:子类继 ...
- js发送邮件确定email地址
<a href="mailto:wjl@tom.com?subject=aaa&body=11111">test</a>
- adb命令笔记
adb devices [-l]: 列出所有连接设备 l: 列出设备限定符 adb connect <host>[:<port>]: 通过ip连接到设备 host: IP po ...
- 08-HTML-框架标签
<html> <head> <title>框架标签学习</title> <meta charset="utf-8"/> ...
- K8S 部署 ingress-nginx (二) 部署后端为 tomcat
在上面已经部署了 ingress-nginx, https://www.cnblogs.com/klvchen/p/9903480.html 创建 service 和 pods cd vi tomca ...
- php对二维数据排序
对于一维数组排序比较简单,像使用sort(),asort(),arsort()等函数进行排序,但是对于二维数组比较麻烦,所有借鉴网上的总结了一下 // 对二维数组进行指定key排序 $arr 二维数组 ...
- 小tips:JS严格模式(use strict)下不能使用arguments.callee的替代方案
在函数内部,有两个特殊的对象:arguments 和 this.其中, arguments 的主要用途是保存函数参数, 但这个对象还有一个名叫 callee 的属性,该属性是一个指针,指向拥有这个 a ...
- oppo手机屏幕录制的详细操作技巧
说起屏幕录制,肯定我们都使用过各式各样的录屏工具,不管是手机或者电脑自带的,还是第三方录屏工具,是不是都出现过不能正常录屏的,或者没有把声音录制下来等一系列问题,为了给更多的人节约时间,因为笔者使用的 ...
- android adb 流程原理代码分析(一)
由于要用到adb的知识,但是对adb啥也不了解,看了下android的代码,adb的源码在system/core/adb下面,然后网上搜下了资料,发现很多大神的源码分析,瞬间信心爆棚,把大神写的博客都 ...
- Django 类视图
引文 所有的类视图都继承django.views.generic.base.View类. 在URLconf中简单的使用通用视图 如果只是简单的做一些属性修改,可以使用as_view()方法,如下所示: ...