Prime is 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
为了解决该问题,我们可以采用回溯法枚举每一个值。当第一个数位为1确定时,我们尝试放入第二个数,使其和1的和为素数,放入后再尝试放入第三个数,使其与第二个数的和为素数,直到所有的数全部被放入环中,且最后一个数与1的和也是素数,那么这个方案即为答案,输出;若在尝试放数的过程中, 发现当前位置无论放置任何之前未被使用的数均不可能满足条件,那么我们回溯 改变其上一个数,直到产生我们所需要的答案,或者确实不再存在更多的解。
#include "stdafx.h"
#include <stdio.h>
using namespace std; int prime[] = { , , , , , , , , , , , };
int number[];
bool hash[];
int n;
bool isPrime(int num)
{
for (int i = ; i < ;i++)
if (num == prime[i])
return true;
return false;
} void printArray()
{
if (isPrime(number[n] + number[]) == false)
return;
for (int i = ; i <= n; i++)
{
if (i != )
printf(" ");
printf("%d", number[i]);
}
printf("\n");
} void DFS(int num)
{
if (num > && isPrime(number[num] + number[num - ]) == false)
return;
if (num == n)
{
printArray();
return;
} for (int i = ; i <= n; i++)
{
if (hash[i] == false)
{
hash[i] = true;
number[num + ] = i;
DFS(num + );
hash[i] = false;
}
}
} int main()
{
int cas = ;
while (scanf("%d", &n) != EOF)
{
cas++;
for (int i = ; i < ; i++)
hash[i] = false;
number[] = ;
printf("Case %d:\n", cas);
hash[] = true;
DFS();
printf("\n");
} return ;
}
Prime is problem - 素数环问题的更多相关文章
- Hdu 1016 Prime Ring Problem (素数环经典dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 题目1459:Prime ring problem(素数环问题——递归算法)
题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- HDOJ 1016 Prime Ring Problem素数环【深搜】
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...
- Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black
Prime Ring Problem Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (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 ...
- hdu1016 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(回溯)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- UVA524 素数环 Prime Ring Problem
题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016: https://vjudge.net/problem/HDU-10 ...
随机推荐
- Centos7 防火墙关闭和启用iptables防火墙
操作系统环境:CentOS Linux release 7.0.1406(Core) 64位CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙步骤. 1.关闭f ...
- ModelSim之tcl自动化仿真
摘要: ModelSim的tcl最大的优势就在于它可以让整个仿真自动运行,免除每次进行各种用户界面控制操作的麻烦.用tcl就可以自动完成建库.映射库到物理目录.编译源代码.启动仿真器.运行仿真等一系列 ...
- ZYSocket 4.2.3 SOCKET框架组 发布[NEW]
http://blog.csdn.net/luyikk/article/details/6099337
- 史上最全的CDN内容分发网络实战技巧(网络优化)
今天来给大家分享下关于 CDN 的东西,以及我自己的一些发现.一些个人的拙见.总共分为 3 个部分:原理.详解.各种坑. 首先说一下 CDN 的基本原理部分,主要分 4 块来描述:CDN 的由来.调度 ...
- Hibernate 的一级缓存和二级缓存总结
缓存:缓存是什么,解决什么问题? 位于速度相差较大的两种硬件/软件之间的,用于协调两者数据传输速度差异的结构,均可称之为缓存Cache.缓存目的:让数据更接近于应用程序,协调速度不匹配,使访问速度更快 ...
- SourceInsight-Symbol not found
使用SourceInsight查看源代码时,发现点击查看相关类型时,无法关联到其代码,出现 symbol not found, 然而明明在我的头文件有定义的 网上查了一下主要是因为新建工程导入文件后, ...
- Oracle PLSQL Demo - 08.定义显式游标[Define CURSOR, Open, Fetch, Close CURSOR]
declare v_empno scott.emp.empno%type; v_sal scott.emp.sal%type; cursor cur_emp is select t.empno, t. ...
- 09、win32 转换为 store app
1.机制: 微软的 Project Centernial ( Project C) 项目的就是把传统的 windows桌面应用程序 转换为 windows10 store app (appx). 目的 ...
- How do I iterate over a Scala List (or more generally, a sequence) using theforeach method or for loop?
Scala List/sequence FAQ: How do I iterate over a Scala List (or more generally, a sequence) using th ...
- StarUML使用说明-指导手册
1.综述 StarUML是一种生成类图和其他类型的统一建模语言(UML)图表的工具.这是一个用Java语言描述的创建类图的简明手册. StarUML(简称SU),是一种创建UML类图,并能够自动生成J ...