Prime Ring Problem
 
                                                                    时间限制: 2000ms               内存限制: 32768KB                HDU       ID: 1016

题目描述

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 (0 < n < 20).

 

输出

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 输入n 原数列:1 2 3 ... n
解空间树思想求解,递归、回溯。相邻的每一个数都要求是素数,把第一数设定为 1 ,当然了是几都可以的,1有 n-1中选择,先从2开始往后选,符合条件放入a数组,由符合条件的这个数重复以上操作,直到最后一个确定出来则打印。

DFS深层理解:

原来对DFS的理解仅仅局限于图,现在发现这只是最基础的。DFS更多的表示的是一种状态,然后利用某中很简单的思维进行一次次的尝试,每次尝试成功了,就    深入一层递归进行下一次尝试,直到之后的尝试表明已经失败了不会成功,则回溯到这里。取消这次的尝试,去尝试其他的操作。简单地说,就是暴搜。只不过利用      了递归来实现尝试失败时的回溯,从而进行新的尝试。

代码:
#include <stdio.h>
#include <math.h>
bool use[];
int a[];
int n; bool isprime(int m)
{
int i;
for(i = ; i <= sqrt(m); i++)
{
if(m%i == )
{
return false;
}
}
return true;
}
void init(int n)
{
int i;
for(i=; i < n; i++)
{
use[i] = false;
}
a[] = ;//让第一个数是1,因为是一个圆,是其他任何数都可以
} void dfs(int num)//搜索当前第num位置应该放的数字
{
int i;
if(num == n && isprime( + a[n-]))//最后一个位置的比较
{
for(i=;i<n;i++)//打印出来
{
printf( i == n- ? "%d\n" : "%d ",a[i]);
}
}
else
{
for( i = ; i <= n; i++)//枚举每一个数
{
if( isprime( i+a[num-]) && !use[i] )//如果这个数符合和当前数成素数的条件并且没有使用过,就把它作为当前的下一个的相邻数
{
a[num] = i;//a[]存的是满足条件的数
use[i] = true;
dfs(num+);//继续搜索下一个位置的数
use[i] = false;//回收重新使用
}
}
}
} int main()
{
int t = ;
while(~scanf("%d",&n))
{
printf("Case %d:\n",++t);
init(n); dfs();
puts("");
}
return ;
}

HDOJ1016 Prime Ring Problem(DFS深层理解)的更多相关文章

  1. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  2. HDU 1016 Prime Ring Problem (DFS)

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

  3. Prime Ring Problem(dfs水)

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

  4. Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏

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

  5. Prime Ring Problem (DFS练习题)

    K - Prime Ring Problem ============================================================================= ...

  6. hdu1016 Prime Ring Problem(DFS)

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

  7. HDOJ-1016 Prime Ring Problem(DFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1016 题意:输入n,代表有一个包含n个节点的环,在环中的节点中填入1,2...n-1,n,要求填入的数与左边的数 ...

  8. [HDOJ1016]Prime Ring Problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 原题: A ring is compose of n circles as shown in d ...

  9. Prime Ring Problem dfs

    A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle ...

随机推荐

  1. 前端插件--fastclick解决点透问题

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. linux设置变量的三种方法

    1在/etc/profile文件中添加变量对所有用户生效(永久的) 用VI在文件/etc/profile文件中增加变量,该变量将会对Linux下所有用户有效,并且是“永久生效”. 例如:编辑/etc/ ...

  3. 获取电脑名和IP地址

    private string  GetHostNameAndIP( bool  isv4Orv6)        {            string HostName = Dns.GetHostN ...

  4. Django用户登陆以及跳转后台管理页面2

    请先写好以下,再来替换文件 Django用户登陆以及跳转后台管理页面1http://www.cnblogs.com/ujq3/p/7891774.html from django.shortcuts ...

  5. 【GDOI2017 day2】凡喵识图 二进制切分

    题面 100 有一个显然的做法是\(O(n^2)\): 想办法优化这个做法: 我们给一个64位整数,切分成四个16位整数. 那么如果两个64位整数符合汉明距离为3的话,那么两者切分的四个16位整数中: ...

  6. 使用Gradle构建项目

    使用gradle构建项目之前,咱们先聊聊maven,使用maven构建项目和配置主要 接下来正式使用gradle来构建项目的和步骤: 1.下载源码 2.下载gradle 1.下载地址:https:// ...

  7. oracle 索引监控

           索引对于在大量数据里检索出少量数据库的查询操作来说是高效的,可是对于DML操作来说.却是负面的:①其对于insert 操作的反面影响最大.该表的索引越多,更新的索引越多,insert 操 ...

  8. 生成mysql数据字典

    data_dictionary.php <?php /** * 生成mysql数据字典 */ header("Content-type: text/html; charset=utf- ...

  9. js正则表达式常见规则整理

    验证数字的正则表达式集 验证数字:^[0-9]*$ 验证n位的数字:^\d{n}$ 验证至少n位数字:^\d{n,}$ 验证m-n位的数字:^\d{m,n}$ 验证零和非零开头的数字:^(0|[1-9 ...

  10. [运维]ESXI系统的安装 标签: 虚拟机运维vmware服务器虚拟化 2017-05-05 09:24 496人阅读 评论(15)

    上篇博客说到了VMware vSphere,那么接下来就讲一下我们如何将之投入使用.vsphere的虚拟机管理程序就是esxi. 什么是ESXI? vSphere产品套件的核心产品是虚拟机管理程序,作 ...