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 ...
随机推荐
- C(n,m)排列组合算法
主要解决C(n,m)问题 static class Extension { public static IList<IList<T>> GetGroup<T>(th ...
- MariaDB主从复制,redis发布订阅,持久化,以及主从同步
一. MariaDB主从复制 mysql基本操作 1 连接数据库 mysql -u root -p -h 127.0.0.1 mysql -u root -p -h 192.168.12.60 2 ...
- Activiti流程变量五步曲 ——by fightingKing
http://blog.csdn.net/zwk626542417/article/details/46648139 一.前言 上一篇文章我们将流程实例的启动与查询,任务的办理查询都进行了介绍,我们这 ...
- linux 一键安装lnmp环境
①,登陆后运行:screen -S lnmp ②.如果提示screen: command not found 命令不存在可以执行: yum install wget ③. 执行命令:wget ...
- WinForm界面设计-Button添加背景图去边框
转自:https://www.cnblogs.com/tommy-huang/p/4283538.html 1.既然是添加背景图片 所以这里应该使用 Button.BackgroudImage = & ...
- python_vlc 播放http流
由于工作需要,项目功能测试用到python调用vlc,用于播放一些直播流,各种尝试后终于可以了,其实蛮简单的,就是由于没有参考自己折腾挺浪费时间的,所以把结果贴出来给需要用到的人. 准备工作: 1.首 ...
- linux常用命令 cut字符截取命令
cut 字符截取 cut [选项] 文件名 选项 -f 列号: 提取第几列 -d 分割符:按照指定的分隔符分割列 默认使用制表符("tab")进行分割 ssgao:Download ...
- Go-ethereum源码解析-Part I
1. 整体概览 makefile CANDY: .PHONY: geth android ios geth-cross swarm evm all test clean 已知phony 目标并非是由其 ...
- keeplived
keepalived高可用集群. keepalived故障切换转移原理1vrrp协议:(vritual router redundancy protocol)虚拟路由冗余协议,2故障转移.keep ...
- 六、Linux的用户和组
1. 用户的管理 useradd 添加一个用户 useradd test 添加test用户 useradd test -d /home/t1 指定用户home目录 或者直接 adduser tes ...