搜索专题: HDU1016Prime Ring Problem
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 52146 Accepted Submission(s): 23096
Note: the number of first circle should always be 1.

lexicographical order.
You are to write a program that completes above process.
Print a blank line after each case.
6
8
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
Asia 1996, Shanghai (Mainland China)
RunId : 21243288 Language : G++ Author : hnustwanghe
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 20 + 5;
bool prime[N*3],used[N]={0};
int a[N],n;
void prime_factor(){
memset(prime,0,sizeof(prime));
prime[0] = prime[1] = 1;
for(int i=2;i<=N*2;i++)
if(!prime[i]){
for(int j=i*i;j<=N;j+=i)
prime[j] = 1;
}
}
void DFS(int cur){
if(cur > n){
if(!prime[a[1]+a[n]])
for(int i=1;i<=n;i++)
printf("%d%c",a[i],i==n?'\n':' ');
return;
}
for(int i=2;i<=n;i++){
if(!used[i]){
if(!prime[a[cur-1]+i]){
used[i] = true;
a[cur] = i;
DFS(cur+1);
used[i] = false;
}
}
}
}
int main(){
a[1] = 1;
prime_factor();
int cnt = 0;
while(scanf("%d",&n)==1){
printf("Case %d:\n",++cnt);
DFS(2);
printf("\n");
}
}
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 20 + 5;
bool prime[N*3],used[N]={0};
int a[N],n;
void prime_factor(){
memset(prime,0,sizeof(prime));
prime[0] = prime[1] = 1;
for(int i=2;i<=N*2;i++)
if(!prime[i]){
for(int j=i*i;j<=N;j+=i)
prime[j] = 1;
}
} void DFS(int cur){
if(cur > n){
if(!prime[a[1]+a[n]])
for(int i=1;i<=n;i++)
printf("%d%c",a[i],i==n?'\n':' ');
return;
}
for(int i=2;i<=n;i++){
if(!used[i]){
if(!prime[a[cur-1]+i]){
used[i] = true;
a[cur] = i;
DFS(cur+1);
used[i] = false;
}
}
}
} int main(){
a[1] = 1;
prime_factor();
int cnt = 0;
while(scanf("%d",&n)==1){
printf("Case %d:\n",++cnt);
DFS(2);
printf("\n");
}
}
搜索专题: HDU1016Prime Ring Problem的更多相关文章
- 素数环:NYOJ--488--dfs||hdu-1016-Prime Ring Problem
/* Name: NYOJ--488--素数环 Author: shen_渊 Date: 15/04/17 15:30 Description: DFS,素数打个表,37以内就够用了 */ #incl ...
- DFS——hdu1016Prime Ring Problem
一.题目回顾 题目链接:Prime Ring Problem Problem Description A ring is compose of n circles as shown in diagra ...
- hdu1016Prime Ring Problem
就是说,给你一个数n, 要你把1到n都连在一起成环. 每一个数不可反复, 且相连的两个数的和要是素数. 把全部情况输出来. 我是用dfs暴力出来的. 首先把素数打表, 然后每次顺时针预測下一个数 ...
- hdu 1016 Prime Ring Problem(深度优先搜索)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- UVA524-Prime Ring Problem(搜索剪枝)
Problem UVA524-Prime Ring Problem Accept:6782 Submit:43814 Time Limit: 3000 mSec Problem Descripti ...
- 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 & % ...
- HDU 1016 Prime Ring Problem (回溯法)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- 如何使用dump文件进行调试
转载[文尾出处链接] 1 简介第一次遇到程序崩溃的问题,之前为单位开发了一个插件程序,在本机运行没有出现问题,但把生成的可执行文件拷贝到服务器上一运行程序,刚进入插件代码,插件服务就崩溃了,当时被这个 ...
- python – 如何禁用Django的CSRF验证?
如果只需要一些视图不使用CSRF,可以使用@csrf_exempt: from django.views.decorators.csrf import csrf_exempt @csrf_exempt ...
- android之实现上下左右翻页效果
如果实现上下或者左右翻页效果,我们借助下这个开源项目:https://github.com/openaphid/android-flip Aphid FlipView是一个能够实现Flipboard翻 ...
- 第一次试验报告&学习总结
打印输出所有的"水仙花数",所谓"水仙花数"是指一个3位数,其中各位数字立方和等于该数本身.例如,153是一个"水仙花数". 试验代码: p ...
- Linux下服务器开发的必要准备
一.Windows下安装Xshell 二.Linux开启SSH 可以先查询有没有SSH服务 sudo ps -e |grep ssh 没有安装SSH的服务器 sudo apt-get install ...
- leetcode-easy-array-122 best time to buy and sell stocks II
mycode 69.45% class Solution(object): def maxProfit(self, prices): """ :type prices: ...
- tomcat在45秒内没有启动,启动超时
在部署的时候出现Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds. If the server ...
- ES6中变量的解构赋值
1.数组的解构赋值 基本用法 ES6 允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring). 输出: 上面代码表示,可以从数组中提取值,按照对应位置,对变 ...
- 阶段3 1.Mybatis_09.Mybatis的多表操作_7 mybatis多对多准备角色表的实体类和映射配置
创建Role表和user_role表 DROP TABLE IF EXISTS `role`; CREATE TABLE `role` ( `ID` int(11) NOT NULL COMMENT ...
- sql语句exists 用法
参考:SQL语句exists用法,Sql语句中IN和exists的区别及应用 现在有三张表 学生表S: sno ,sname 课程表C:cno ,cname 学生选课表SC: sno ,cno 查询选 ...