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

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

Prime Ring Problem

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

Total Submission(s): 53931    Accepted Submission(s): 23862

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
 
 
直接dfs就可以了
 
 
 
#include 
#include
#include
#include
#include
using namespace std;int vis[25],n;bool prime(int k){ for(int i=2;i<=sqrt(k);i++) { if(k%i==0) return false; } return true;}void dfs(int k){ if(vis[k]==n&&prime(k+1)) { printf("1"); for(int i=2;i<=n;i++) { for(int j=1;j<=n;j++) { if(vis[j]==i) printf(" %d",j); } } printf("\n"); return ; } for(int i=1;i<=n;i++) { if(prime(i+k)&&!vis[i]) { vis[i]=vis[k]+1; dfs(i); vis[i]=0; } }}int main() { int Case=0; while(~scanf("%d",&n)) { printf("Case %d:\n",++Case); memset(vis,0,sizeof(vis)); vis[1]=1; dfs(1); printf("\n"); } return 0;}

 

转载于:https://www.cnblogs.com/52why/p/7478134.html

你可能感兴趣的文章
mac添加环境变量
查看>>
ORM之创建数据库
查看>>
PHP处理Android的POST数据
查看>>
总结spring下配置dbcp,c3p0,proxool数据源链接池
查看>>
Sublime Text3 快捷键汇总及设置快捷键配置环境变量
查看>>
代码调试--自定义一个简单的debug函数
查看>>
T4语法快速入门
查看>>
OOP 第四章博客总结
查看>>
JAVAEE——SpringBoot配置篇:配置文件、YAML语法、文件值注入、加载位置与顺序、自动配置原理...
查看>>
洛谷 P1044 栈
查看>>
springMVC学习(2)
查看>>
【CentOS-7+ Ambari 2.7.0 + HDP 3.0+HAWQ2.3.00】遭遇问题及解决记录
查看>>
总结-jQuery
查看>>
Spring 声明式事务
查看>>
Eclipse中将含有图片资源的项目打包成jar文件
查看>>
【剑指offer12】矩阵中的路径(回朔法),C++实现
查看>>
Bzoj2342 [Shoi2011]双倍回文
查看>>
git-git使用
查看>>
Kubernetes安装
查看>>
MyBatis(1)优点&介绍&工程
查看>>