题目描述:

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 - 素数环问题的更多相关文章

  1. Hdu 1016 Prime Ring Problem (素数环经典dfs)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. 题目1459:Prime ring problem(素数环问题——递归算法)

    题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  3. HDOJ 1016 Prime Ring Problem素数环【深搜】

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...

  4. Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black

    Prime Ring Problem Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) ...

  5. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  6. hdu1016 Prime Ring Problem【素数环问题(经典dfs)】

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  8. [HDU 1016]--Prime Ring Problem(回溯)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  9. UVA524 素数环 Prime Ring Problem

    题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016:  https://vjudge.net/problem/HDU-10 ...

随机推荐

  1. 聊天server-解密陌生人(10)位置管理和获取周围一公里陌生人

    提示: 由于project略微有点大对我个人来说.所以可能在某些方面讲的不清楚或逻辑性不够强,假设有问题请@我. 原project:https://github.com/LineChen/ 六.用户位 ...

  2. vim-snipmate的c.snippets(2016.7.10)

    ## Main # main snippet main int main ( void ) { ${} ; } ##include snippet inc #include <${:stdio} ...

  3. 正则表达式入门(c#)

    本文是对该教程的学习练习 http://www.jb51.net/tools/zhengze.html 注:正则符号转义和普通的转义一样,加反斜杠,比如[ 变成 \[ 正则表达式符号和转义符号最好用+ ...

  4. NGUI学习笔记(六):ScrollView、Grid和Table

    下面我们来看看游戏UI开发中比较核心的开发,我称为列表开发,比如背包和各种形式不一的列表等,下面我们来看几个具体的样例:   基本上就是一些重复的制作好的多个UI控件进行排列,同时可以支持滚动,当然, ...

  5. 树形列表 jqtree数据 使用

    jqtree调试笔记 用的是data-url的方式从远程加载的数据返回的数据格式须要是json,当然也可以使用text模式,但是恐怕要自己写格式的解析了 其中返回数据的时候,是在这个函数的末尾_loa ...

  6. pssh安装和使用

    前提,安装了python.pssh安装下载pssh-2.2.2.tar.gztar zxvf pssh-2.2.2.tar.gzcd pssh-2.2.2python setup.py buildpy ...

  7. Android开发日记(二)

    HashMap<String, Object> map;定义一个HashMap用来传递字符 TextView textView_JobTitle=(TextView)findViewByI ...

  8. 简易web项目jdbcUtil

    jdbc.username=root jdbc.password=root jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://mini1: ...

  9. MapReduce的集群行为和框架

    MapReduce的集群行为 MapReduce的集群行为包括: 1.任务调度与执行MapReduce任务由一个JobTracker和多个TaskTracker两类节点控制完成.(1)JobTrack ...

  10. linux支持的内存根文件系统

    linux支持两种内存根文件系统:ramdisk和initramfs. ---------------------------------------------------------------- ...