ZOJ 1457 Prime Ring Problem(dfs+剪枝)
Prime Ring Problem
Time Limit: 10 Seconds
Memory Limit: 32768 KB
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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath> using namespace std; int n,test=0;;
int cir[30];
int vis[30];
int prime[50]={0}; void dfs(int k)
{
if(k==n)
{
if(!prime[cir[k]+cir[1]])
return;
for(int i=1;i<n;i++)
printf("%d ",cir[i]);
printf("%d",cir[n]);
printf("\n");
return;
} for(int j=2;j<=n;j++)
{
if(!vis[j]&&prime[cir[k]+j]) //对于相邻素数的推断,在这里才干在zoj,AC,放最前面就仅仅能在HDU,AC了,一层递归调用的时间而已啊!
{ vis[j]=1;
cir[k+1]=j;
dfs(k+1);
vis[j]=0; //注意递归复原,这预计是最大的亮点了 } }
} int main()
{
prime[3]=1;
prime[5]=1;
prime[7]=1;
prime[11]=1;
prime[13]=1;
prime[17]=1;
prime[19]=1;
prime[23]=1;
prime[29]=1;
prime[31]=1;
prime[37]=1; while(scanf("%d",&n)!=EOF)
{
//memset(cir,0,sizeof cir);
//memset(vis,0,sizeof vis);
for(int i=1;i<=n;i++)
{
vis[i]=0;
}
printf("Case %d:\n",++test);
if(n % 2 == 1)
{
printf("\n");
continue;
} cir[1]=1;
dfs(1);
printf("\n");
} return 0;
}
ZOJ 1457 Prime Ring Problem(dfs+剪枝)的更多相关文章
- 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 ...
- Prime Ring Problem(dfs水)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Prime Ring Problem (DFS练习题)
K - Prime Ring Problem ============================================================================= ...
- hdu1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- ZOJ 1457 E-Prime Ring Problem
https://vjudge.net/contest/67836#problem/E A ring is compose of n circles as shown in diagram. Put n ...
- Prime Ring Problem dfs
A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle ...
- Uva 552 Prime Ring Problem(dfs)
题目链接:Uva 552 思路分析:时间限制为3s,数据较小,使用深度搜索查找所有的解. 代码如下: #include <iostream> #include <string.h&g ...
随机推荐
- [leetcode]Median of Two Sorted Arrays @ Python
原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ 题意:There are two sorted arrays A ...
- [leetcode]Unique Binary Search Trees @ Python
原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees/ 题意: Given n, how many structurally ...
- jQuery中的编程范式
浏览器前端编程的面貌自2005年以来已经发生了深刻的变化,这并不简单的意味着出现了大量功能丰富的基础库,使得我们可以更加方便的编写业务代码,更重要的是我们看待前端技术的观念发生了重大转变,明确意识到了 ...
- java中正则表达式基本用法(转)
https://www.cnblogs.com/xhj123/p/6032683.html 正则表达式是一种可以用于模式匹配和替换的规范,一个正则表达式就是由普通的字符(例如字符a到z)以及特殊字符( ...
- 【图片识别】Java中使用tess4J进行图片文字识别(支持中文)(转)
http://blog.csdn.net/wsk1103/article/details/54173282 java中识别文字比较简单,使用的软件是tesseractocr(使用的版本是3.02,3以 ...
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(八)安装zookeeper-3.4.12
如何搭建配置centos虚拟机请参考<Kafka:ZK+Kafka+Spark Streaming集群环境搭建(一)VMW安装四台CentOS,并实现本机与它们能交互,虚拟机内部实现可以上网.& ...
- 转: xshell远程连接自动断开的问题解决办法
转:http://blog.csdn.net/haijiaoqihao20160106/article/details/50623431 2.客户端的配置 Keep Alive修改.我的xshell的 ...
- React从0到1
本篇将一直更新下去,写的多了,可能会拆成小章节,记录完整的学习笔记 github https://github.com/ae6623/ReactL
- hdu 5411 CRB and Puzzle (矩阵高速幂优化dp)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5411 题意:按题目转化的意思是,给定N和M,再给出一些边(u,v)表示u和v是连通的,问走0,1,2... ...
- python 微信企业号
python 微信企业号 准备,如果没有微信企业号,可以先申请体验号记下CorpID和Secret(获取Token用) 发送消息首先可以在微信的开发者中心,查看接口文档 下面就是python代码:1. ...