题目描述:

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. 在ISE查看各个模块消耗的资源

    这个是很多兄弟姐妹非常想知道的事情.我今天就简单和大家详细说一说.其实很简单,只是大家可能没有注意而已.把图上的-detail选定即可. 转载自:http://bbs.21ic.com/blog-73 ...

  2. 【Android】4.0 Android项目的基本结构

    分类:C#.Android.VS2015: 创建日期:2016-02-06: 修改日期:2016-02-27 一.简介 第3章虽然通过百度地图应用展示了你可能感兴趣的内容,但是,如果你是一个初学者,一 ...

  3. 强者联盟——Python语言结合Spark框架

    引言:Spark由AMPLab实验室开发,其本质是基于内存的高速迭代框架,"迭代"是机器学习最大的特点,因此很适合做机器学习. 得益于在数据科学中强大的表现,Python语言的粉丝 ...

  4. JDK自带工具之问题排查场景示例

    最近看到了大量关于java性能调优.故障排查的文章,自己也写了一篇< Java调优经验谈 >.接着此篇文章,其实一直打算写写一些常用调优工具以及它们的惯常用法的.后来在http://jav ...

  5. angular学习笔记(九)-css类和样式1

    本篇主要介绍通过数据绑定来给元素添加特定的类名,从而应用特定的样式 从一个最基本的例子来看: <!DOCTYPE html> <html ng-app> <head> ...

  6. 78. Longest Common Prefix【medium】

    Given k strings, find the longest common prefix (LCP).   Example For strings "ABCD", " ...

  7. 微信wap开发---页面自适应大小

    <meta name="viewport" content="width=device-width, initial-scale=0.5, minimum-scal ...

  8. How do I add elements to a Scala List?

    Scala List FAQ: How do I add elements to a Scala List? This is actually a trick question, because yo ...

  9. pre 强制换行

    当前位置:懒人建站 > javascript > 网页特效 > css强制pre标签换行 css强制pre标签换行,pre标签的功能就是保留文本源格式,将会保留文本的长度,空格 空行 ...

  10. c++之五谷杂粮---3

    3.1如果同一作用域内的几个函数名字相同但形参列表不同,我们称之为重载函数. 3.1.1不允许两个函数除了返回类型外所有的要素都相同.(这也是很好解释的) #include<iostream&g ...