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 ... 
随机推荐
- FPGA Prototyping By Verilog Examples第五章 状态机FSM设计
			上升沿检测电路之Moore型FSM // Listing 5.3module edge_detect_moore ( input wire clk, reset, input wire level, ... 
- ise和modelsim联合仿真的一些准备
			首先要在modelsim中编译xilinx的三个库,分别是unisims库,simprims库,和corelib库,其中unisims库全称为(library of united component ... 
- C++ new操作符详解
			一.new操作符的概念 我们通常讲的new是指的是new operator,其实还有另外两个概念,operator new 和 placement new. 1.new operator 我们在使用n ... 
- Makefile学习之路——4
			变量的类别有递归扩展变量和简单扩展变量.只用一个“=”符号定义的变量被称为递归扩展变量.通过下面例子观察递归扩展变量的特点. .PHONY: all foo=$(bar) bar=$(ugh) ugh ... 
- windows下好用的markdown编辑器
			Markdown是一种用来写作的轻量级[标记语言],它用简洁的语法代替了排版.字体设置,使我们可以专心写作,目前被越来越多的开发者,写作爱好者使用.Markdown的语法十分简单,常用的标记不超过十个 ... 
- C++基础学习-20120516
			1.一下是使用strcpy_s与strcpy的安全性比较 char szBuf[2] = {0}; strcpy_s(szBuf, 2, "12131"); //新的CRT函数 ... 
- java 关于同步异步的理解
			经常看到介绍 ArrayList 和HashMap是异步,Vector和HashTable是同步,这里同步是线程安全的,异步不是线程安全的,举例说明: 当创建一个Vector对象时候, Vector ... 
- MongoDB学习之(一)安装
			第一步:下载MongoDB的安装版进行安装 https://pan.baidu.com/s/1X3hIqORJ61TCG1UJ_yr6ag 由于第二次安装出现一些问题,所有还是记录一下,免得以后踩坑. ... 
- eql高可用部署方案
			运行环境 服务器两台(后面的所有配置案例都是以10.96.0.64和10.96.0.66为例) 操作系统CentOS release 6.2 必须要有共同的局域网网段 两台服务器都要安装keepali ... 
- SpringMVC 之类型转换Converter详解转载
			SpringMVC之类型转换Converter详解 本文转载 http://www.tuicool.com/articles/uUjaum 1.1 目录 1.1 目录 1.2 ... 
