博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu1016 Prime Ring Problem
阅读量:6922 次
发布时间:2019-06-27

本文共 1512 字,大约阅读时间需要 5 分钟。

Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 41154    Accepted Submission(s): 18221

Problem Description
A ring is compose of n circles as shown in diagram. Put natural number 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 < 20).
 

 

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. Print solutions in lexicographical order.
You are to write a program that completes above process.
Print a blank line after each case.
 

 

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
直接暴力搜索,我估计是需要剪枝,因为开始没有剪枝,然后测试了一下n=20,半天没有出结果。
于是想了一下剪枝:
1 开始的数字不是1的时候,说明所有的组合都找出来了,剩余的就是旋转这个圈得到的结果
2 如果当前的数字和它前一个相加不是素数的话,就不用继续往下找了。
 
#include
#include
using namespace std;int n;int num[25];bool vis[25];int prime[30];void dfs(int a){ if(a>n+1) return; if(num[1]!=1) return; if(a==n+1) { for(int i=1; i
1) { if (!prime[num[a%n]+num[(a-1)%n]]) continue; } //cout<
<<" "<
View Code

 

 

 

Source

  

 

直接暴力搜索9

转载地址:http://kokjl.baihongyu.com/

你可能感兴趣的文章
iOS BLE4.0开发--工作中遇到的一些坑
查看>>
执行Shell脚本的4种方法
查看>>
部署QtQuick应用程序到安卓设备上时,AVD突然检测不到目标设备
查看>>
Linux笔记(shell基础,历史命令,命令补全/别名,通配符,输出重定向)
查看>>
Yii2---composer方法安装(Mac+MAMP)
查看>>
运维文章
查看>>
转-深入探讨Ruby与Python语法比较
查看>>
linux redhat6.5中 搭建NFS服务
查看>>
JMeter接口压力测试课程入门到高级实战(目录)
查看>>
extjs与struts开发的项目
查看>>
Linux20180423五周第四次课(4月23日)
查看>>
eclipse项目中,dubbo.xml不能识别schema报错
查看>>
MySQL中concat函数(连接字符串)
查看>>
[版本控制]原来Git分支都是这么用的
查看>>
浏览器兼容的获取event.offsetX的最简单方法
查看>>
JDK动态代理
查看>>
作为面试官,如何考察工程师的软素质
查看>>
怎么制作QQ动态表情包,GIF出处是哪
查看>>
万字长文概述NLP中的深度学习技术
查看>>
当量子计算遇到机器学习
查看>>