Prime ring problem,递归,广搜,回溯法枚举,很好的题
- 题目描述:
-
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.
- 输入:
-
n (1 < n < 17).
- 输出:
-
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.
- 样例输入:
-
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
- 提示:
-
用printf打印输出。
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std; int ans[];
bool mark[];
bool check(int a){
if ((a%2 == 0||a%3==0||a%5==0)&&a!=2&&a!=3&&a!=5)//因为输入的n<17,所以相邻数字加和小于32,合数的因子就235
return false;
return true;
}
int n;
void dfs(int cnt){
if (cnt> )
if (check(ans[cnt-]+ans[cnt-])==false)
return;
if (cnt==n)
if (check(ans[cnt-]+ans[])==false)
return;
if (cnt == n){
printf("");
for (int i=;i<n;i++){
printf(" %d",ans[i]);
}
printf("\n");
}
for (int i=;i<=n;i++){
if (mark[i]==false){
mark[i]=true;
ans[cnt]=i;
//cnt++;
dfs(cnt+);
mark[i]=false;
} }
} int main (){
int c=;
while (cin>>n){
c++;
for (int i=;i<=n;i++)
mark[i]=false;
ans[]=;
mark[]=true; printf("Case %d:\n",c);
dfs();
printf("\n");
} return ;
}
哎呀呀!他说我超时!!!!找了好久好久好久的bug
后来突然想起之前学长说printf比cout快
然后换了,ac了
刚刚粘题目的时候发现。。提示里面有,我没看见。哭哭
所以说啊,printf好啊,以后尽量用这个吧
这道题也很好啊,
标红是核心,好好看
Prime ring problem,递归,广搜,回溯法枚举,很好的题的更多相关文章
- 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 ...
- UVa 524 Prime Ring Problem(DFS , 回溯)
题意 把1到n这n个数以1为首位围成一圈 输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的 n最大为16 利用回溯法 边生成边推断 就要快非常多了 #inc ...
- HDU1016 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(回溯)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- HDOJ1016 Prime Ring Problem(DFS深层理解)
Prime Ring Problem 时间限制: 200 ...
- hdu 1016 Prime Ring Problem(深度优先搜索)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Prime Ring Problem
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...
- UVA524 素数环 Prime Ring Problem
题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016: https://vjudge.net/problem/HDU-10 ...
随机推荐
- English trip V2 - 6 Sports Teacher:Taylor Key:phrasal verbs
In this lesson you will learn to talk about sports. 课上内容(Lesson) # How many different sports can you ...
- django+nginx+gunicorn+pipenv微信小程序实践笔记
一.我采用pipenv来管理虚拟环境,在本地新建虚拟环境: mkdir wxProject #进入环境目录,创建虚拟环境 pipenv install #激活虚拟环境 pipenv shell #然后 ...
- 敏捷开发——User Story
敏捷开发流程: 1.我们首先需要确定一个Product Backlog(按优先顺序排列的一个产品需求列表),这个是由Product Owner 负责的: 2.Scrum Team根据Product B ...
- list内含有元组或字典
a=[(")] for k,v,i in a: print(k,v,i) 结果: 1 21 12 2 31 32 list里含元组,可以用这种遍历输出挨个元素 但list里含字典时,这样就只 ...
- 在python3里面使用ueditor(基于adminx)
第一步,下载ueditor,在git上下载 直接download : https://github.com/twz915/DjangoUeditor3/ 然后放到任何一个文件夹里面 配置setting ...
- PAT 1116 Come on! Let's C
1116 Come on! Let's C (20 分) "Let's C" is a popular and fun programming contest hosted b ...
- hive -- 自定义函数和Transform
hive -- 自定义函数和Transform UDF操作单行数据, UDAF:聚合函数,接受多行数据,并产生一个输出数据行 UDTF:操作单个数据 使用udf方法: 第一种: add jar xxx ...
- bash: ./xxx 权限不够
Linux环境下要运行C编译的一个可执行文件play,终端cd到当前目录后输入./play,提示 bash: ./xxx 权限不够 用sudo, 提示 sudo:./play: command no ...
- 云计算概述和KVM虚拟化
前言: 近些年一直听着 虚拟化.云计算.公有云.私有云.混合云这些个概念,一直想着....这些概念要用什么技术实现? 一.云计算的概念 1.传统IDC机房面都会临什么问题? 任何新事物都是由需求催生的 ...
- 从安装Mac OS X虚拟机到第一个IOS程序
对于纯粹地抄这种行为是比较抵触的,别人已经写得挺好的东西没必要又去写一遍,但如果不写经验来看下次再做时自己又要重复百度筛选一遍,所以还是要记一记. 之前要获取IOS静态库的版本,但一直以来没有Mac没 ...